-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGame.cpp
More file actions
452 lines (361 loc) · 10.4 KB
/
Copy pathGame.cpp
File metadata and controls
452 lines (361 loc) · 10.4 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
#include "Game.h"
using namespace std;
using namespace sf;
RenderWindow window;
Game::Game(void)
:fullScrean(false)
{
srand(time(0));
window.create(VideoMode(1280, 720), "Boston Explorer", Style::Fullscreen);
state = END;
if (!font.loadFromFile("Data/GoodDog.otf"))
{
MessageBox(NULL, "Font not found !", "ERROR", NULL);
return;
}
audio.loadMusic(true);
audio.loadSounds();
audio.playMusic();
state = MENU;
}
Game::~Game()
{
audio.stopMusic();
}
//load appropriate state (MENU, GAME, GAME_OVER, END...)
void Game::runGame(){
Staty::GameStatistics gameStatistics;
for (size_t i = 0; i < gameStatistics.howmanylevels; i++)
{
gameStatistics.tabWithRecords[i].SetLinie(i);
gameStatistics.tabWithRecords[i].loading();
}
for (size_t i = 0; i < gameStatistics.howmanylevels; i++)
{
gameStatistics.ifRecord[i] = false;
}
for (size_t i = 0; i < 3; i++)
{
gameStatistics.ifGameRecord[0] = false;
}
window.setFramerateLimit(65);
while (state != END)
{
switch (state)
{
case Game::MENU:
if (!gameStatistics.flagsoundingame)
{
audio.playMusic();
}
gameStatistics.audio2.stopMusic();
menu();
break;
case Game::GAME:
audio.stopMusic();
single(gameStatistics);
break;
case Game::GAME_OVER:
game_over(gameStatistics);
break;
case Game::ABOUT:
about();
break;
case Game::END:
break;
default:
break;
}
}
}
//handling state - MENU
void Game::menu(){
Text title("Boston Explorer", font, 122);
title.setStyle(Text::Bold);
title.setPosition(1280 / 2 - title.getGlobalBounds().width / 2, 20);
const int how = 4;
Text textMenu[how];
string str[] = { "Play", "Exit" , "Records", "About" };
for (size_t i = 0; i < how; i++)
{
if (i < 2)
{
textMenu[i].setFont(font);
textMenu[i].setCharacterSize(65);
if (i == 0) textMenu[i].setCharacterSize(85);
textMenu[i].setString(str[i]);
textMenu[i].setPosition(1280 / 2 - textMenu[i].getGlobalBounds().width / 2, 250 + i * 320);
}
else{
textMenu[i].setFont(font);
textMenu[i].setCharacterSize(33);
textMenu[i].setString(str[i]);
textMenu[i].setPosition(1280 / 2 - textMenu[i].getGlobalBounds().width / 2, 300 + i * 55);
}
}
while (state == MENU)
{
Vector2f mouse(Mouse::getPosition());
Event event;
while (window.pollEvent(event))
{
// ESC
if (event.type == Event::Closed || event.type == Event::KeyPressed && event.key.code == Keyboard::Escape)
{
state = END;
}
//Exit
else if (textMenu[1].getGlobalBounds().contains(mouse) && event.type == Event::MouseButtonReleased && event.key.code == Mouse::Left)
{
state = END;
}
//Game
else if (textMenu[0].getGlobalBounds().contains(mouse) && event.type == Event::MouseButtonReleased && event.key.code == Mouse::Left || event.type == Event::KeyPressed && event.key.code == Keyboard::Space)
{
state = GAME;
}
//Records
else if (textMenu[2].getGlobalBounds().contains(mouse) && event.type == Event::MouseButtonReleased && event.key.code == Mouse::Left || event.type == Event::KeyPressed && event.key.code == Keyboard::X)
{
state = GAME_OVER;
}
//About
else if (textMenu[3].getGlobalBounds().contains(mouse) && event.type == Event::MouseButtonReleased && event.key.code == Mouse::Left)
{
state = ABOUT;
}
}
//color after you hover over the button
for (size_t i = 0; i < how; i++)
{
if (textMenu[i].getGlobalBounds().contains(mouse))
{
textMenu[i].setColor(Color::Cyan);
}
else
{
textMenu[i].setColor(Color::White);
}
}
window.clear();
window.draw(title);
for (size_t i = 0; i < how; i++)
{
window.draw(textMenu[i]);
}
window.display();
}
}
//handling state - GAME (run engine)
void Game::single(Staty::GameStatistics &gameStatistics){
Engine engine(window, gameStatistics);
if (engine.GameState2 == Engine::MENU_AGAIN )
{
state = MENU;
}
if (engine.GameState2 == Engine::NEXT_LEVEL)
{
state = GAME;
gameStatistics.nrLevel++;
if (gameStatistics.nrLevel >= 29) gameStatistics.nrLevel = 15;
}
if (engine.GameState2 == Engine::LEVEL_AGAIN)
{
state = GAME;
}
if (engine.GameState2 == Engine::END)
{
state = END;
}
if (engine.GameState2 == Engine::GAME_OVER)
{
state = GAME_OVER;
}
}
//handling state - GAME_OVER (Records)
void Game::game_over(Staty::GameStatistics &gameStatistics)
{
Text keyCode("Esc - Menu Space - New Game F4 - End", font, 26);
Text f1("F1 - Fullscreen / Window Mode", font, 26);
keyCode.setPosition(888, 680);
f1.setPosition(37, 680);
//Game Records
Text game_records("Game Records", font, 67);
game_records.setStyle(Text::Bold);
game_records.setPosition(1280 / 2 - game_records.getGlobalBounds().width / 2, 0);
Text level_records("Levels Records", font, 55);
level_records.setStyle(Text::Bold);
level_records.setPosition(1280 / 2 - level_records.getGlobalBounds().width / 2, 170);
const int how_gr = 3;
Text tab_rec[how_gr];
string str[] = { to_string(gameStatistics.tabWithRecords[0].firstplace), to_string(gameStatistics.tabWithRecords[0].secondplace), to_string(gameStatistics.tabWithRecords[0].thirdplace) };
string addNewRecord = "New Record !";
while (state == GAME_OVER)
{
window.clear();
Vector2f mouse(Mouse::getPosition());
Event event;
for (size_t i = 0; i < how_gr; i++)
{
tab_rec[i].setFont(font);
tab_rec[i].setCharacterSize(40);
tab_rec[i].setString(to_string(i + 1) + " )\t\t" + str[i]);
tab_rec[i].setPosition(1280 / 2 - tab_rec[i].getGlobalBounds().width / 2, 60 + i * 25);
if (gameStatistics.ifGameRecord[i] == true)
{
Text addNewGameRecord;
addNewGameRecord.setFont(font);
addNewGameRecord.setCharacterSize(40);
addNewGameRecord.setString(addNewRecord);
addNewGameRecord.setColor(Color::Red);
addNewGameRecord.setPosition(1280 / 2 + tab_rec[i].getGlobalBounds().width / 2 + 50, 60 + i * 25);
window.draw(addNewGameRecord);
}
if (tab_rec[i].getGlobalBounds().contains(mouse)) tab_rec[i].setColor(Color::Green);
else{ tab_rec[i].setColor(Color::White); };
}
//Levels Records
int to_setPos = 0;
for (size_t i = 0; i < gameStatistics.howmanylevels; i++)
{
Text str;
str.setFont(font);
str.setCharacterSize(23);
if (gameStatistics.ifRecord[i] == true)
{
str.setString(to_string(gameStatistics.tabWithRecords[i].nr_level) + " )\t\t" + gameStatistics.tabWithRecords[i].date
+ "\t\t" + to_string(gameStatistics.tabWithRecords[i].nr_points) + "\t\t\t\t" + addNewRecord);
}
else
{
str.setString(to_string(gameStatistics.tabWithRecords[i].nr_level) + " )\t\t" + gameStatistics.tabWithRecords[i].date
+ "\t\t" + to_string(gameStatistics.tabWithRecords[i].nr_points));
}
str.setPosition(1280 / 2 - 280 / 2, 250 + i * 13 + to_setPos);
if (!((i + 1) % 5)) to_setPos += 10;
if (str.getGlobalBounds().contains(mouse)) str.setColor(Color::Red);
window.draw(str);
}
//handling keys
while (window.pollEvent(event))
{
if (event.type == Event::KeyReleased && event.key.code == Keyboard::Escape)
{
for (size_t i = 0; i < gameStatistics.howmanylevels; i++)
{
gameStatistics.ifRecord[i] = false;
if (i < 3) gameStatistics.ifGameRecord[i] = false;
}
state = MENU;
}
if (event.type == Event::KeyPressed && event.key.code == Keyboard::F4)
{
state = END;
}
if (event.type == Event::KeyPressed && event.key.code == Keyboard::F1)
{
window.close();
if (fullScrean)
window.create(VideoMode(1280, 720), "Boston Explorer", Style::Fullscreen);
if (!fullScrean)
window.create(VideoMode(1280, 720), "Boston Explorer", Style::Close);
window.setFramerateLimit(65);
fullScrean = !fullScrean;
}
if (event.type == Event::KeyPressed && event.key.code == Keyboard::Space)
{
for (size_t i = 0; i < gameStatistics.howmanylevels; i++)
{
gameStatistics.ifRecord[i] = false;
if (i < 3) gameStatistics.ifGameRecord[i] = false;
}
state = GAME;
}
}//pollEvent
window.draw(game_records);
window.draw(level_records);
for (size_t i = 0; i < how_gr; i++)
{
window.draw(tab_rec[i]);
}
window.draw(keyCode);
window.draw(f1);
window.display();
}//while
}
//handling state - ABOUT
void Game::about(){
window.clear();
Text keyCode("Esc - Menu Space - New Game F4 - End", font, 26);
Text f1("F1 - Fullscreen / Window Mode", font, 26);
keyCode.setPosition(888, 680);
f1.setPosition(37, 680);
Text back_button("BACK", font, 77);
back_button.setPosition(1280/2 - back_button.getGlobalBounds().width/2, 620);
const int how = 12;
Text aboutText[how];
string str[] = { "Boston Explorer - Simple SFML Game",
"Copyright (C) 2015 Dominik \"b00sti\" Pawlik <pawlik.dominik@gmail.com>",
"",
"\t\tRules of the game:",
"1.\t W - move towards mouse.",
"2. \tYou have to collect items.",
"3. \tIf your power bar will be empty you will lose one life.",
"4. \tTo go to the next level you need to collect the right amount of items with the highest value.",
"5. \tTo new life you need to collect 50 000 points.",
"6. \tBreak the records !",
"",
"\t \tGood Luck !"
};
for (size_t i = 0; i < how; i++)
{
aboutText[i].setFont(font);
aboutText[i].setCharacterSize(33);
aboutText[i].setString(str[i]);
aboutText[i].setPosition(200, 100 + i * 40);
}
while (state == ABOUT)
{
window.clear();
Vector2f mouse(Mouse::getPosition());
Event event;
if (back_button.getGlobalBounds().contains(mouse)) back_button.setColor(Color::Cyan);
while (window.pollEvent(event))
{
if (event.type == Event::KeyReleased && event.key.code == Keyboard::Escape)
{
state = MENU;
}
if (event.type == Event::KeyPressed && event.key.code == Keyboard::F4)
{
state = END;
}
if (event.type == Event::KeyPressed && event.key.code == Keyboard::F1)
{
window.close();
if (fullScrean)
window.create(VideoMode(1280, 720), "Boston Explorer", Style::Fullscreen);
if (!fullScrean)
window.create(VideoMode(1280, 720), "Boston Explorer", Style::Close);
window.setFramerateLimit(65);
fullScrean = !fullScrean;
}
if (event.type == Event::KeyPressed && event.key.code == Keyboard::Space)
{
state = GAME;
}
if (back_button.getGlobalBounds().contains(mouse) && event.type == Event::MouseButtonReleased && event.key.code == Mouse::Left)
{
state = MENU;
}
}//pollEvent
for (size_t i = 0; i < how; i++)
{
window.draw(aboutText[i]);
}
window.draw(keyCode);
window.draw(f1);
window.draw(back_button);
window.display();
}
}