-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
389 lines (346 loc) · 10.6 KB
/
Copy pathgame.cpp
File metadata and controls
389 lines (346 loc) · 10.6 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
#include "game.h"
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include "common.h"
//#define DEBUG_TEST 1
Game::Game()
:width(CELL*XCELLS)
,height(CELL*YCELLS)
,lvl(0)
,gameWon(false)
,speedCounter(60)
,resetSpeed(60)
,originalSpeed(60)
,score(0)
,highScore(0)
,dir(LEFT)
,gameOver(false) {
}
Game::~Game() {
for (int i = 0; i < enemies.size(); i++) {
delete enemies[i];
}
for (int j = 0; j < barriers.size(); j++) {
delete barriers[j];
}
for (int k = 0; k < bullets.size(); k++) {
delete bullets[k];
}
}
void Game::Init(sf::RenderWindow& window){
if (!font.loadFromFile("font.ttf")) {
std::cout<<"Font not loaded"<<std::endl;
}
if (!music1.openFromFile("siclone_death.ogg")) { // Death Sound
std::cout<<"Sound not loaded"<<std::endl;
}
if (!music2.openFromFile("siclone_extra_life.ogg")) { // Win Sound
std::cout<<"Sound not loaded"<<std::endl;
}
if (!music3.openFromFile("siclone_shoot.ogg")) { // Shoot Sound
std::cout<<"Sound not loaded"<<std::endl;
}
if (!music4.openFromFile("siclone_shoot_enemy.ogg")) { // Enemy Shoot Sound
std::cout<<"Sound not loaded"<<std::endl;
}
if (!music5.openFromFile("siclone_shoot_enemy.ogg")) { // Explosion Sound
std::cout<<"Sound not loaded"<<std::endl;
}
if (!music6.openFromFile("siclone_shoot_enemy.ogg")) { // Small Explosion Sound
std::cout<<"Sound not loaded"<<std::endl;
}
for (int xSpaces = 0; xSpaces < 2*XCELLS/3; xSpaces++) {
for (int ySpaces = 0; ySpaces < 4; ySpaces++) {
Enemy* enemy = new Enemy(CELL*(xSpaces + 1), CELL + ySpaces*CELL/2, 5 - ySpaces);
enemies.push_back(enemy);
}
}
cannon.Init();
BuildLvl();
}
void Game::Update(int paddleDir, sf::RenderWindow& window) {
if (gameOver || gameWon) {
GameOverState();
return;
}
resetSpeed--;
if (resetSpeed%10 == 0) {
for (auto bullet: bullets) {
bullet->Move(UP);
}
for (auto bullet: eBullets) {
bullet->Move(DOWN);
}
}
CheckCollision();
CannonCollision();
for (int b = 0; b < bullets.size(); b++) {
if (bullets[b]->GetY() == 0) {
DeleteBullet(b);
}
}
for (int b = 0; b < eBullets.size(); b++) {
if (eBullets[b]->GetY() == YCELLS*CELL) {
DeleteEBullet(b);
}
}
if (eBullets.empty()) {
int maxY = 0;
int maxX = enemies[enemies.size()/2] -> GetX();
for (int i = 0; i < enemies.size(); i++) {
if (enemies[i]->GetY() > maxY) {
maxY = enemies[i]->GetY();
}
}
Bullet* bullet = new Bullet(maxX, maxY, 1);
eBullets.push_back(bullet);
if (!gameOver and !gameWon) {
music4.play();
}
}
else {
BarrierCollision();
}
if (resetSpeed == 0) {
if (!enemies.empty()) {
if (enemies[0]->GetX() == 0) {
if (dir == LEFT) {
dir = DOWN;
}
else {
dir = RIGHT;
}
}
else if (enemies[enemies.size() - 1]->GetX() == (XCELLS - 1)*CELL) {
if (dir == RIGHT) {
dir = DOWN;
}
else {
dir = LEFT;
}
}
}
for (int i = 0; i < enemies.size(); i++) {
enemies[i]->Move(dir);
if (enemies[i]->GetY() >= CELL*YCELLS) {
DeleteEnemy(i);
gameOver = true;
}
}
resetSpeed = speedCounter;
}
cannon.Move(paddleDir, window);
}
void Game::Render(sf::RenderWindow& window) {
sf::Color colour1 (93, 74, 128);
sf::Color colour2 (64, 47, 94);
sf::Color colour3 (137, 129, 153);
sf::Vertex backgroundgr[] =
{
sf::Vertex(sf::Vector2f(0, 0), colour2),
sf::Vertex(sf::Vector2f(0, CELL*(YCELLS+1)), colour3),
sf::Vertex(sf::Vector2f(CELL*XCELLS, CELL*(YCELLS+1)), colour3),
sf::Vertex(sf::Vector2f(CELL*XCELLS, 0), colour2)
};
window.draw(backgroundgr, 4, sf::Quads);
cannon.Draw(window);
for (auto barrier: barriers) {
barrier->Draw(window);
}
for (auto enemy: enemies) {
enemy->Draw(window);
}
for (auto bullet: bullets) {
bullet->Draw(window);
}
for (auto bullet: eBullets) {
bullet->Draw(window);
}
if(gameWon) {
if (resetSpeed == 10) {
text.setFont(font); // font is a sf::Font
text.setString("FINAL LEVEL COMPLETE!");
text.setCharacterSize(40); // in pixels, not points!
text.setFillColor(sf::Color::Green);
text.setStyle(sf::Text::Bold | sf::Text::Underlined);
text.setPosition(width/8 + CELL/4, height/3);
window.draw(text);
}
else {
text.setFont(font); // font is a sf::Font
text.setString("LEVEL COMPLETE!");
text.setCharacterSize(40); // in pixels, not points!
text.setFillColor(sf::Color::Green);
text.setStyle(sf::Text::Bold | sf::Text::Underlined);
text.setPosition(width/8 + CELL/4, height/3);
window.draw(text);
text.setString("Press SPACE to Continue");
text.setCharacterSize(25); // in pixels, not points!
text.setFillColor(sf::Color::Green);
text.setPosition(width/8 + CELL/4, height/2 + 64);
window.draw(text);
}
}
else if (IsGameOver()) {
text.setFont(font); // font is a sf::Font
text.setString("GAME OVER");
text.setCharacterSize(64); // in pixels, not points!
text.setFillColor(sf::Color::Green);
text.setStyle(sf::Text::Bold | sf::Text::Underlined);
text.setPosition(width/6, height/3);
window.draw(text);
text.setString("Press SPACE to Play Again");
text.setCharacterSize(25); // in pixels, not points!
text.setFillColor(sf::Color::Green);
text.setStyle(sf::Text::Underlined);
text.setPosition(width/8 + CELL/4, height/2 + CELL);
window.draw(text);
std::string scoreString;
std::string tempString;
scoreString = "Your Score: ";
tempString = std::to_string(score);
scoreString = scoreString + tempString + "; High Score: ";
tempString = std::to_string(highScore);
scoreString = scoreString + tempString;
text.setString(scoreString);
text.setCharacterSize(12); // in pixels, not points!
text.setFillColor(sf::Color::Green);
text.setStyle(sf::Text::Underlined);
text.setPosition(width/4, height/2 + 2*CELL);
window.draw(text);
}
}
void Game::NewBullet() {
int bulletX = cannon.GetX() + 2*CELL/3 + 10;
int bulletY = cannon.GetY() - CELL/4;
Bullet* bullet = new Bullet(bulletX, bulletY, 0);
bullets.push_back(bullet);
if (!gameOver || !gameWon){
music3.play();
}
}
void Game::BuildLvl() {
#ifdef DEBUG_TEST
for (int brickX = 0; brickX < 3; brickX++) {
for (int brickY = 0; brickY < 1; brickY++) {
brick.Init(CELL*brickX, CELL*brickY/2, brickY);
bricks.push_back(brick);
}
}
return;
#endif
int barrierX = CELL;
for (int i = 0; i < 6; i++) {
Barrier* barrier = new Barrier(barrierX, CELL*7);
barriers.push_back(barrier);
barrierX = barrierX + 2*CELL;
}
}
bool Game::IsGameOver() {
return gameOver;
}
void Game::Reset(sf::RenderWindow& window) {
if (gameWon) {
speedCounter = speedCounter - 10;
if (speedCounter == 0) {
speedCounter = originalSpeed;
score = 0;
}
}
else {
speedCounter = originalSpeed;
score = 0;
}
gameOver = false;
gameWon = false;
dir = LEFT;
barriers.clear();
Init(window);
resetSpeed = speedCounter;
}
void Game::CheckCollision() {
for (int b = 0; b < bullets.size(); b++) {
int bulletX = bullets[b]->GetX();
int bulletY = bullets[b]->GetY();
for (int e = 0; e < enemies.size(); e++) {
int enemyX = enemies[e]->GetX();
int enemyY = enemies[e]->GetY();
if (((bulletX >= enemyX) and (bulletX <= (enemyX + CELL/2))) and ((bulletY >= enemyY) and (bulletY <= (enemyY + CELL/2)))) {
score = score + enemies[e] -> GetPoints();
if (!gameOver || !gameWon){
music6.play();
}
DeleteEnemy(e);
DeleteBullet(b);
}
}
if (enemies.empty()) {
gameWon = true;
}
}
}
void Game::DeleteEnemy(int e) {
delete enemies[e];
enemies.erase(e + enemies.begin(), e + enemies.begin() + 1);
}
void Game::DeleteBullet(int b) {
delete bullets[b];
bullets.erase(b + bullets.begin(), b + bullets.begin() + 1);
}
void Game::DeleteEBullet(int b) {
delete eBullets[b];
eBullets.erase(b + eBullets.begin(), b + eBullets.begin() + 1);
}
void Game::DeleteBarrier(int b) {
delete barriers[b];
barriers.erase(b + barriers.begin(), b + barriers.begin() + 1);
}
void Game::BarrierCollision() {
for (int e = 0; e < eBullets.size(); e++) {
int eBulX = eBullets[e] -> GetX();
int eBulY = eBullets[e] -> GetY();
for (int i = 0; i < barriers.size(); i++) {
if (barriers[i] -> Hit(eBulX, eBulY)) {
barriers[i] -> Damage();
DeleteEBullet(e);
if (barriers[i] -> GetHits() == 0) {
if (!gameOver || !gameWon){
music5.play();
}
DeleteBarrier(i);
}
}
}
}
}
void Game::CannonCollision() {
for (int e = 0; e < eBullets.size(); e++) {
int eBulX = eBullets[e] -> GetX();
int eBulY = eBullets[e] -> GetY();
if (cannon.Hit(eBulX, eBulY)) {
music1.play();
gameOver = true;
}
}
}
void Game::GameOverState() {
if (gameOver) {
gameWon = false;
}
else {
gameOver = false;
}
for (int i = 0; i < enemies.size(); i++) {
DeleteEnemy(i);
}
for (int i = 0; i < bullets.size(); i++) {
DeleteBullet(i);
}
for (int i = 0; i < eBullets.size(); i++) {
DeleteEBullet(i);
}
if (score > highScore) {
highScore = score;
}
}