-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectfinal.cpp
More file actions
764 lines (628 loc) · 22.1 KB
/
Copy pathprojectfinal.cpp
File metadata and controls
764 lines (628 loc) · 22.1 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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/audio.hpp>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
using namespace sf;
class Bullet
{
public:
CircleShape shape;
Vector2f currVelocity;
float maxspeed;
Bullet(float radius = 5.f) : currVelocity(0.f, 0.f), maxspeed(20.f)
{
this->shape.setRadius(radius);
this->shape.setFillColor(Color::Red);
};
};
//constant variables for different menu
const int START_MENU = 0;
const int GAMEPLAY = 1;
const int CONTROLS = 2;
const int GAMEOVER = 3;
const int HighScores = 4;
//health of the player
int pHp = 10;
//function for start menu
static void drawStartMenu(RenderWindow& window, Sprite& MBG)
{
window.clear(Color(54, 69, 79));
Font font;
if (!font.loadFromFile("Vogue.ttf"))
{
cerr << "Failed to load font!" << endl;
return;
}
Text title("360° Monster Shooter", font, 60);
title.setPosition(650, 125);
Text startText("Start --- Enter", font, 30);
startText.setPosition(850, 300);
Text controlsText("Controls --- Ctrl", font, 30);
controlsText.setPosition(850, 400);
Text scoreCal("Highscores --- H", font, 30);
scoreCal.setPosition(850, 500);
Text Exit("Exit --- Alt", font, 30);
Exit.setPosition(850, 600);
window.draw(MBG);
window.draw(title);
window.draw(startText);
window.draw(controlsText);
window.draw(scoreCal);
window.draw(Exit);
}
struct HighPoints {
string name;
int score;
};
// Function to load highscores from a file
static vector<HighPoints> loadHighscores()
{
vector<HighPoints> highscores;
ifstream file("highscores.txt");
if (file.is_open())
{
string name;
int score;
while (file >> name >> score) {
highscores.push_back({ name, score });
}
file.close();
}
return highscores;
}
// Function to save highscores to a file
static void saveHighscores(const vector<HighPoints>& highscores)
{
ofstream file("highscores.txt");
if (file.is_open())
{
for (const auto& hs : highscores) {
file << hs.name << " " << hs.score << endl;
}
file.close();
}
}
// Function to add a new highscore
static void addHighscore(vector<HighPoints>& highscores, const string& playerName, int playerScore)
{
highscores.push_back({ playerName, playerScore });
//lambda function for adding to the file
sort(highscores.begin(), highscores.end(), [](const HighPoints& a, const HighPoints& b) {
return a.score > b.score;
});
if (highscores.size() > 10) {
highscores.resize(10);
}
}
static void drawHighScores(RenderWindow& window, Sprite& MBG, const vector<HighPoints>& highscores)
{
window.clear(Color(54, 69, 79));
Font font;
if (!font.loadFromFile("Vogue.ttf"))
{
cerr << "Failed to load font!" << endl;
return;
}
Font f;
if (!font.loadFromFile("arial.ttf"))
{
cerr << "Failed to load font!" << endl;
return;
}
Text high("HIGHSCORES", font, 50);
high.setPosition(750, 100);
float yPos = 250;
window.draw(MBG);
for (int i = 0; i < highscores.size(); i++)
{
string s = highscores[i].name;
string b = to_string(highscores[i].score);
string total = s + " : " + b;
Text text1(total, font, 30);
text1.setPosition(820, yPos);
yPos += 50;
window.draw(text1);
}
Text backButton("Back ... ESC", font, 30);
backButton.setPosition(820, 800);
window.draw(high);
window.draw(backButton);
}
//function for game over menu
static void drawGameOver(RenderWindow& window, int& score, int& level, Sprite& GBG)
{
window.clear(Color(54, 69, 79));
Font font;
if (!font.loadFromFile("Vogue.ttf")) {
cerr << "Failed to load font!" << endl;
return;
}
Text title("GAME OVER", font, 100);
title.setPosition(700, 350);
Text points("Level : " + to_string(level), font, 30);
points.setPosition(870, 500);
Text points2("Kills : " + to_string(score), font, 30);
points2.setPosition(870, 550);
Text over("Press Esc for Main menu", font, 30);
over.setPosition(750, 600);
window.draw(GBG);
window.draw(title);
window.draw(over);
window.draw(points);
window.draw(points2);
}
//function for control menu
static void drawControlsMenu(RenderWindow& window, Sprite& MBG) {
window.clear(Color(54, 69, 79));
Font font;
if (!font.loadFromFile("Vogue.ttf")) {
cerr << "Failed to load font!" << endl;
return;
}
Text title("CONTROLS", font, 60);
title.setPosition(800, 150);
Text backButton("Back ... ESC", font, 30);
backButton.setPosition(850, 750);
Text Controls1("W ... Move up", font, 30);
Controls1.setPosition(850, 250);
Text Controls2("S ... Move Down", font, 30);
Controls2.setPosition(850, 350);
Text Controls3("A ... Move Left", font, 30);
Controls3.setPosition(850, 450);
Text Controls4("D ... Move Right", font, 30);
Controls4.setPosition(850, 550);
Text MouseControl("Left click ... Shoot", font, 30);
MouseControl.setPosition(850, 650);
window.draw(MBG);
window.draw(title);
window.draw(backButton);
window.draw(Controls1);
window.draw(Controls2);
window.draw(Controls3);
window.draw(Controls4);
window.draw(MouseControl);
}
//function for reset the all variables of the game
static void resetGame(int& gameState, vector<Bullet>& bullets, vector<RectangleShape>& enemies)
{
gameState = GAMEOVER;
pHp = 10;
bullets.clear();
enemies.clear();
}
int main()
{
srand(time(NULL)); // Initialising random function
//To render a window in output with the name of "360° Monster Shooter"
RenderWindow window(VideoMode(1920, 1080), "360° Monster Shooter", Style::Fullscreen);
window.setFramerateLimit(60);
int gameState = START_MENU;
//Background
Texture BackG;
Sprite BG;
if (!BackG.loadFromFile("Background.jpg"))
throw "Could not load the texture!";
BG.setTexture(BackG);
//Main Menu Background
Texture MenuG;
Sprite MBG;
if (!MenuG.loadFromFile("BackMenu.png"))
throw "Could not load the texture!";
MBG.setTexture(MenuG);
//Gameover Background
Texture GameO;
Sprite GBG;
if (!GameO.loadFromFile("GameOver.png"))
throw "Could not load the texture!";
GBG.setTexture(GameO);
//Player
CircleShape player(25.f);
player.setFillColor(Color::White);
//Bullets
Bullet b1;
vector<Bullet> bullets;
//Enemy
RectangleShape enemy;
enemy.setFillColor(Color::Magenta);
enemy.setSize(Vector2f(30.f, 30.f));
int EnemySpawnTimer = 35;
vector<RectangleShape> enemies;
//Hp bar
RectangleShape hpBar;
hpBar.setFillColor(Color(255, 140, 0));
hpBar.setSize(Vector2f(200, 20));
hpBar.setPosition(window.getSize().x - 250.f, window.getSize().y - 50.f);
//Vectors
Vector2f PlayerCenter;
Vector2f MousePosWindow;
Vector2f aimDir;
Vector2f aimDirNorm;
Vector2f enemyDir;
Vector2f enemyDirNorm;
int shootTimer = 0;
int killed = 0;
int stime = 35;
int pause = -1;
float spdMultiplier = 2.f;
//Music
Music music;
if (!music.openFromFile("sound.mp3"))
return -1; // error
music.setVolume(50.f);
music.play();
Music gameOn;
if (!gameOn.openFromFile("gameover.wav"))
return -1; // error
gameOn.setVolume(100.f);
Music beat;
if (!beat.openFromFile("musically.mp3"))
return -1; // error
beat.setVolume(100.f);
//Scores
Font f;
f.loadFromFile("Vogue.ttf");
int score = 0;
Text scoreboard;
scoreboard.setFont(f);
scoreboard.setCharacterSize(40);
scoreboard.setString("KILLS: " + to_string(score));
scoreboard.setPosition(Vector2f(window.getSize().x / 2 - 180.f, 10.f));
//levels
int level = 1;
Text difficulty;
difficulty.setFont(f);
difficulty.setCharacterSize(40);
difficulty.setString("LEVEL: " + to_string(level));
difficulty.setPosition(scoreboard.getPosition().x + 200.f, 10.f);
//health
Text health;
health.setFont(f);
health.setCharacterSize(40);
health.setString("HEALTH");
health.setPosition(hpBar.getPosition().x + 30.f, hpBar.getPosition().y - 50.f);
//to store all the highscores
vector<HighPoints> highscores = loadHighscores();
//to store name of the player
string playerName;
bool nameEntered = false;
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
if (event.key.code == Keyboard::LAlt || event.key.code == Keyboard::RAlt)
window.close();
if (event.type == Event::KeyPressed)
{
if (event.key.code == Keyboard::Escape && gameState == GAMEOVER)
{
gameOn.stop();
beat.stop();
addHighscore(highscores, playerName, score);
saveHighscores(highscores);
gameState = START_MENU;
score = 0;
level = 1;
enemies.clear();
music.play();
}
if (event.key.code == Keyboard::Escape && gameState != START_MENU)
{
gameState = START_MENU;
gameOn.pause();
beat.pause();
}
if (gameState == START_MENU && event.key.code == Keyboard::Enter)
{
music.stop();
gameOn.stop();
gameState = GAMEPLAY;
beat.play();
}
if (gameState == START_MENU && (event.key.code == Keyboard::LControl || event.key.code == Keyboard::RControl))
{
gameOn.stop();
gameState = CONTROLS;
}
if (gameState == START_MENU && event.key.code == Keyboard::H)
{
gameState = HighScores;
}
/* if (gameState == GAMEOVER && event.key.code == Keyboard::Escape)
resetGame(gameState, bullets, enemies);*/
}
}
if (gameState == START_MENU)
{
drawStartMenu(window, MBG);
}
else if (gameState == GAMEPLAY)
{
// to update the kills and the levels
scoreboard.setString("KILLS: " + to_string(score));
difficulty.setString("LEVEL: " + to_string(level));
if (shootTimer < 10)
{
shootTimer++;
}
//Vectors:
PlayerCenter = Vector2f(player.getPosition().x + player.getRadius(), player.getPosition().y + player.getRadius()); // Use assignment operator '=' instead of '=='
MousePosWindow = Vector2f(Mouse::getPosition(window));
aimDir = MousePosWindow - PlayerCenter; // Basic vector math
float aimDirLength = sqrt(pow(aimDir.x, 2) + pow(aimDir.y, 2)); // Magnitude
if (aimDirLength != 0)
{
aimDirNorm = aimDir / aimDirLength;
}
else
{
aimDirNorm = Vector2f(0.f, 0.f); // Set aimDirNorm to zero vector if aimDir length is zero
}
//Player
//defining the boundries & movements of the player
if ((Keyboard::isKeyPressed(Keyboard::A)) && (player.getPosition().x > 0))
{
player.move(-10.f, 0);
}
else if (Keyboard::isKeyPressed(Keyboard::D) && (player.getPosition().x < (window.getSize().x - 2 * player.getRadius())))
{
player.move(10.f, 0);
}
else if ((Keyboard::isKeyPressed(Keyboard::W)) && (player.getPosition().y > 0))
{
player.move(0, -10.f);
}
else if (Keyboard::isKeyPressed(Keyboard::S) && (player.getPosition().y < (window.getSize().y - 2 * player.getRadius())))
{
player.move(0.f, 10.f);
}
//Shooting
if (Mouse::isButtonPressed(Mouse::Left) && shootTimer >= 5)
{
b1.shape.setPosition(PlayerCenter);
b1.currVelocity = aimDirNorm * b1.maxspeed;
bullets.push_back(Bullet(b1));
shootTimer = 0;
}
for (size_t i = 0; i < bullets.size(); i++)
{
bullets[i].shape.move(bullets[i].currVelocity);
if (bullets[i].shape.getPosition().x < 0 || bullets[i].shape.getPosition().x > window.getSize().x || bullets[i].shape.getPosition().y < 0 || bullets[i].shape.getPosition().y > window.getSize().y)
{
bullets.erase(bullets.begin() + i);
}
}
// Update enemies
for (size_t i = 0; i < enemies.size(); i++)
{
// Calculate direction towards the player
enemyDir = PlayerCenter - enemies[i].getPosition();
float enemyDirLength = sqrt(pow(enemyDir.x, 2) + pow(enemyDir.y, 2)); // Magnitude
if (enemyDirLength != 0)
{
enemyDirNorm = enemyDir / enemyDirLength;
}
else
{
enemyDirNorm = Vector2f(0.f, 0.f);
}
// Move the enemy towards the player
enemies[i].move(enemyDirNorm * spdMultiplier); // Adjust the multiplier for speed
}
//Spawn enemies
if (EnemySpawnTimer < stime)
{
EnemySpawnTimer++;
}
else if (EnemySpawnTimer >= stime)
{
// Determine the edge from which to spawn the enemy
int edge = rand() % 4; // 0: top, 1: right, 2: bottom, 3: left
int spawnX, spawnY;
switch (edge)
{
case 0: // Top edge
spawnX = rand() % int(window.getSize().x - enemy.getSize().x);
spawnY = 0 - enemy.getSize().y; // Spawn above the window
break;
case 1: // Right edge
spawnX = window.getSize().x;
spawnY = rand() % int(window.getSize().y - enemy.getSize().y);
break;
case 2: // Bottom edge
spawnX = rand() % int(window.getSize().x - enemy.getSize().x);
spawnY = window.getSize().y;
break;
case 3: // Left edge
spawnX = 0 - enemy.getSize().x; // Spawn to the left of the window
spawnY = rand() % int(window.getSize().y - enemy.getSize().y);
break;
}
enemy.setPosition(spawnX, spawnY);
enemies.push_back(RectangleShape(enemy));
EnemySpawnTimer = 0;
}
//Collision
for (size_t i = 0; i < bullets.size(); i++)
{
for (size_t k = 0; k < enemies.size(); k++)
{
if (bullets[i].shape.getGlobalBounds().intersects(enemies[k].getGlobalBounds()))
{
bullets.erase(bullets.begin() + i);
enemies.erase(enemies.begin() + k);
//killed++;
score++;
//Decrementing only i to prevent skipping elements after erasing projectiles
i--;
break; //exiting inner loop if collision detected
}
}
}
//different levels of the game
if (score < 30)
{
enemy.setSize(Vector2f(30.f, 30.f));
level = 1;
stime = 30;
enemy.setFillColor(Color::Magenta);
}
else if (score >= 30 && score < 60)
{
level = 2;
stime = 30;
enemy.setFillColor(Color::Green);
}
else if (score >= 60 && score < 90)
{
level = 3;
stime = 25;
enemy.setFillColor(Color::Yellow);
}
else if (score >= 90 && score < 120)
{
level = 4;
stime = 20;
enemy.setFillColor(Color::Red);
}
else if (score >= 120 && score < 150)
{
level = 5;
stime = 30;
enemy.setSize(Vector2f(60.f, 60.f));
enemy.setFillColor(Color::Green);
}
else if (score >= 150 && score < 180)
{
stime = 20;
enemy.setFillColor(Color::Yellow);
}
else if (score >= 180 && score < 210)
{
level = 6;
stime = 10;
enemy.setFillColor(Color::Red);
}
else if (score >= 210 && score < 240)
{
level = 7;
enemy.setSize(Vector2f(20.f, 20.f));
spdMultiplier == 3;
stime = 5;
enemy.setFillColor(Color(255, 215, 0));
}
else if (score >= 240)
{
level = 8;
enemy.setSize(Vector2f(100.f, 100.f));
stime = 5;
}
//Collision of enemies
for (size_t i = 0; i < enemies.size(); i++)
{
if (player.getGlobalBounds().intersects(enemies[i].getGlobalBounds()))
{
pHp--;
enemies.erase(enemies.begin() + i);
}
}
//Updating UI (HP bar....etc)
hpBar.setSize(Vector2f(pHp * 20, 20));
// Check if the player's health is zero
if (pHp <= 0)
{
resetGame(gameState, bullets, enemies);
/* gameState = GAMEOVER;*/
// to Draw the game over window
// Display the game over window before clearing the screen
}
else if (pHp > 0)
{
// DRAW
window.clear(Color(53, 56, 57));
window.draw(BG);
window.draw(player);
for (size_t i = 0; i < bullets.size(); i++)
{
window.draw(bullets[i].shape);
}
for (size_t j = 0; j < enemies.size(); j++)
{
window.draw(enemies[j]);
}
window.draw(hpBar);
window.draw(scoreboard);
window.draw(difficulty);
window.draw(health);
}
}
else if (gameState == CONTROLS)
{
drawControlsMenu(window, MBG);
}
else if (gameState == HighScores)
{
drawHighScores(window, MBG, highscores);
}
else if (gameState == GAMEOVER)
{
music.stop();
beat.stop();
gameOn.play();
drawGameOver(window, score, level, GBG);
Text inputText("Enter your name : ", f, 30);
inputText.setPosition(700, 540);
Event event2;
Text s("Save ... ENTER", f, 30);
s.setPosition(820, 850);
Text t("Main Menu ... ESC", f, 30);
t.setPosition(820, 900);
while (window.isOpen()) {
Event event2;
while (window.pollEvent(event2)) {
if (event2.type == sf::Event::Closed)
window.close();
if (!nameEntered && event2.type == sf::Event::TextEntered) {
if (event2.text.unicode < 128) {
if (event2.text.unicode == '\b') {
if (!playerName.empty()) {
playerName.pop_back();
}
}
else if (event2.text.unicode == '\r') {
// Enter key pressed, finish input
nameEntered = true;
}
else {
playerName += static_cast<char>(event2.text.unicode);
}
}
}
}
window.clear(sf::Color::Black);
// Draw input text
Text nameText(playerName, f, 30);
nameText.setPosition(980, 540);
window.draw(inputText);
window.draw(nameText);
window.draw(t);
window.draw(s);
window.display();
if (nameEntered)
break;
}
}
window.display();
}
return 0;
}