-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
108 lines (93 loc) · 2.87 KB
/
main.cpp
File metadata and controls
108 lines (93 loc) · 2.87 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
#include <stdio.h>
#include "SDL.h"
#include "Game.h"
#include "Frog.h"
#include "Obstacle.h"
int main(int argc, char** argv) {
Game* game = new Game();
game->init("Frogger 1.0", false);
Frog* frog = new Frog(5);
Obstacle** trucks = new Obstacle * [20];
Obstacle** logs = new Obstacle * [20];
for (int i = 0; i < 5; i++) {
trucks[i] = new Obstacle(i * 256, 256, 96, 32, -0.3, "images/truck_head_left.bmp");
trucks[i + 5] = new Obstacle(i * 288, 288, 64, 32, 0.7, "images/car_head_right.bmp");
trucks[i + 10] = new Obstacle(i * 288, 320, 64, 32, -0.5, "images/car_head_left.bmp");
trucks[i + 15] = new Obstacle(i * 256, 352, 96, 32, 0.1, "images/truck_head_right.bmp");
logs[i] = new Obstacle(i * 256, 96, 96, 32, -0.1, "images/log_size_3.bmp");
logs[i + 5] = new Obstacle(i * 288, 128, 64, 32, 0.5, "images/turtle_size_2.bmp");
logs[i + 10] = new Obstacle(i * 288, 160, 96, 32, -0.7, "images/log_size_3.bmp");
logs[i + 15] = new Obstacle(i * 256, 192, 64, 32, 0.3, "images/turtle_size_2.bmp");
}
int t1 = SDL_GetTicks();
int t2 = 0;
double delta = 0;
double time = 0;
char text[128];
while (frog->isAlive() && !frog->endGame()) {
// static output
t2 = SDL_GetTicks();
delta = (t2 - t1) * 0.001;
time += delta;
t1 = t2;
game->handleEvents();
game->update(frog->getLives());
sprintf_s(text, "Remaining time: %02.02f", 50 - time);
game->drawText(8, 436, text);
if(time < 40) game->drawRectangle(8, 452, 624 - (time * 12.48), 24, game->green, game->green);
else if(time > 40 && time < 50) game->drawRectangle(8, 456, 624 - (time * 12.48), 16, game->red, game->red);
else {
frog->reset();
time = 0;
}
// movement
for (int i = 0; i < 20; i++) {
trucks[i]->move();
logs[i]->move();
}
frog->move();
// handling collisions
if (frog->onRiver()) {
bool dead = true;
float speed;
for (int i = 0; i < 20; i++) {
if (frog->collision(logs[i])) {
dead = false;
speed = logs[i]->getSpeed();
}
}
if (!dead) frog->move(speed);
else frog->reset();
}
else {
for (int i = 0; i < 20; i++) {
if (frog->collision(trucks[i])) frog->reset();
}
}
frog->touchdown();
// drawing objects
for (int i = 0; i < 20; i++) {
trucks[i]->draw(game->getScreen());
logs[i]->draw(game->getScreen());
}
frog->draw(game->getScreen());
frog->drawBase(game->getScreen());
game->render();
}
bool gameOver = true;
SDL_Event event;
while (gameOver) {
SDL_FillRect(game->getScreen(), NULL, game->black);
game->drawText(100, 100, "GAMEOVER");
game->drawText(100, 120, "Czy zakończyć grę?");
game->drawText(100, 140, "Wciśnij dowolny klawisz!");
while (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN) {
gameOver = false;
}
}
game->render();
}
game->clean();
return 0;
}