-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
406 lines (357 loc) · 10.8 KB
/
game.cpp
File metadata and controls
406 lines (357 loc) · 10.8 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* Author: Nolan Schirripa, Christine Seng, Erick Martinez, Georgia Rushing, Graham Balas
* Assignment Title: Group Project (game.cpp)
* Assignment Description: defines game class to run and update game
* Due Date: 12/09/2024
* Date Created: 12/6/2024
* Date Last Modified: 12/07/2024
*/
#include "game.h"
#include "SDL_Plotter.h"
int Game::maxDifficulty = 3;
int Game::minDifficulty = 1;
Game::Game()
{
ballPoint = {500, 50};
shooter.setBallLoc(ballPoint);
//Initialize SDL_Image
if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) {
cerr << "SDL_image could not initialize! SDL_image Error: "
<< IMG_GetError() << endl;
}
}
Game::~Game()
{
// Existing cleanup
IMG_Quit();
}
void Game::run()
{
currentState = START_SCREEN;
while (!g.getQuit())
{
Block endLine;
point p1 = {0, 100};
point p2 = {1000, 100};
fpsTimer.start();
g.clear();
switch (currentState)
{
case START_SCREEN:
drawStartScreen();//TODO
if (g.kbhit())
{
int key = g.getKey();
switch (key)
{
case ' ': // Space key to start game
currentState = PLAYING;
ballPoint = {500, 50};
shooter.setBallLoc(ballPoint);
break;
case 27: // ESC key to quit
g.setQuit(true);
break;
}
}
break;
case PLAYING:
g.initSound("assets/soundHit.wav");
// Existing game loop logic
shape.drawLevel(startLoc, g);
shooter.drawBall(ballPoint, BALLSIZE, BLACKCOLOR, g);
endLine.drawLine(p1, p2, BLACKCOLOR, g);
if (levelChanged)
{
shape.createLevel(startLoc);
levelChanged = false;
}
if (g.mouseClick())
{
launchBall();
}
else if (isFalling)
{
ballFalling();
}
if (bottomHit())
{
updateLevel();
// Check if game over condition is met
if (checkGameOver())//TODO
{
currentState = GAME_OVER;
}
isFalling = false;
}
g.update(score);
break;
case GAME_OVER:
g.clear();
drawEndScreen();//TODO
// Check for restart or quit
if (g.kbhit())
{
int key = g.getKey();
switch (key)
{
case 27: // ESC key to quit
g.setQuit(true);
break;
}
}
break;
}
// Update score on screen
/*g.update(score);*/
// FPS control
g.Sleep(10);
++countedFrames;
}
g.setQuit(true);
g.Sleep(500);
}
bool Game::checkGameOver() {
for (int i = 0; i < shape.getAllActiveShapes().size(); ++i) {
Block ¤tBlock = shape.getAllActiveShapes().at(i);
if (currentBlock.getLocation().y <= 150 && currentBlock.getLife() != 0) {
setGameOver(true);
}
}
return getGameOver();
}
void Game::drawStartScreen()
{
// Clear the renderer first
SDL_RenderClear(g.getRenderer());
// Load the start screen image
SDL_Surface* startScreenSurface = IMG_Load("assets/startScreen.png");
if (!startScreenSurface) {
std::cerr << "Unable to load start screen image! SDL_image Error: "
<< IMG_GetError() << std::endl;
SDL_RenderPresent(g.getRenderer());
exit(1);
}
// Convert surface to texture
SDL_Texture* startScreenTexture = SDL_CreateTextureFromSurface(g.getRenderer(), startScreenSurface);
if (!startScreenTexture) {
std::cerr << "Unable to create texture from start screen surface! SDL Error: "
<< SDL_GetError() << std::endl;
SDL_RenderPresent(g.getRenderer());
SDL_FreeSurface(startScreenSurface);
exit(1);
}
// Render the entire texture
SDL_RenderCopy(g.getRenderer(), startScreenTexture, NULL, NULL);
// Present the renderer
SDL_RenderPresent(g.getRenderer());
// Clean up
SDL_FreeSurface(startScreenSurface);
SDL_DestroyTexture(startScreenTexture);
}
void Game::drawEndScreen()
{
// Clear the renderer first
SDL_RenderClear(g.getRenderer());
// Load the end screen image
SDL_Surface* endScreenSurface = IMG_Load("assets/endScreen.png");
if (!endScreenSurface) {
std::cerr << "Unable to load end screen image! SDL_image Error: "
<< IMG_GetError() << std::endl;
// Fallback to the original circle drawing if image fails to load
shape.drawCircle(startLoc, 50, {0,255,0}, g);
SDL_RenderPresent(g.getRenderer());
return;
}
// Convert surface to texture
SDL_Texture* endScreenTexture = SDL_CreateTextureFromSurface(g.getRenderer(), endScreenSurface);
if (!endScreenTexture) {
std::cerr << "Unable to create texture from end screen surface! SDL Error: "
<< SDL_GetError() << std::endl;
// Fallback to the original circle drawing if texture creation fails
shape.drawCircle(startLoc, 50, {0,255,0}, g);
SDL_RenderPresent(g.getRenderer());
SDL_FreeSurface(endScreenSurface);
return;
}
// Render the entire texture
SDL_RenderCopy(g.getRenderer(), endScreenTexture, NULL, NULL);
// Present the renderer
SDL_RenderPresent(g.getRenderer());
// Clean up
SDL_FreeSurface(endScreenSurface);
SDL_DestroyTexture(endScreenTexture);
}
bool Game::bottomHit()
{
return ballPoint.y > 930;
}
void Game::launchBall()
{
clickPos = g.getMouseClick();
ballPoint.y = 50;
ballPoint.x = 500;
xPos = ballPoint.x;
yPos = ballPoint.y;
xDist = clickPos.x - ballPoint.x;
yDist = clickPos.y - ballPoint.y;
shooter.setMagnitude(13);
shooter.setDirection(atan(static_cast<double>(xDist) / yDist));
isFalling = true;
firstHit = false;
}
void Game::ballFalling()
{
// apply gravity
if (firstHit)
{
shooter.apply(GRAVITY);
}
// change y and x pos based on magnitude and direction
yPos += shooter.getMagnitude() * cos(shooter.getDirection());
ballPoint.y = static_cast<int>(yPos);
if (ballPoint.y < BALLSIZE + 3)
{
ballPoint.y = BALLSIZE + 3;
}
xPos += shooter.getMagnitude() * sin(shooter.getDirection());
ballPoint.x = static_cast<int>(xPos);
if (ballPoint.x < BALLSIZE + 3)
{
ballPoint.x = BALLSIZE + 3;
}
else if (ballPoint.x > g.getCol() - BALLSIZE - 3)
{
ballPoint.x = g.getCol() - BALLSIZE - 3;
}
// update flag positions
ballFlags.update(ballPoint.x, ballPoint.y, BALLSIZE, shooter.getForce());
// if hit left or right wall
if (ballPoint.x < 15 || ballPoint.x > 985)
{
shooter.setDirection(2 * M_PI - shooter.getDirection());
}
for (int i = 0; i < shape.getAllActiveShapes().size(); ++i)
{
if (HitBox::isHit(shooter.getHitBox(), shape.getAllActiveShapes().at(i).getHitBox()))
{
// if hit box detected, then check if flags also detect hit in correct direction
checkHits(i);
}
}
// If frame finished early syncing with fps
frameTicks = fpsTimer.getTicks();
if (frameTicks < SCREEN_TICKS_PER_FRAME)
{
// Wait remaining time
SDL_Delay(SCREEN_TICKS_PER_FRAME - frameTicks);
}
}
void Game::checkHits(int index)
{
int flagNum = ballFlags.isHit(g);
if (flagNum != -1)
{
// if first hit, turn on gravity
if (!firstHit)
{
firstHit = true;
}
shooter.setMagnitude(shooter.getMagnitude() - 0.4);
if (shooter.getMagnitude() < 0)
{
shooter.setMagnitude(0);
isFalling = false;
}
Block& currentBlock = shape.getAllActiveShapes().at(index);
g.playSound("assets/soundHit.wav");
currentBlock.decreaseLife();
if (currentBlock.getLife() <= 0)
{
++score;
}
if (Game::score % 5 == 0)
{
if (Game::maxDifficulty <= 7)
{
++Game::maxDifficulty;
}
if (Game::minDifficulty <= 5)
{
++Game::minDifficulty;
}
}
}
if (flagNum == 0)
{
// top hit
shooter.setDirection(M_PI - shooter.getDirection());
}
else if (flagNum == 2)
{
// bottom hit
shooter.setDirection(3 * M_PI - shooter.getDirection());
// if direction is close to straight up/down, set it to the closest PI/8 value so that it doesn't get stuck bouncing up/down
if (shooter.getDirection() >= 7.0 * M_PI / 8.0 && shooter.getDirection() <= M_PI)
{
shooter.setDirection(7.0 * M_PI / 8.0);
}
else if (shooter.getDirection() >= M_PI && shooter.getDirection() <= 9.0 * M_PI / 8.0)
{
shooter.setDirection(9.0 * M_PI / 8.0);
}
}
else if (flagNum == 1)
{
// right hit
shooter.setDirection(0 - shooter.getDirection());
if ((shooter.getDirection() > 15.0 / 16.0 * M_PI && shooter.getDirection() < 17.0 / 16.0 * M_PI) || (shooter.
getDirection() > 31.0 / 16.0 * M_PI) || shooter.getDirection() < 1.0 / 16.0 * M_PI)
{
shooter.apply(PUSHLEFT);
}
}
else if (flagNum == 3)
{
// left hit
shooter.setDirection(0 - shooter.getDirection());
if ((shooter.getDirection() > 15.0 / 16.0 * M_PI && shooter.getDirection() < 17.0 / 16.0 * M_PI) || shooter.
getDirection() > 31.0 / 16.0 || shooter.getDirection() < 1 / 16.0 * M_PI)
{
shooter.apply(PUSHRIGHT);
}
}
else if (flagNum == 4)
{
// top right corner hit
shooter.setDirection(shooter.getDirection() - M_PI);
}
else if (flagNum == 5)
{
// bottom right corner hit
shooter.setDirection(M_PI + shooter.getDirection());
}
else if (flagNum == 6)
{
// bottom left corner hit
shooter.setDirection(M_PI + shooter.getDirection());
}
else if (flagNum == 7)
{
// top left corner hit
shooter.setDirection(shooter.getDirection() - M_PI);
}
}
void Game::updateLevel()
{
g.clear();
// reset the ball position to the top
ballPoint.x = 500;
ballPoint.y = 50;
isFalling = false;
// redraw the ball and update/draw the new level
shooter.drawBall(ballPoint, BALLSIZE, BLACKCOLOR, g);
shape.nextLevel();
levelChanged = true;
shape.drawLevel(startLoc, g);
}