-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·544 lines (481 loc) · 14 KB
/
main.cpp
File metadata and controls
executable file
·544 lines (481 loc) · 14 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include <iostream>
#include <fstream>
#include <ostream>
#include <SFML/Graphics.hpp>
#include "include/globals.hpp"
#include "include/Platform.hpp"
#include "include/Background.hpp"
#include "include/Playground.hpp"
#include "include/Player.hpp"
#include "include/Enemy.hpp"
#include "include/Pickup.hpp"
#include "include/SpecialEffects.hpp"
using namespace sf;
class GameInput
{
public:
int moveX;
bool spaceBar, keyP;
GameInput() : moveX(0), spaceBar(0), keyP(0) {}
};
class GameState
{
public:
enum States
{
Title,
Playing,
Paused,
GameOver
};
int currentState, currentScore, highestScore;
const char *highscoreFilename;
GameState()
{
highestScore = 0;
highscoreFilename = "hiscore";
}
void loadHighScore()
{
std::ifstream inputFile(highscoreFilename);
inputFile >> highestScore;
inputFile.close();
}
void saveHighScore()
{
std::ofstream outputFile(highscoreFilename);
outputFile << highestScore;
outputFile.close();
}
bool onTitle()
{
return currentState == Title;
}
bool onGameOver()
{
return currentState == GameOver;
}
bool onPause()
{
return currentState == Paused;
}
bool onPlaying()
{
return currentState == Playing;
}
void set(int newState)
{
currentState = newState;
if (newState == GameOver)
{
if (currentScore > highestScore)
{
highestScore = currentScore;
saveHighScore();
}
}
}
};
class JumpGame
{
public:
RenderWindow window;
Font font;
GameInput input;
GameState state;
Texture *gameTextures;
Background *background;
Playground *playground;
Player *player;
Platform *platform;
Enemy *enemy;
Pickup *pickup;
SpecialEffects *sparks;
std::vector<Enemy *> bombs;
int maxBombs;
JumpGame() : window(VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Jump!")
{
window.setFramerateLimit(60);
font.loadFromFile("font.ttf");
state.currentState = GameState::Title;
state.loadHighScore();
/*
Free assets:
- platforms: https://opengameart.org/content/cute-platformer-sisters
- clouds: https://opengameart.org/content/old-frogatto-clouds
- creeper: https://www.spriters-resource.com/custom_edited/minecraftcustoms/sheet/91008
*/
gameTextures = loadTexture("textures.png");
background = new Background(gameTextures);
player = new Player(gameTextures);
playground = new Playground(gameTextures);
enemy = new Enemy(gameTextures);
bombs.push_back(enemy);
pickup = new Pickup(gameTextures);
sparks = new SpecialEffects();
maxBombs = 1;
}
void restartGame()
{
state.currentScore = 0;
player->posX = rand() % 550;
player->posY = 400;
player->velX = 0;
player->velY = 0;
player->fuel = 10;
playground->GeneratePlatforms();
pickup->posX = rand() % 550;
pickup->posY = -300;
pickup->velY = 0;
pickup->state = Pickup::Falling;
clearIteratorAndElements(&bombs);
enemy = new Enemy(gameTextures);
enemy->posY = -300;
enemy->posX = rand() % 550;
enemy->velY = 0;
enemy->state = Enemy::Falling;
bombs.push_back(enemy);
}
void Update()
{
background->Update();
sparks->Update();
if (state.onTitle())
{
if (input.spaceBar)
{
state.set(GameState::Playing);
input.spaceBar = 0;
restartGame();
}
}
else if (state.onPlaying())
{
if (input.moveX > 0) // JUMP RIGHT
{
if (player->velX < 5)
{
player->velX += 0.2;
}
}
else if (input.moveX < 0) // JUMP LEFT
{
if (player->velX > -5)
{
player->velX -= 0.2;
}
}
// Move the player higher and the platforms lower
if (player->posY < 200)
{
player->posY = 200;
state.currentScore++;
background->MoveUp(player->velY);
playground->MoveDown(player->velY);
// adjust the position of the collectible pickup
if (pickup->state == Pickup::Stopped)
{
pickup->state = Pickup::Falling;
}
pickup->posY = pickup->posY - player->velY;
// adjust bombs positions
for (std::vector<Enemy *>::iterator it = bombs.begin(); it != bombs.end(); ++it)
{
if ((*it)->state == Pickup::Stopped)
{
(*it)->state = Pickup::Falling;
}
(*it)->posY = (*it)->posY - player->velY;
}
}
if (input.spaceBar) // JETPACK USAGE
{
if (player->fuel > 0)
{
player->velY = -8;
player->fuel--;
player->state = Player::UsingJetPack;
}
else
{
player->state = Player::Idle;
}
}
else
{
player->state = Player::Idle;
}
// DETECT COLLISIONS
playground->CheckPlayerCollisions(player);
playground->CheckEnemyCollisions(bombs);
playground->CheckPickupCollisions(pickup);
// reuse the same enemy if it falls off the screen
for (std::vector<Enemy *>::iterator it = bombs.begin(); it != bombs.end(); ++it)
{
if ((*it)->posY > 800)
{
(*it)->posY = -300;
(*it)->posX = rand() % 570;
(*it)->velY = 0;
(*it)->state = Enemy::Falling;
}
}
// update final positions and animations
player->Update();
playground->Update();
pickup->Update();
for (std::vector<Enemy *>::iterator it = bombs.begin(); it != bombs.end(); ++it)
{
(*it)->Update();
}
// check if it's GAME OVER when the player collides with an enemy
for (std::vector<Enemy *>::iterator it = bombs.begin(); it != bombs.end(); ++it)
{
if (player->Collided(*it))
{
sparks->SpawnEffect(player->posX, player->posY, 30);
state.set(GameState::GameOver);
return;
}
}
// fuel up when the player collides with an pickup
if (player->Collided(pickup))
{
if (player->fuel < 250)
{
player->fuel += 50;
if (player->fuel > 250)
{
player->fuel = 250;
}
}
// reuse the same pickup if it falls off the screen
pickup->posX = rand() % 550;
pickup->posY = -300;
pickup->velY = 0;
pickup->state = Pickup::Falling;
}
spawnBombs();
// its also game over when the player falls off the screen
if (player->posY + player->sizeY >= 800)
{
state.set(GameState::GameOver);
maxBombs = 1;
return;
}
if (input.keyP) // PAUSE
{
state.set(GameState::Paused);
input.keyP = 0;
}
}
else if (state.onPause())
{
if (input.keyP)
{
state.set(GameState::Playing);
input.keyP = 0;
}
}
else if (state.onGameOver())
{
if (input.spaceBar)
{
state.set(GameState::Title);
input.spaceBar = 0;
}
}
}
void spawnBombs()
{
if (state.currentScore > 200 and state.currentScore < 400)
{
maxBombs = 2;
}
else if (state.currentScore > 400 and state.currentScore < 600)
{
maxBombs = 3;
}
else if (state.currentScore > 600 and state.currentScore < 700)
{
maxBombs = 4;
}
else if (state.currentScore > 700 and state.currentScore < 900)
{
maxBombs = 5;
}
else if (state.currentScore > 1000 and state.currentScore < 1800)
{
maxBombs = 6;
}
else if (state.currentScore > 1800)
{
maxBombs = 10; // good luck! ;)
}
else
{
maxBombs = 1;
}
if (bombs.size() < maxBombs)
{
for (int i = 0; i < (maxBombs - bombs.size()); i++)
{
enemy = new Enemy(gameTextures);
enemy->posY = -1 * (rand() % 400 + 200);
enemy->posX = rand() % 550;
enemy->velY = 0;
enemy->state = Enemy::Falling;
bombs.push_back(enemy);
}
}
}
void Render()
{
window.clear(Color(76, 180, 255));
background->Render(window);
sparks->Render(window);
if (state.onTitle())
{
RenderText("Jump!", 185, 260, 64);
}
else if (state.onPlaying() or state.onPause())
{
playground->Render(window);
pickup->Render(window);
for (std::vector<Enemy *>::iterator it = bombs.begin(); it != bombs.end(); ++it)
{
(*it)->Render(window);
}
player->Render(window);
RenderText("score: ", 10, 10, 22);
RenderNumber(state.currentScore, 110, 10, 22);
RenderText("hi: ", 200, 10, 22);
RenderNumber(state.highestScore, 240, 10, 22);
RenderText("fuel: ", 480, 10, 22);
RenderNumber(player->fuel, 550, 10, 22);
}
else if (state.onGameOver())
{
RenderText("Game Over", 100, 260, 60);
RenderText("score: ", 155, 350, 32);
RenderNumber(state.currentScore, 310, 350, 32);
}
window.display();
}
void RenderText(const char *string, int x, int y, int size)
{
sf::Text text;
text.setFont(font);
text.setString(string);
text.setFillColor(Color(255, 255, 255));
text.setCharacterSize(size);
text.setPosition(x, y);
window.draw(text);
}
void RenderNumber(int number, int x, int y, int size)
{
sf::Text text;
text.setFont(font);
text.setString(std::to_string(number));
text.setFillColor(Color(255, 255, 255));
text.setCharacterSize(size);
text.setPosition(x, y);
window.draw(text);
}
void run()
{
// const sf::Vector2u windowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
// window.setSize(windowSize);
while (window.isOpen())
{
Event e;
while (window.pollEvent(e))
{
switch (e.type)
{
case Event::Closed:
window.close();
break;
// case Event::Resized:
// window.setSize(windowSize);
// break;
case sf::Event::KeyPressed:
switch (e.key.code)
{
case sf::Keyboard::Left:
input.moveX = -1;
break;
case sf::Keyboard::Right:
input.moveX = 1;
break;
case sf::Keyboard::Space:
input.spaceBar = 1;
break;
case sf::Keyboard::P:
input.keyP = 1;
break;
default:
break;
}
break;
case sf::Event::KeyReleased:
switch (e.key.code)
{
case sf::Keyboard::Left:
input.moveX = 0;
break;
case sf::Keyboard::Right:
input.moveX = 0;
break;
case sf::Keyboard::Space:
input.spaceBar = 0;
break;
default:
break;
}
break;
default:
break;
}
}
Update();
Render();
}
}
~JumpGame()
{
delete background;
delete playground;
delete player;
delete pickup;
delete sparks;
clearIteratorAndElements(&bombs);
delete gameTextures;
}
template <typename Container>
void clearIteratorAndElements(Container *container)
{
for (typename Container::iterator it = container->begin(); it != container->end(); ++it)
{
delete *it;
}
container->clear();
}
Texture *loadTexture(const char *filename, sf::Color transparencyColor = sf::Color::Transparent)
{
Texture *tex = new Texture();
Image img;
if (img.loadFromFile(filename))
{
img.createMaskFromColor(transparencyColor);
tex->loadFromImage(img);
}
return tex;
}
};
int main()
{
JumpGame app;
app.run();
return 0;
}