forked from bilalaniq/Xonix-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1799 lines (1516 loc) · 56.9 KB
/
main.cpp
File metadata and controls
1799 lines (1516 loc) · 56.9 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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <time.h>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <iostream>
#include <fstream>
#include <cmath>
enum GameState
{
MENU,
PLAY,
GAMEOVER_MENU,
MODES,
highscore
};
enum GameMode
{
SINGLE_PLAYER,
TWO_PLAYER
};
enum difficulty
{
EASY,
MEDIUM,
HARD,
continuous
};
enum movement_type
{
LINEAR,
ZIG_ZAG,
CIRCULAR
};
GameState gameState = MENU;
difficulty gameDifficulty = EASY;
GameMode gameMode = SINGLE_PLAYER;
float continuousModeTimer = 0;
float enemySpeed_timer = 0;
float enemy_movement_timer = 0;
int tiles_covered_1p = 0;
int tiles_covered_2p = 0;
int movementCounter_1p = 0;
int movementCounter_2p = 0;
int _1p_points = 0;
int _2p_points = 0;
bool lock_1p = false;
bool lock_2p = false;
int reward_counter_1p = 1;
int reward_counter_2p = 1;
sf::Clock powerUpClock;
sf::Clock playModeClock;
bool playModeClock_clockRunning = false;
bool isPaused = false;
bool disablePlayer2Controls = false;
bool disablePlayer1Controls = false;
bool p1_dead = false;
bool p2_dead = false;
bool New_highscore = false;
int power_up_inventory_1p = 0;
int power_up_inventory_2p = 0;
int elapsedTime = 0;
float playElapsedTime = 0;
const int M = 35;
const int N = 55;
int grid[M][N] = {0};
int ts = 18;
struct Enemy
{
int x, y, dx, dy;
movement_type movement = LINEAR;
Enemy()
{
x = y = 300;
dx = 4 - rand() % 11;
dy = 4 - rand() % 11;
}
void move()
{
if (isPaused)
return;
if (movement == LINEAR)
{
x += dx;
if (grid[y / ts][x / ts] == 1)
{
dx = -dx;
x += dx;
}
y += dy;
if (grid[y / ts][x / ts] == 1)
{
dy = -dy;
y += dy;
}
}
else if (movement == ZIG_ZAG)
{
zig_zag();
}
else if (movement == CIRCULAR)
{
circular();
}
}
void zig_zag()
{
static int frameCount = 0;
static bool goingRight = true;
static int verticalDir = 1;
const int zigzagInterval = 50;
frameCount++;
if (frameCount >= zigzagInterval)
{
goingRight = !goingRight;
frameCount = 0;
}
int move_dx;
if (goingRight)
{
move_dx = 5;
}
else
{
move_dx = -5;
}
int move_dy = verticalDir * 2;
move_dx += rand() % 3 - 1;
move_dy += rand() % 3 - 1;
int new_x = x + move_dx;
int new_y = y + move_dy;
if (new_x >= 0 && new_x < N * ts && new_y >= 0 && new_y < M * ts)
{
if (grid[new_y / ts][new_x / ts] != 1)
{
x = new_x;
y = new_y;
}
else
{
goingRight = !goingRight;
verticalDir *= -1;
frameCount = 0;
}
}
else
{
goingRight = !goingRight;
verticalDir *= -1;
}
}
void circular()
{
static float angle = 0.0f;
angle += 0.05f;
int radius = 60;
int cx = 300, cy = 300;
x = cx + static_cast<int>(radius * cos(angle)); // x = h + r cos(t)
y = cy + static_cast<int>(radius * sin(angle)); // y = k + r sin(t)
}
};
struct ScoreEntry
{
int score;
int timeTaken;
};
void ensureScoreFileExists(const std::string &filename = "scores.txt")
{
std::ifstream file(filename);
if (!file)
{
std::cerr << "File not found. Creating a new file: " << filename << std::endl;
std::ofstream newFile(filename); // Create the file
if (!newFile)
{
std::cerr << "Error: Could not create file " << filename << "!" << std::endl;
return;
}
newFile.close();
}
else
{
std::cout << "File " << filename << " already exists." << std::endl;
}
}
void loadScoresFromFile(ScoreEntry scores[], int &count, const std::string &filename = "scores.txt")
{
std::ifstream file(filename);
if (!file)
{
std::cerr << "File not found. Creating a new file: " << filename << std::endl;
std::ofstream newFile(filename);
if (!newFile)
{
std::cerr << "Error: Could not create file " << filename << "!" << std::endl;
count = 0;
return;
}
newFile.close();
count = 0;
return;
}
count = 0;
while (file >> scores[count].score >> scores[count].timeTaken)
{
count++;
if (count >= 5)
{
break;
}
}
file.close();
}
void updateScoreboard(ScoreEntry scores[], int &count, int newScore, int newTime)
{
// Check if the new score qualifies for the top 5
if (count < 5 || newScore > scores[count - 1].score)
{
// Check if the new score is the highest score
if (count == 0 || newScore > scores[0].score) // If no scores exist or newScore is higher
{
New_highscore = true; // Update the high_score flag
}
if (count < 5)
{
count++;
}
int i = count - 1;
while (i > 0 && scores[i - 1].score < newScore)
{
scores[i] = scores[i - 1];
i--;
}
scores[i].score = newScore;
scores[i].timeTaken = newTime;
}
else
{
New_highscore = false; // No new high score
}
}
void saveScoresToFile(ScoreEntry scores[], int count, const std::string &filename = "scores.txt")
{
std::ofstream file(filename);
if (!file)
{
std::cerr << "Error: Could not open file " << filename << " for writing!" << std::endl;
return;
}
// Write only the top 5 scores to the file
for (int i = 0; i < std::min(count, 5); i++)
{
file << scores[i].score << " " << scores[i].timeTaken << std::endl;
}
file.close();
}
void handleGameOver()
{
ScoreEntry scores[5];
int count = 0;
loadScoresFromFile(scores, count);
int newScore;
if (_1p_points > _2p_points)
{
newScore = _1p_points;
}
else if (_2p_points > _1p_points)
{
newScore = _2p_points;
}
else
{
newScore = _1p_points;
}
int newTime = static_cast<int>(playElapsedTime);
updateScoreboard(scores, count, newScore, newTime);
saveScoresToFile(scores, count);
std::cout << "Scoreboard updated!" << std::endl;
}
void drop(int y, int x)
{
if (grid[y][x] == 0)
grid[y][x] = -1;
if (grid[y - 1][x] == 0)
drop(y - 1, x);
if (grid[y + 1][x] == 0)
drop(y + 1, x);
if (grid[y][x - 1] == 0)
drop(y, x - 1);
if (grid[y][x + 1] == 0)
drop(y, x + 1);
}
void construct_boundry()
{
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
if (i == 0 || j == 0 || i == M - 1 || j == N - 1)
grid[i][j] = 1;
}
void dullSprite(sf::Sprite &sprite)
{
sprite.setColor(sf::Color(128, 128, 128, 255)); // Dull the sprite by changing its color to gray
}
void restoreSprite(sf::Sprite &sprite)
{
sprite.setColor(sf::Color::White); // Restore by resetting to original color
}
void reward_p1()
{
if (tiles_covered_1p >= 10 && reward_counter_1p <= 3)
{
_1p_points += tiles_covered_1p * 2;
reward_counter_1p++;
std::cout << "Reward 1: " << reward_counter_1p << " (×2 points)" << std::endl;
}
else if (tiles_covered_1p >= 5 && reward_counter_1p > 3 && reward_counter_1p <= 5)
{
_1p_points += tiles_covered_1p * 2;
reward_counter_1p++;
std::cout << "Reward Counter2: " << reward_counter_1p << " (×2 points, reduced threshold)" << std::endl;
}
else if (tiles_covered_1p >= 5 && reward_counter_1p > 5)
{
_1p_points += tiles_covered_1p * 4;
reward_counter_1p++;
std::cout << "Reward Counter3: " << reward_counter_1p << " (×4 points)" << std::endl;
}
static int nextPowerUpThreshold = 50;
if (_1p_points >= nextPowerUpThreshold)
{
power_up_inventory_1p++;
std::cout << "Player 1 earned a power-up! Total: " << power_up_inventory_1p << std::endl;
nextPowerUpThreshold += 30;
}
tiles_covered_1p = 0;
}
void reward_p2()
{
if (tiles_covered_2p >= 10 && reward_counter_2p <= 3)
{
_2p_points += tiles_covered_2p * 2;
reward_counter_2p++;
std::cout << "Reward 1 (P2): " << reward_counter_2p << " (×2 points)" << std::endl;
}
else if (tiles_covered_2p >= 5 && reward_counter_2p > 3 && reward_counter_2p <= 5)
{
_2p_points += tiles_covered_2p * 2;
reward_counter_2p++;
std::cout << "Reward Counter2 (P2): " << reward_counter_2p << " (×2 points, reduced threshold)" << std::endl;
}
else if (tiles_covered_2p >= 5 && reward_counter_2p > 5)
{
_2p_points += tiles_covered_2p * 4;
reward_counter_2p++;
std::cout << "Reward Counter3 (P2): " << reward_counter_2p << " (×4 points)" << std::endl;
}
static int nextPowerUpThreshold = 50;
if (_2p_points >= nextPowerUpThreshold)
{
power_up_inventory_2p++;
std::cout << "Player 2 earned a power-up! Total: " << power_up_inventory_2p << std::endl;
nextPowerUpThreshold += 30;
}
tiles_covered_2p = 0;
}
void floodFill(int i, int j, int playerTrail)
{
// Check boundaries to avoid out-of-bounds access
if (i < 0 || i >= M || j < 0 || j >= N)
return;
// If the cell is not empty (0) or is a wall (1), stop processing this cell
if (grid[i][j] != 0)
return;
// Mark the cell as part of the player's trail
grid[i][j] = playerTrail;
// Recursively flood-fill all neighbors (4 sides)
floodFill(i - 1, j, playerTrail); // Top
floodFill(i + 1, j, playerTrail); // Bottom
floodFill(i, j - 1, playerTrail); // Left
floodFill(i, j + 1, playerTrail); // Right
}
void processFloodFill(int playerTrail)
{
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (grid[i][j] == playerTrail)
{
if (i > 0)
floodFill(i - 1, j, playerTrail);
if (i < M - 1)
floodFill(i + 1, j, playerTrail);
if (j > 0)
floodFill(i, j - 1, playerTrail);
if (j < N - 1)
floodFill(i, j + 1, playerTrail);
}
}
}
}
int main()
{
ensureScoreFileExists("scores.txt");
srand(time(0));
sf::RenderWindow window(sf::VideoMode(N * ts, M * ts), "Xonix Game!");
window.setFramerateLimit(60);
sf::Texture t1, t2, t3;
t1.loadFromFile("images/tiles.png");
t2.loadFromFile("images/gameover.png");
t3.loadFromFile("images/enemy.png");
sf::Sprite sTile(t1), sGameover(t2), sEnemy(t3);
sGameover.setPosition(300, 300); // The origin (0, 0) is at the top-left corner of the window
sEnemy.setOrigin(20, 20); // Set the origin to the center of the sprite which is 20x20 pixels(assuming the sprite is 40x40 pixels)
// The sprite rotates around its origin point.
// If the origin is at the center of the sprite, the rotation looks natural.
// If the origin is at the top-left corner (default), the sprite rotates around that corner, which can look strange.
int enemyCount = 2;
Enemy a[18];
bool Game = true;
int x = 0, y = 0, dx = 0, dy = 0;
int x_2 = N - 1, y_2 = 0, dx_2 = 0, dy_2 = 0;
bool power_up_1p = false;
bool power_up_2p = false;
float timer = 0, delay = 0.07;
sf::Clock clock;
construct_boundry(); // for making the boundry constructed by 1
sf::Font font;
if (!font.loadFromFile("images/Arial.ttf"))
{
std::cerr << "Error loading font!" << std::endl;
return -1;
}
sf::Texture background_texture;
if (!background_texture.loadFromFile("images/menu_bg.png"))
{
std::cerr << "Error loading menu_bg.png!" << std::endl;
return -1;
}
sf::Sprite menu_background(background_texture);
menu_background.setScale(
static_cast<float>(N * ts) / background_texture.getSize().x,
static_cast<float>(M * ts) / background_texture.getSize().y);
sf::Texture menu_texture;
menu_texture.loadFromFile("images/menu_xonix_pic.png");
sf::Sprite menu_text(menu_texture);
menu_text.setPosition(50, 20);
menu_text.setScale(0.4f, 0.4f);
sf::Text start_button_text;
start_button_text.setFont(font);
start_button_text.setString("Start");
start_button_text.setCharacterSize(50);
start_button_text.setFillColor(sf::Color::White);
start_button_text.setPosition(50, 150);
start_button_text.setStyle(sf::Text::Bold);
sf::Text mode_button_text;
mode_button_text.setFont(font);
mode_button_text.setString("Modes");
mode_button_text.setCharacterSize(50);
mode_button_text.setFillColor(sf::Color::White);
mode_button_text.setPosition(50, 250);
mode_button_text.setStyle(sf::Text::Bold);
sf::Text stop_button_text;
stop_button_text.setFont(font);
stop_button_text.setString("Stop");
stop_button_text.setCharacterSize(50);
stop_button_text.setFillColor(sf::Color::White);
stop_button_text.setPosition(50, 350);
stop_button_text.setStyle(sf::Text::Bold);
sf::Texture _1p_buton_texture;
_1p_buton_texture.loadFromFile("images/1p.png");
sf::Sprite _1p_button(_1p_buton_texture);
_1p_button.setPosition(50, 70);
_1p_button.setScale(0.5, 0.5f);
sf::Texture _2p_buton_texture;
_2p_buton_texture.loadFromFile("images/2p.png");
sf::Sprite _2p_button(_2p_buton_texture);
_2p_button.setPosition(670, 70);
_2p_button.setScale(0.5, 0.5f);
sf::Text easy_button_text;
easy_button_text.setFont(font);
easy_button_text.setString("Easy");
easy_button_text.setCharacterSize(50);
easy_button_text.setFillColor(sf::Color::White);
easy_button_text.setPosition(430, 150);
easy_button_text.setStyle(sf::Text::Bold);
sf::Text medium_button_text;
medium_button_text.setFont(font);
medium_button_text.setString("Medium");
medium_button_text.setCharacterSize(50);
medium_button_text.setFillColor(sf::Color::White);
medium_button_text.setPosition(400, 250);
medium_button_text.setStyle(sf::Text::Bold);
sf::Text hard_button_text;
hard_button_text.setFont(font);
hard_button_text.setString("Hard");
hard_button_text.setCharacterSize(50);
hard_button_text.setFillColor(sf::Color::White);
hard_button_text.setPosition(430, 350);
hard_button_text.setStyle(sf::Text::Bold);
sf::Texture ContinuousMode;
ContinuousMode.loadFromFile("images/ContinuousMode.png");
sf::Sprite continuous_button(ContinuousMode);
continuous_button.setPosition(350, 50);
continuous_button.setScale(1.7, 1.7f);
sf::Text back_button_mode_text;
back_button_mode_text.setFont(font);
back_button_mode_text.setString("Back");
back_button_mode_text.setCharacterSize(50);
back_button_mode_text.setFillColor(sf::Color::White);
back_button_mode_text.setPosition(430, 450);
back_button_mode_text.setStyle(sf::Text::Bold);
sf::Text back_button_score_text;
back_button_score_text.setFont(font);
back_button_score_text.setString("Back");
back_button_score_text.setCharacterSize(50);
back_button_score_text.setFillColor(sf::Color::White);
back_button_score_text.setPosition(350, 500);
back_button_score_text.setStyle(sf::Text::Bold);
sf::Text point_1p;
point_1p.setFont(font);
point_1p.setCharacterSize(25);
point_1p.setFillColor(sf::Color(255, 0, 0));
point_1p.setPosition(10, 30);
point_1p.setString("0");
point_1p.setStyle(sf::Text::Bold);
sf::Text score_x1p;
score_x1p.setFont(font);
score_x1p.setCharacterSize(25);
score_x1p.setFillColor(sf::Color(255, 0, 0));
score_x1p.setPosition(10, 50);
score_x1p.setString("0");
score_x1p.setStyle(sf::Text::Bold);
sf::Text movement_counter_1p_text; // help
movement_counter_1p_text.setFont(font);
movement_counter_1p_text.setCharacterSize(25);
movement_counter_1p_text.setFillColor(sf::Color(255, 0, 0));
movement_counter_1p_text.setPosition(10, 70);
movement_counter_1p_text.setString("0");
movement_counter_1p_text.setStyle(sf::Text::Bold);
sf::Text score_x2p;
score_x2p.setFont(font);
score_x2p.setCharacterSize(25);
score_x2p.setFillColor(sf::Color(255, 165, 0));
score_x2p.setPosition(815, 50);
score_x2p.setString("0");
score_x2p.setStyle(sf::Text::Bold);
sf::Text point_2p;
point_2p.setFont(font);
point_2p.setCharacterSize(25);
point_2p.setFillColor(sf::Color(255, 165, 0));
point_2p.setPosition(815, 30);
point_2p.setString("0");
point_2p.setStyle(sf::Text::Bold);
sf::Text movement_counter_2p_text;
movement_counter_2p_text.setFont(font);
movement_counter_2p_text.setCharacterSize(25);
movement_counter_2p_text.setFillColor(sf::Color(255, 165, 0));
movement_counter_2p_text.setPosition(815, 70);
movement_counter_2p_text.setString("0");
movement_counter_2p_text.setStyle(sf::Text::Bold);
sf::Text playElapsedTimeText;
playElapsedTimeText.setFont(font);
playElapsedTimeText.setCharacterSize(20);
playElapsedTimeText.setFillColor(sf::Color::White);
playElapsedTimeText.setPosition(480, 10);
playElapsedTimeText.setString("0");
sf::SoundBuffer buffer_button;
if (!buffer_button.loadFromFile("sounds/button_click.wav"))
{
std::cerr << "Error loading button_sound!" << std::endl;
return -1;
}
sf::Sound button_sound;
button_sound.setBuffer(buffer_button);
button_sound.setVolume(100);
button_sound.setPitch(1.0f);
sf::SoundBuffer buffer_gameover;
if (!buffer_gameover.loadFromFile("sounds/gameover.wav"))
{
std::cerr << "Error loading gameover_sound!" << std::endl;
return -1;
}
sf::Sound gameover_sound;
gameover_sound.setBuffer(buffer_gameover);
gameover_sound.setVolume(100);
gameover_sound.setPitch(1.0f);
sf::Texture highscore_texture;
highscore_texture.loadFromFile("images/high_score.png");
sf::Sprite highscore_button(highscore_texture);
highscore_button.setPosition(800, 500);
highscore_button.setScale(0.7, 0.7f);
sf::SoundBuffer buffer_power_up;
if (!buffer_power_up.loadFromFile("sounds/power_up.wav"))
{
std::cerr << "Error loading power_up_sound!" << std::endl;
return -1;
}
sf::Sound power_up_sound;
power_up_sound.setBuffer(buffer_power_up);
power_up_sound.setVolume(100);
power_up_sound.setPitch(1.0f);
sf::Texture power_up_texture;
if (!power_up_texture.loadFromFile("images/power_up_effect.png"))
{
std::cerr << "Error loading power_up_effect.png!" << std::endl;
return -1;
}
sf::Sprite power_up(power_up_texture);
sf::SoundBuffer buffer_p1_terminated;
if (!buffer_p1_terminated.loadFromFile("sounds/p1_terminated.wav"))
{
std::cerr << "Error loading p1_terminated_sound!" << std::endl;
return -1;
}
sf::Sound p1_terminated_sound;
p1_terminated_sound.setBuffer(buffer_p1_terminated);
p1_terminated_sound.setVolume(100);
p1_terminated_sound.setPitch(1.0f);
sf::SoundBuffer buffer_p2_terminated;
if (!buffer_p2_terminated.loadFromFile("sounds/p2_terminated.wav"))
{
std::cerr << "Error loading p2_terminated_sound!" << std::endl;
return -1;
}
sf::Sound p2_terminated_sound;
p2_terminated_sound.setBuffer(buffer_p2_terminated);
p2_terminated_sound.setVolume(100);
p2_terminated_sound.setPitch(1.0f);
sf::Text gameover_Menu_restart_text;
gameover_Menu_restart_text.setFont(font);
gameover_Menu_restart_text.setString("Restart");
gameover_Menu_restart_text.setCharacterSize(50);
gameover_Menu_restart_text.setFillColor(sf::Color::White);
gameover_Menu_restart_text.setPosition(50, 60);
gameover_Menu_restart_text.setStyle(sf::Text::Bold);
sf::Text gameover_Menu_resume_text;
gameover_Menu_resume_text.setFont(font);
gameover_Menu_resume_text.setString("Resume");
gameover_Menu_resume_text.setCharacterSize(50);
gameover_Menu_resume_text.setFillColor(sf::Color::White);
gameover_Menu_resume_text.setPosition(55, 170);
gameover_Menu_resume_text.setStyle(sf::Text::Bold);
sf::Text gameover_menu_stop_text;
gameover_menu_stop_text.setFont(font);
gameover_menu_stop_text.setString("Stop");
gameover_menu_stop_text.setCharacterSize(50);
gameover_menu_stop_text.setFillColor(sf::Color::White);
gameover_menu_stop_text.setPosition(50, 420);
gameover_menu_stop_text.setStyle(sf::Text::Bold);
sf::Text score_show_gameover_menu;
score_show_gameover_menu.setFont(font);
score_show_gameover_menu.setString("Score: 0");
score_show_gameover_menu.setCharacterSize(50);
score_show_gameover_menu.setFillColor(sf::Color::White);
score_show_gameover_menu.setPosition(600, 170);
score_show_gameover_menu.setStyle(sf::Text::Bold);
sf::Text score_show_gameover_menu_1p;
score_show_gameover_menu_1p.setFont(font);
score_show_gameover_menu_1p.setString("p1 Score: 0");
score_show_gameover_menu_1p.setCharacterSize(50);
score_show_gameover_menu_1p.setFillColor(sf::Color::White);
score_show_gameover_menu_1p.setPosition(550, 170);
score_show_gameover_menu_1p.setStyle(sf::Text::Bold);
sf::Text score_show_gameover_menu_2p;
score_show_gameover_menu_2p.setFont(font);
score_show_gameover_menu_2p.setString("P2 Score: 0");
score_show_gameover_menu_2p.setCharacterSize(50);
score_show_gameover_menu_2p.setFillColor(sf::Color::White);
score_show_gameover_menu_2p.setPosition(550, 250);
score_show_gameover_menu_2p.setStyle(sf::Text::Bold);
sf::Text who_wins_text;
who_wins_text.setFont(font);
who_wins_text.setString("Still Playing...");
who_wins_text.setCharacterSize(50);
who_wins_text.setFillColor(sf::Color::White);
who_wins_text.setPosition(550, 110);
who_wins_text.setStyle(sf::Text::Bold);
sf::Text New_highscore_text;
New_highscore_text.setFont(font);
New_highscore_text.setString("New Highscore!");
New_highscore_text.setCharacterSize(50);
New_highscore_text.setFillColor(sf::Color::Red);
New_highscore_text.setPosition(600, 250);
New_highscore_text.setStyle(sf::Text::Bold);
sf::Text main_menu_text;
main_menu_text.setFont(font);
main_menu_text.setString("Main Menu");
main_menu_text.setCharacterSize(50);
main_menu_text.setFillColor(sf::Color::White);
main_menu_text.setPosition(50, 300);
main_menu_text.setStyle(sf::Text::Bold);
sf::SoundBuffer buffer_movement;
if (!buffer_movement.loadFromFile("sounds/movement_effect.wav"))
{
std::cerr << "Error loading movement_sound!" << std::endl;
return -1;
}
sf::Sound movement_sound;
movement_sound.setBuffer(buffer_movement);
movement_sound.setVolume(75);
movement_sound.setPitch(3.0f);
while (window.isOpen())
{
float time = clock.getElapsedTime().asSeconds();
clock.restart();
timer += time;
sf::Event e;
while (window.pollEvent(e))
{
if (e.type == sf::Event::Closed)
window.close();
if (e.type == sf::Event::KeyPressed && e.key.code == sf::Keyboard::Escape)
{
if (gameState == PLAY && !power_up_1p && !power_up_2p)
{
gameState = GAMEOVER_MENU;
}
}
if (e.type == sf::Event::MouseButtonPressed && e.mouseButton.button == sf::Mouse::Left)
{
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
if (gameState == MENU)
{
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
if (start_button_text.getGlobalBounds().contains(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y)))
{
button_sound.play();
gameState = PLAY;
}
else if (mode_button_text.getGlobalBounds().contains(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y)))
{
button_sound.play();
gameState = MODES;
}
else if (highscore_button.getGlobalBounds().contains(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y)))
{
button_sound.play();
gameState = highscore;
}
}
else if (gameState == MODES)
{
if (_1p_button.getGlobalBounds().contains((float)mousePos.x, (float)mousePos.y))
{
gameMode = SINGLE_PLAYER;
button_sound.play();
std::cout << "1P mode selected!" << std::endl;
}
else if (_2p_button.getGlobalBounds().contains((float)mousePos.x, (float)mousePos.y))
{
gameMode = TWO_PLAYER;
button_sound.play();
std::cout << "2P mode selected!" << std::endl;
}
else if (back_button_mode_text.getGlobalBounds().contains((float)mousePos.x, (float)mousePos.y))
{
button_sound.play();
gameState = MENU;
}
else if (easy_button_text.getGlobalBounds().contains((float)mousePos.x, (float)mousePos.y))
{
gameDifficulty = EASY;
enemyCount = 2; // Set enemy count for easy mode
button_sound.play();
std::cout << "Easy mode selected!" << std::endl;
}
else if (medium_button_text.getGlobalBounds().contains((float)mousePos.x, (float)mousePos.y))
{
gameDifficulty = MEDIUM;
enemyCount = 4; // Set enemy count for medium mode
button_sound.play();
std::cout << "Medium mode selected!" << std::endl;
}
else if (hard_button_text.getGlobalBounds().contains((float)mousePos.x, (float)mousePos.y))
{
gameDifficulty = HARD;
enemyCount = 6; // Set enemy count for hard mode
button_sound.play();
std::cout << "Hard mode selected!" << std::endl;
}
else if (continuous_button.getGlobalBounds().contains((float)mousePos.x, (float)mousePos.y))
{
gameDifficulty = continuous;
button_sound.play();
std::cout << "Continuous mode selected!" << std::endl;
}
}
else if (gameState == highscore)
{
if (back_button_score_text.getGlobalBounds().contains((float)mousePos.x, (float)mousePos.y))
{
button_sound.play();
gameState = MENU;
}
}
else if (gameState == GAMEOVER_MENU)
{
if (gameover_Menu_restart_text.getGlobalBounds().contains((float)mousePos.x, (float)mousePos.y))
{
button_sound.play();
// Reset timers
continuousModeTimer = 0;
enemySpeed_timer = 0;
enemy_movement_timer = 0;
// Reset scores
_1p_points = 0;
_2p_points = 0;
// Reset game states
isPaused = false;
disablePlayer2Controls = false;
disablePlayer1Controls = false;
p1_dead = false;
p2_dead = false;
// Reset elapsed time
playElapsedTime = 0;
elapsedTime = 0;
// Reset power-ups
power_up_1p = false;
power_up_2p = false;
// Reset reward counters
reward_counter_1p = 1;
reward_counter_2p = 1;
// Reset movement counters
movementCounter_1p = 0;
movementCounter_2p = 0;
power_up_inventory_1p = 0;
power_up_inventory_2p = 0;
// Restart the play mode clock
playModeClock.restart();
playModeClock_clockRunning = false;
// Reset the grid
for (int i = 1; i < M - 1; i++)
{
for (int j = 1; j < N - 1; j++)
{
grid[i][j] = 0; // Clear the grid
}
}
// Reset enemies
for (int i = 0; i < enemyCount; i++)
{
x = y = 300;
a[i].dx = 4 - rand() % 11;
a[i].dy = 4 - rand() % 11;
a[i].movement = LINEAR; // Reset enemy movement type to default
}