-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
322 lines (270 loc) · 7.96 KB
/
main.cpp
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
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
using namespace sf;
using namespace std;
// rect rect collision detection helper function
bool collides(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2) {
if (x1 + w1 >= x2 && x1 <= x2 + w2 && y1 + h1 >= y2 && y1 <= y2 + h2) {
return true;
}
return false;
}
int main() {
// create the window and set general settings. Plant the seeds
RenderWindow window(VideoMode(1000, 600), "Floppy Bird");
window.setFramerateLimit(60);
window.setKeyRepeatEnabled(false);
srand(time(0));
// all sounds and their buffers will rest in this singleton struct
struct Sounds {
SoundBuffer chingBuffer;
SoundBuffer hopBuffer;
SoundBuffer dishkBuffer;
Sound ching;
Sound hop;
Sound dishk;
} sounds;
// load sounds
sounds.chingBuffer.loadFromFile("./audio/score.wav");
sounds.hopBuffer.loadFromFile("./audio/flap.wav");
sounds.dishkBuffer.loadFromFile("./audio/crash.wav");
sounds.ching.setBuffer(sounds.chingBuffer);
sounds.hop.setBuffer(sounds.hopBuffer);
sounds.dishk.setBuffer(sounds.dishkBuffer);
// all textures remain in here. Flappy has 3 textures, which will repeadetly draw, creating the illusion of flying.
struct Textures {
Texture flappy[3];
Texture pipe;
Texture background;
Texture gameover;
} textures;
// load textures
textures.background.loadFromFile("./images/background.png");
textures.pipe.loadFromFile("./images/pipe.png");
textures.gameover.loadFromFile("./images/gameover.png");
textures.flappy[0].loadFromFile("./images/flappy1.png");
textures.flappy[1].loadFromFile("./images/flappy2.png");
textures.flappy[2].loadFromFile("./images/flappy3.png");
// flappy singleton struct.
// v = vertical speed
// frame = which texture to print
struct Flappy {
double v = 0;
int frame = 0;
Sprite sprite;
} flappy;
// initial position, scale
flappy.sprite.setPosition(250, 300);
flappy.sprite.setScale(2, 2);
// resizable array of sprites representing pipes
vector<Sprite> pipes;
// waiting = game not started yet
// started = playing
// gameover = game is over
enum GameState { waiting,
started,
gameover };
// game settings in singleton game struct
// frames counts total frames passed since the beginning of time
struct Game {
int score = 0;
int highscore = 0;
int frames = 0;
GameState gameState = waiting;
Sprite background[3];
Sprite gameover;
Text pressC;
Text scoreText;
Text highscoreText;
Font font;
} game;
// load font, set positions, scales etc
game.font.loadFromFile("./fonts/flappy.ttf");
game.background[0].setTexture(textures.background);
game.background[1].setTexture(textures.background);
game.background[2].setTexture(textures.background);
game.background[0].setScale(1.15625, 1.171875);
game.background[1].setScale(1.15625, 1.171875);
game.background[2].setScale(1.15625, 1.171875);
game.background[1].setPosition(333, 0);
game.background[2].setPosition(666, 0);
game.gameover.setTexture(textures.gameover);
game.gameover.setOrigin(192 / 2, 42 / 2);
game.gameover.setPosition(500, 125);
game.gameover.setScale(2, 2);
game.pressC.setString("Press C to continue");
game.pressC.setFont(game.font);
game.pressC.setFillColor(Color::White);
game.pressC.setCharacterSize(50);
game.pressC.setOrigin(game.pressC.getLocalBounds().width / 2, 0);
game.pressC.setPosition(500, 250);
game.scoreText.setFont(game.font);
game.scoreText.setFillColor(Color::White);
game.scoreText.setCharacterSize(75);
game.scoreText.move(30, 0);
game.highscoreText.setFont(game.font);
game.highscoreText.setFillColor(Color::White);
game.highscoreText.move(30, 80);
// main loop
while (window.isOpen()) {
// update score
flappy.sprite.setTexture(textures.flappy[1]);
game.scoreText.setString(to_string(game.score));
game.highscoreText.setString("HI " + to_string(game.highscore));
// update flappy
float fx = flappy.sprite.getPosition().x;
float fy = flappy.sprite.getPosition().y;
float fw = 34 * flappy.sprite.getScale().x;
float fh = 24 * flappy.sprite.getScale().y;
// flap the wings if playing
if (game.gameState == waiting || game.gameState == started) {
// change the texture once in 6 frames
if (game.frames % 6 == 0) {
flappy.frame += 1;
}
if (flappy.frame == 3) {
flappy.frame = 0;
}
}
flappy.sprite.setTexture(textures.flappy[flappy.frame]);
// move flappy
if (game.gameState == started) {
flappy.sprite.move(0, flappy.v);
flappy.v += 0.5;
}
// if hits ceiling, stop ascending
// if out of screen, game over
if (game.gameState == started) {
if (fy < 0) {
flappy.sprite.setPosition(250, 0);
flappy.v = 0;
} else if (fy > 600) {
flappy.v = 0;
game.gameState = gameover;
sounds.dishk.play();
}
}
// count the score
for (vector<Sprite>::iterator itr = pipes.begin(); itr != pipes.end(); itr++) {
if (game.gameState == started && (*itr).getPosition().x == 250) {
game.score++;
sounds.ching.play();
if (game.score > game.highscore) {
game.highscore = game.score;
}
break;
}
}
// generate pipes
if (game.gameState == started && game.frames % 150 == 0) {
int r = rand() % 275 + 75;
int gap = 150;
// lower pipe
Sprite pipeL;
pipeL.setTexture(textures.pipe);
pipeL.setPosition(1000, r + gap);
pipeL.setScale(2, 2);
// upper pipe
Sprite pipeU;
pipeU.setTexture(textures.pipe);
pipeU.setPosition(1000, r);
pipeU.setScale(2, -2);
// push to the array
pipes.push_back(pipeL);
pipes.push_back(pipeU);
}
// move pipes
if (game.gameState == started) {
for (vector<Sprite>::iterator itr = pipes.begin(); itr != pipes.end(); itr++) {
(*itr).move(-3, 0);
}
}
// remove pipes if offscreen
if (game.frames % 100 == 0) {
vector<Sprite>::iterator startitr = pipes.begin();
vector<Sprite>::iterator enditr = pipes.begin();
for (; enditr != pipes.end(); enditr++) {
if ((*enditr).getPosition().x > -104) {
break;
}
}
pipes.erase(startitr, enditr);
}
// collision detection
if (game.gameState == started) {
for (vector<Sprite>::iterator itr = pipes.begin(); itr != pipes.end(); itr++) {
float px, py, pw, ph;
if ((*itr).getScale().y > 0) {
px = (*itr).getPosition().x;
py = (*itr).getPosition().y;
pw = 52 * (*itr).getScale().x;
ph = 320 * (*itr).getScale().y;
} else {
pw = 52 * (*itr).getScale().x;
ph = -320 * (*itr).getScale().y;
px = (*itr).getPosition().x;
py = (*itr).getPosition().y - ph;
}
if (collides(fx, fy, fw, fh, px, py, pw, ph)) {
game.gameState = gameover;
sounds.dishk.play();
}
}
}
// events
Event event;
while (window.pollEvent(event)) {
// bye bye
if (event.type == Event::Closed) {
window.close();
}
// flap
else if (event.type == Event::KeyPressed &&
event.key.code == Keyboard::Space) {
if (game.gameState == waiting) {
game.gameState = started;
}
if (game.gameState == started) {
flappy.v = -8;
sounds.hop.play();
}
// restart
} else if (event.type == Event::KeyPressed &&
event.key.code == Keyboard::C &&
game.gameState == gameover) {
game.gameState = waiting;
flappy.sprite.setPosition(250, 300);
game.score = 0;
pipes.clear();
}
}
// clear, draw, display
window.clear();
window.draw(game.background[0]);
window.draw(game.background[1]);
window.draw(game.background[2]);
window.draw(flappy.sprite);
// draw pipes
for (vector<Sprite>::iterator itr = pipes.begin(); itr != pipes.end(); itr++) {
window.draw(*itr);
}
// draw scores
window.draw(game.scoreText);
window.draw(game.highscoreText);
// gameover. press c to continue
if (game.gameState == gameover) {
window.draw(game.gameover);
if (game.frames % 60 < 30) {
window.draw(game.pressC);
}
}
window.display();
// dont forget to update total frames
game.frames++;
}
return 0;
}