-
Notifications
You must be signed in to change notification settings - Fork 1
/
medium_mode.cpp.qx3500
306 lines (247 loc) · 10.1 KB
/
medium_mode.cpp.qx3500
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "medium_mode.h" // for easy_mode function declarations in header
#include "ui_medium_mode.h"
#include <QPainter> // for QPainter
#include <QDebug> // for qDebug()/debugging purposes
#include <QPoint> // for setX/setY functions
#include <QSize> // for our size variables (i.e. how big/long our baller and hoop and ball is)
#include <QTimer> // for animating the hoop (and the ball maybe?)
#include <QRect>
#include <string>
#include <sstream>
// EASY MODE CONSTRUCTOR!
medium_mode::medium_mode(QWidget *parent) : QWidget(parent), ui(new Ui::medium_mode)
{
ui->setupUi(this);
// background images...
background_image = new QPixmap(":/images/elliot bball game court 1 copy (small).jpg"); // background
baller_image = new QPixmap(":/images/test_kobe.jpg");
hoop_image = new QPixmap(":/images/elliot-bball-game-hoop-1.png");
ball_image = new QPixmap(":/images/elliot-bball-game-ball-1.png");
// first we grab our screen dimensions
screen_width = this->width();
screen_height = this->height();
// initialize baller_size and baller_position
baller_size = new QSize(screen_width/12, screen_height/4); //now we have the size
baller_position = new QPoint(screen_width/2, screen_height - baller_size->height());
// initialize hoop_size and hoop_position
hoop_size = new QSize(screen_width/6, screen_height/9);
hoop_position = new QPoint(screen_width/2, 0);
// let's set up the ball variables...
ball_size = new QSize(screen_width/12, screen_width/12);
ball_default_height = baller_position->y() - ball_size->height();
ball_position = new QPoint(baller_position->x(), ball_default_height); // want to match baller position
// we need a separate timer so that our timer on screen increments down per second
shot_clock_per_second = new QTimer(this);
connect(shot_clock_per_second, SIGNAL(timeout()), this, SLOT(display_time_remaining()));
// now let's create a new timer that signals the end of the game...
shot_clock = new QTimer(this);
connect(shot_clock, SIGNAL(timeout()), parent, SLOT(game_over()));
shot_clock->setSingleShot(true);
// start both the shot_clock and shot_clock_per_second
shot_clock->start(60000); // should last us 90 seconds/1 min 30 seconds
shot_clock_per_second->start(1000); // triggers every one second
// initialize defender position
defender_size = new QSize(screen_width/6, screen_height/9);
defender_position = new QPoint((screen_width - defender_size->width())/2, hoop_size->height()); // start the defender right under
// connect our hoop, ball and defender to the timer and start that shit
hoop_and_ball_and_defender_timer = new QTimer(this); // might need to delete this stuff....
connect(hoop_and_ball_and_defender_timer, SIGNAL(timeout()), this, SLOT(animate_hoop())); // but don't start yet...
connect(hoop_and_ball_and_defender_timer, SIGNAL(timeout()), this, SLOT(animate_ball()));
connect(hoop_and_ball_and_defender_timer, SIGNAL(timeout()), this, SLOT(animate_defender())); // also link up the defender to the timer
hoop_and_ball_and_defender_timer->start(10);
qDebug() << "MEDIUM MODE ACTIVATED";
}
void medium_mode::display_time_remaining()
{
--time_remaining;
// need to convert our time_remaining to a string, then a QString
std::string time_string;
std::ostringstream temp;
temp << time_remaining;
time_string = temp.str();
QString qs = QString::fromStdString(time_string);
ui->label_2->setText(qs);
}
//DESTRUCTOR!!!!
medium_mode::~medium_mode()
{
// Let's clean house...
delete background_image;
delete hoop_and_ball_and_defender_timer;
delete shot_clock;
delete shot_clock_per_second;
delete baller_position;
delete baller_size;
delete baller_image;
delete defender_position;
delete defender_size;
delete defender_image;
delete hoop_position;
delete hoop_size;
delete hoop_image;
delete ball_position;
delete ball_size;
delete ball_image;
delete ui;
}
//our paint event
void medium_mode::paintEvent(QPaintEvent *e)
{
QPainter fancy_french_painter(this);
// background image!
fancy_french_painter.drawPixmap(0,0,this->width(),this->height(), *background_image);
// DRAW THE KOBE!
fancy_french_painter.drawPixmap(baller_position->x(), baller_position->y(), baller_size->width(), baller_size->height(), *baller_image);
// DRAW THE HOOP
fancy_french_painter.drawRect(hoop_position->x(), hoop_position->y(), hoop_size->width(), hoop_size->height());
// DRAW THE DEFENDER
fancy_french_painter.drawRect(defender_position->x(), defender_position->y(), defender_size->width(), defender_size->height());
// DRAW THE BALL
fancy_french_painter.drawPixmap(ball_position->x(), ball_position->y(), ball_size->width(), ball_size->height(), *ball_image);
}
//let's get keyboard input here...
//after adding in the showEvent code, we have activation now...
void medium_mode::keyPressEvent(QKeyEvent* event)
{
switch(event->key())
{
case Qt::Key_Left: // if the key press is left, then...
if(baller_position->x() > 0) // move the baller's location left
{
baller_position->setX(baller_position->x() - x_change_per_press);
// we also want to change ball position if it was not shot yet
if(ball_in_motion == false)
ball_position->setX(ball_position->x() - x_change_per_press);
}
break;
case Qt::Key_Right: // if the key press is right, then...
if(baller_position->x() < (screen_width - baller_size->width())) // move the baller's location right
{
baller_position->setX(baller_position->x() + x_change_per_press);
// we also want to change ball position if it was not shot yet
if(ball_in_motion == false)
ball_position->setX(ball_position->x() + x_change_per_press);
}
break;
case Qt::Key_Space:
// if the ball has been reset...
if(ball_position->y() == ball_default_height)
{
ball_in_motion = true;
}
break;
}
//repaint the screen with updated positions
this->repaint();
}
//but need focus for keyboard input...
void medium_mode::showEvent(QShowEvent *event)
{
//widget activated
this->activateWindow();
//now the easy_mode widget/object gets focus
this->setFocus();
//not sure what this does...
QWidget::showEvent(event);
}
// this function will check to see if the ball made it into the hoop or not
bool medium_mode::is_ball_in_hoop()
{
// swish?
bool swish;
// let's grab the ball's current center x coordinate
size_t ball_x_center = ball_position->x() + (ball_size->width())/2;
// now check to see if the ball swished through the hoop!
if(ball_x_center > hoop_position->x() && ball_x_center < (hoop_position->x() + hoop_size->width()))
{
swish = true;
this->points += 2;
// now change the text on screen
display_points();
}
else
{
swish = false;
}
return swish;
}
void medium_mode::display_points()
{
// now convert our score to a string, then a QString
std::string points_string;
std::ostringstream temp2;
temp2 << points;
points_string = temp2.str();
QString qs2 = QString::fromStdString(points_string);
ui->label_4->setText(qs2);
}
// this will aninate our hoop left and right...
void medium_mode::animate_hoop()
{
// if the hoop is too far to the right...
if((hoop_position->x() + hoop_size->width()) >= screen_width)
{
hoop_change_per_timeout *= -1; // we reverse the change
}
// if the hoop is too far to the left...
else if(hoop_position->x() <= 0)
{
hoop_change_per_timeout *= -1;
}
//now add the hoop_change_per_timeout to the position
hoop_position->setX(hoop_position->x() + hoop_change_per_timeout);
//call for a repaint()
this->repaint();
}
// this will get our ball up towards the basket
void medium_mode::animate_ball()
{
size_t ball_x = ball_position->x();
size_t ball_y = ball_position->y();
size_t defender_x = defender_position->x();
size_t defender_y = defender_position->y();
// if the ball is within the defender
if(ball_y <= defender_y + defender_size->height()
&& ball_y >= defender_y
&& (ball_x + ball_size->width()/2) >= defender_x
&& (ball_x + ball_size->width()/2) <= defender_x + defender_size->width())
{
qDebug() << "DIKEMBO MUTOMBE";
// now reset the ball
ball_position->setX(baller_position->x());
ball_position->setY(ball_default_height);
// set ball_in_motion as false
ball_in_motion = false;
}
// so if the ball passes the hoop's bottom side
if(ball_position->y() <= hoop_size->height())
{
// this will increment our score or not depending on if the ball was in the hoop!
is_ball_in_hoop();
// reset the ball
ball_position->setX(baller_position->x());
ball_position->setY(ball_default_height);
// set ball_in_motion as false
ball_in_motion = false;
}
if(ball_in_motion == true)
{
ball_position->setY(ball_position->y() - ball_change_per_timeout);
}
this->repaint();
}
// animates our defender left and right!
void medium_mode::animate_defender()
{
// if the defender is too far right...
if(defender_position->x() >= screen_width - defender_size->width())
{
defender_change_per_timeout *= -1;
}
else if(defender_position->x() <= 0)
{
defender_change_per_timeout *= -1;
}
defender_position->setX(defender_position->x() + defender_change_per_timeout);
this->repaint();
}