-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspaceInvader.cpp
More file actions
1904 lines (1728 loc) · 69.9 KB
/
spaceInvader.cpp
File metadata and controls
1904 lines (1728 loc) · 69.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 <iostream>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <chrono>
#include <thread>
#include <fstream>
#include <iomanip>
using namespace std;
int difficulty = 0; // 0 برای عادی، 1 برای سخت
int numBullets = 1;
int screenWidth = 100; // عرض جدید کنسول
int screenHeight = 40; // ارتفاع جدید کنسول
const int maxEnemies = 9; // تعداد کل دشمنها (3 ردیف × 3 ستون)
const int enemySpacing = 15; // فاصله بین دشمنها در هر ردیف
const int NORMAL = 0;
const int HARD = 1;
struct Player
{
int x, y;
const char *design[2];
int width;
int lives; // تعداد جانها
int score;
string username;
};
Player player[100];
// تعریف استراکت برای تیر
struct Bullet
{
int x, y;
int isActive;
};
// تعریف استراکت برای دشمن
struct Enemy
{
int x, y;
bool active;
const char *design[3]; // طراحی جدید برای انمیها که کوچکتر است
Bullet bullet; // هر دشمن یک تیر دارد
unsigned long lastShotTime; // زمان آخرین شلیک
const int shootCooldown = 0; // زمان خنثیسازی در میلیثانیه
};
// تعریف استراکت برای سفینه
struct Spaceship
{
int x, y;
bool active;
const char *design[3];
Bullet bullet; // Bullet for the spaceship
unsigned long respawnTime; // Time to respawn after being hit
};
// تعریف استراکت برای موانع (Barriers)
struct Barrier
{
int x, y;
int width = 6; // عرض جدید
int height = 4; // ارتفاع جدید
bool active; // Indicates if the barrier is still intact
bool blocks[6][4]; // Represents individual blocks of the barrier (width x height)
};
void gotoxy(int x, int y);
// تعریف استراکت برای امتیاز
struct Score
{
int points;
void draw()
{
gotoxy(screenWidth - 20, 1);
cout << "Score: " << points;
}
void increase(int amount)
{
points += amount;
draw();
}
};
void setColor(int color);
void moveCursor(int x, int y);
// تابع برای تنظیم اندازه کنسول
void setConsoleSize(int width, int height);
// تابع برای جابهجایی مکان مکاننما
void gotoxy(int x, int y);
// تابع برای دریافت ابعاد کنسول
void getConsoleSize();
// تابع برای رسم دورچین
void drawBoundary();
// تعریف استراکت برای بازیکن// تابع برای رسم بازیکن
void drawPlayer(struct Player *player);
// تابع برای پاک کردن بازیکن از صفحه
void erasePlayer(struct Player *player);
// تابع برای حرکت بازیکن
void movePlayer(struct Player *player, char direction);
// تابع برای شلیک تیر
void shootBullet(struct Player *player, struct Bullet *bullet);
// تابع برای حرکت تیر
void moveBullet(struct Bullet *bullet);
// تابع برای رسم دشمنان
void drawEnemies(Enemy enemies[], int maxEnemies);
// تابع برای حرکت دشمنان
void moveEnemies(Enemy enemies[], int maxEnemies, int &direction);
// تابع برای شلیک تیر دشمن به صورت تصادفی
void enemyShoot(Enemy enemies[], int maxEnemies);
// تابع برای حرکت تیر دشمنها
void moveEnemyBullets(Enemy enemies[], int maxEnemies);
// تابع برای بررسی برخورد تیر با دشمنان
bool checkCollisionWithEnemies(struct Bullet *bullet, Enemy enemies[], int maxEnemies, Score &score);
// تابع برای بررسی برخورد تیرهای دشمن با بازیکن
bool checkCollisionWithPlayerBullets(struct Player *player, Enemy enemies[], int maxEnemies);
// تابع برای بررسی برخورد دشمن با بازیکن
bool checkCollisionWithPlayer(struct Player *player, Enemy enemies[], int maxEnemies);
// تابع برای رسم فضاپیما
void drawSpaceship(Spaceship &spaceship);
// تابع برای پاک کردن فضاپیما
void eraseSpaceship(Spaceship &spaceship);
// تابع برای حرکت فضاپیما
void moveSpaceship(Spaceship &spaceship);
// تابع برای اسپاون فضاپیما
void spawnSpaceship(Spaceship &spaceship, unsigned long &lastSpawnTime);
// تابع برای حذف فضاپیما بعد از رسیدن به آخر صفحه
void checkAndRemoveSpaceship(Spaceship &spaceship);
// تابع برای بررسی برخورد تیرهای فضاپیما با بازیکن
// Function to check collision with the spaceship
bool checkCollisionWithSpaceship(struct Bullet *bullet, Spaceship &spaceship, Score &score);
// تابع برای بررسی برخورد تیرهای دشمن با موانع
bool checkBulletCollisionWithBarrier(Bullet *bullet, Barrier &barrier);
// تابع برای رسم موانع
void drawBarrier(Barrier &barrier);
// تابع برای بررسی اینکه آیا فضاپیما باید شلیک کند
void checkSpaceshipShoot(Spaceship &spaceship, unsigned long &lastShootTime);
void moveSpaceshipBullet(Spaceship &spaceship, Player &player);
void hideCursor();
void gameOverAnimation(int screenWidth, int screenHeight);
bool allEnemiesDestroyed(Enemy enemies[], int maxEnemies);
// Function to check collision between spaceship bullets and the player
bool checkSpaceshipBulletCollisionWithPlayer(Spaceship &spaceship, Player &player);
// Function to check collision between spaceship bullets and barriers
bool checkSpaceshipBulletCollisionWithBarrier(Spaceship &spaceship, Barrier barriers[], int numBarriers);
void printLogo();
void printCentered(const string &str, int width);
void clearScreen();
void printCenteredText(const string &str, int width);
void mainMenu(int selected);
void pauseMenu(int selected);
// انتخاب درجه سختی
int selectDifficulty();
void newGame();
void loadGame();
void showLeaderboard();
void howToPlay();
void setDifficulty(int difficultyLevel);
void show_game(Barrier barrier[], struct Player *player, Spaceship &spaceship);
void loadLeaderboard(const string &filename, Player leaderboard[], int &count, int maxEntries);
void savePlayerData(const string &filename, const Player &player);
void bublesort(Player leaderboard[], int count);
bool checkEnemyCollisionWithBarrier(Enemy enemies[], int maxEnemies, Barrier barriers[], int numBarriers);
void spawnSpaceship(Spaceship &spaceship, unsigned long &lastSpawnTime, bool &spaceshipDestroyed, unsigned long &lastDestructionTime);
// تابع اصلی
int main()
{
// Example of enemy bullet settings
SetConsoleOutputCP(CP_UTF8);
hideCursor();
srand(static_cast<unsigned int>(time(0))); // Initialize random seed
setConsoleSize(800, 600); // Setting console size
getConsoleSize();
system("cls");
srand(time(0));
int selected = 0;
bool running = true;
clearScreen();
printLogo();
cout << "\nPress ENTER to enter the main menu..." << endl;
while (_getch() != '\r')
;
system("cls");
while (running)
{
clearScreen();
printLogo();
mainMenu(selected);
char key = _getch();
if (key == 'w' || key == 'W' || key == 72)
{
selected = (selected - 1 + 6) % 6;
}
else if (key == 's' || key == 'S' || key == 80)
{
selected = (selected + 1) % 6;
}
else if (key == '\r')
{
if (selected == 0)
{
system("cls");
newGame();
}
else if (selected == 1)
{
showLeaderboard();
}
else if (selected == 2)
{
system("cls");
howToPlay();
}
else if (selected == 3)
{
running = false; // Exit game
}
system("pause");
}
else if (key == 27)
{ // If ESC is pressed
int pauseSelected = 0;
system("cls");
while (true)
{
// pauseMenu(pauseSelected); // نمایش منوی Pause
moveCursor(0, 0);
char pauseKey = _getch();
if (pauseKey == 'w' || pauseKey == 'W' || pauseKey == 72)
{
pauseSelected = (pauseSelected - 1 + 3) % 3;
}
else if (pauseKey == 's' || pauseKey == 'S' || pauseKey == 80)
{
pauseSelected = (pauseSelected + 1) % 3;
}
else if (pauseKey == '\r')
{
if (pauseSelected == 0)
{
// Logic for saving game goes here
mainMenu(selected);
running = false; // Exit the game after saving
return 0;
}
else if (pauseSelected == 1)
{
cout << "Restarting game..." << endl;
system("cls");
newGame(); // Restart the game by calling newGame function
}
else if (pauseSelected == 2)
{
system("cls");
break; // Resume the game and go back to the game loop
}
}
}
}
}
const char *playerDesign[] = {
" █ ",
"█████"};
struct Player player = {30, screenHeight - 4, {playerDesign[0], playerDesign[1]}, 5, 3}; // number of player lives = 3
struct Bullet bullet = {0, 0, 0};
Score score = {0}; // امتیاز اولیه 0
// طراحی مختلف برای ردیفها
const char *enemyDesign1[] = {
" ███ ",
" █████ ",
" ███ ",
};
const char *enemyDesign2[] = {
" ███ ",
" ████ ",
" ███ ",
};
const char *enemyDesign3[] = {
" ███ ",
" █████ ",
" ███ ",
};
// ایجاد 9 دشمن (3 ردیف × 3 ستون) با فاصله بیشتر بین ردیفها
Enemy enemies[maxEnemies] = {
{screenWidth / 6, 10, true, {enemyDesign1[0], enemyDesign1[1], enemyDesign1[2]}, {0, 0, 0}, 0},
{screenWidth / 6 + enemySpacing, 10, true, {enemyDesign2[0], enemyDesign2[1], enemyDesign2[2]}, {0, 0, 0}, 0},
{screenWidth / 6 + 2 * enemySpacing, 10, true, {enemyDesign3[0], enemyDesign3[1], enemyDesign3[2]}, {0, 0, 0}, 0},
{screenWidth / 6, 14, true, {enemyDesign1[0], enemyDesign1[1], enemyDesign1[2]}, {0, 0, 0}, 0},
{screenWidth / 6 + enemySpacing, 14, true, {enemyDesign2[0], enemyDesign2[1], enemyDesign2[2]}, {0, 0, 0}, 0},
{screenWidth / 6 + 2 * enemySpacing, 14, true, {enemyDesign3[0], enemyDesign3[1], enemyDesign3[2]}, {0, 0, 0}, 0},
{screenWidth / 6, 18, true, {enemyDesign1[0], enemyDesign1[1], enemyDesign1[2]}, {0, 0, 0}, 0},
{screenWidth / 6 + enemySpacing, 18, true, {enemyDesign2[0], enemyDesign2[1], enemyDesign2[2]}, {0, 0, 0}, 0},
{screenWidth / 6 + 2 * enemySpacing, 18, true, {enemyDesign3[0], enemyDesign3[1], enemyDesign3[2]}, {0, 0, 0}, 0}};
// تعریف سفینه جدید
Spaceship spaceship = {0, 0, false, {" ███ ", " █████", " █ "}, {0, 0, 0}, 0}; // Initialize bullet as inactive
unsigned long lastSpawnTime = 0; // زمان آخرین اسپاون
unsigned long lastShootTime = 0; // زمان آخرین شلیک فضاپیما
unsigned long lastDestructionTime = 0; // زمان آخرین تخریب سفینه
const int respawnDelay = 60; // 60 seconds for respawn delay
// Initialize barriers
// Initialize barriers
const int numBarriers = 3; // Number of barriers
Barrier barriers[numBarriers] = {
{20, screenHeight - 10, 6, 4, true, {true}}, // Barrier at position (20, height - 10) with width 6 and height 4
{40, screenHeight - 10, 6, 4, true, {true}}, // Barrier at position (40, height - 10) with width 6 and height 4
{60, screenHeight - 10, 6, 4, true, {true}} // Barrier at position (60, height - 10) with width 6 and height 4
};
return 0;
}
void setColor(int color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
void moveCursor(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// تابع برای تنظیم اندازه کنسول
void setConsoleSize(int width, int height)
{
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r); // Get current console window size
MoveWindow(console, r.left, r.top, width, height, TRUE);
// Set the console buffer size
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT sr;
sr.Left = 0;
sr.Top = 0;
sr.Right = width / 8 - 1; // Considering average character width
sr.Bottom = height / 16 - 1; // Considering average character height
SetConsoleWindowInfo(hConsole, TRUE, &sr);
// Set the console screen buffer size
COORD coord;
coord.X = sr.Right + 1;
coord.Y = sr.Bottom + 1;
SetConsoleScreenBufferSize(hConsole, coord);
}
// تابع برای جابهجایی مکان مکاننما
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// تابع برای دریافت ابعاد کنسول
void getConsoleSize()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
screenWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
screenHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
}
// تابع برای رسم دورچین
void drawBoundary()
{
gotoxy(0, 0);
cout << "╔";
for (int x = 1; x < screenWidth - 1; ++x)
{
cout << "═";
}
cout << "╗";
gotoxy(0, screenHeight - 1);
cout << "╚";
for (int x = 1; x < screenWidth - 1; ++x)
{
cout << "═";
}
cout << "╝";
for (int y = 1; y < screenHeight - 1; ++y)
{
gotoxy(0, y);
cout << "║";
gotoxy(screenWidth - 1, y);
cout << "║";
}
}
// تابع برای رسم بازیکن
void drawPlayer(struct Player *player)
{
for (int i = 0; i < 2; i++)
{
gotoxy(player->x, player->y + i);
cout << player->design[i];
}
}
// تابع برای پاک کردن بازیکن از صفحه
void erasePlayer(struct Player *player)
{
for (int i = 0; i < 2; i++)
{
gotoxy(player->x, player->y + i);
cout << " "; // فضای خالی برای پاک کردنb
}
}
// تابع برای حرکت بازیکن
void movePlayer(struct Player *player, char direction)
{
if (direction == 'a' || direction == 75 || direction == 'A') // Move left
{
if (player->x > 1) // Ensure player doesn't go outside the left boundary
{
erasePlayer(player);
player->x--;
drawPlayer(player);
}
}
else if (direction == 'd' || direction == 77 || direction == 'D') // Move right
{
if (player->x + player->width < screenWidth - 1) // Ensure player doesn't go outside the right boundary
{
erasePlayer(player);
player->x++;
drawPlayer(player);
}
}
int counter = 0;
while (_kbhit())
{
if (counter % 5 == 0)
{
Sleep(1);
}
counter++;
erasePlayer(player);
drawPlayer(player);
char ch = _getch();
if (ch == 'a' || ch == 'd' || ch == 75 || ch == 77 || ch == 'A' || ch == 'D')
{
if (direction == 'a' || direction == 75 || direction == 'A') // Move left
{
if (player->x > 1) // Ensure player doesn't go outside the left boundary
{
erasePlayer(player);
player->x--;
drawPlayer(player);
}
}
else if (direction == 'd' || direction == 77 || direction == 'D') // Move right
{
if (player->x + player->width < screenWidth - 1) // Ensure player doesn't go outside the right boundary
{
erasePlayer(player);
player->x++;
drawPlayer(player);
}
}
}
}
}
// تابع برای شلیک تیر
void shootBullet(struct Player *player, struct Bullet *bullet)
{
bullet->x = player->x + 2; // تیر دقیقاً به وسط سفینه شلیک میشود (با توجه به طراحی)
bullet->y = player->y - 1; // موقعیت تیر در بالای سفینه
bullet->isActive = 1;
}
// تابع برای حرکت تیر
void moveBullet(struct Bullet *bullet)
{
if (bullet->isActive)
{
// Erase the bullet from its current position
gotoxy(bullet->x, bullet->y);
cout << " ";
// Move the bullet up
bullet->y--;
// Check if the bullet is off-screen
if (bullet->y < 1)
{ // Top of the screen
bullet->isActive = false; // Deactivate the bullet
}
else
{
// Draw the bullet at its new position
gotoxy(bullet->x, bullet->y);
cout << "|";
}
}
}
// تابع برای رسم دشمنان
void drawEnemies(Enemy enemies[], int maxEnemies)
{
for (int i = 0; i < maxEnemies; ++i)
{
if (enemies[i].active)
{
for (int j = 0; j < 3; ++j)
{
gotoxy(enemies[i].x, enemies[i].y + j);
cout << enemies[i].design[j];
}
}
}
}
// تابع برای حرکت دشمنان
void moveEnemies(Enemy enemies[], int maxEnemies, int &direction)
{
bool reachedBoundary = false;
// Erase enemies from their current position
for (int i = 0; i < maxEnemies; ++i)
{
if (enemies[i].active)
{
for (int j = 0; j < 3; ++j)
{
gotoxy(enemies[i].x, enemies[i].y + j);
cout << " "; // Clear the enemy's previous position (adjust width as needed)
}
}
}
// Move enemies
for (int i = 0; i < maxEnemies; ++i)
{
if (enemies[i].active)
{
enemies[i].x += direction; // Move in the current direction
// If any enemy reaches the left or right boundary, mark for movement down
if (enemies[i].x >= screenWidth - 15 || enemies[i].x <= 1)
{
reachedBoundary = true;
}
}
}
// If any enemy reaches the boundary, move all enemies down and change direction
if (reachedBoundary)
{
for (int i = 0; i < maxEnemies; ++i)
{
if (enemies[i].active)
{
enemies[i].y += 1; // Move down
}
}
direction = -direction; // Reverse direction
}
// Redraw enemies at new positions
for (int i = 0; i < maxEnemies; ++i)
{
if (enemies[i].active)
{
for (int j = 0; j < 3; ++j)
{
gotoxy(enemies[i].x, enemies[i].y + j);
cout << enemies[i].design[j];
}
}
}
}
bool checkEnemyCollisionWithBarrier(Enemy enemies[], int maxEnemies, Barrier barriers[], int numBarriers)
{
for (int i = 0; i < maxEnemies; ++i)
{
if (enemies[i].active)
{
for (int j = 0; j < numBarriers; ++j)
{
if (barriers[j].active)
{
// Check if the enemy is within the barrier's bounds
if (enemies[i].x + 7 >= barriers[j].x && // Enemy width is 7
enemies[i].x <= barriers[j].x + barriers[j].width &&
enemies[i].y + 3 >= barriers[j].y && // Enemy height is 3
enemies[i].y <= barriers[j].y + barriers[j].height)
{
// Destroy the barrier
barriers[j].active = false;
// Erase the barrier from the screen
for (int k = 0; k < barriers[j].height; ++k)
{
gotoxy(barriers[j].x, barriers[j].y + k);
cout << string(barriers[j].width, ' '); // Clear the barrier
}
// Destroy the enemy
enemies[i].active = false;
// Erase the enemy from the screen
for (int k = 0; k < 3; ++k)
{ // Enemy height is 3
gotoxy(enemies[i].x, enemies[i].y + k);
cout << " "; // Clear the enemy (adjust width as needed)
}
return true; // Collision detected
}
}
}
}
}
return false; // No collision detected
}
// تابع برای شلیک تیر دشمن به صورت تصادفی
void enemyShoot(Enemy enemies[], int maxEnemies)
{
unsigned long currentTime = time(0) * 1000; // Current time in milliseconds
for (int i = 0; i < maxEnemies; ++i)
{
if (enemies[i].active && !enemies[i].bullet.isActive)
{
// Randomly decide to shoot (e.g., 10% chance)
if (rand() % 200 < 2) // Adjust the shooting rate here (0-9, 2 means 20% chance)
{
// Check cooldown
if (currentTime - enemies[i].lastShotTime >= enemies[i].shootCooldown)
{
// Set bullet position just below the enemy
enemies[i].bullet.x = enemies[i].x + (strlen(enemies[i].design[0]) / 2); // Center bullet over enemy
enemies[i].bullet.y = enemies[i].y + 3; // Adjust based on enemy design height
enemies[i].bullet.isActive = true;
enemies[i].lastShotTime = currentTime; // Update last shot time
}
}
}
}
}
// تابع برای حرکت تیر دشمنها
void moveEnemyBullets(Enemy enemies[], int maxEnemies)
{
for (int i = 0; i < maxEnemies; ++i)
{
if (enemies[i].bullet.isActive)
{
gotoxy(enemies[i].bullet.x, enemies[i].bullet.y);
cout << " ";
enemies[i].bullet.y++;
if (enemies[i].bullet.y >= screenHeight - 1)
{ // اگر تیر به پایین برسد
enemies[i].bullet.isActive = 0;
}
else
{
gotoxy(enemies[i].bullet.x, enemies[i].bullet.y);
cout << "*"; // نمایش تیر دشمن
}
}
}
}
// تابع برای بررسی برخورد تیر با دشمنان
bool checkCollisionWithEnemies(struct Bullet *bullet, Enemy enemies[], int maxEnemies, Score &score)
{
// Check if the bullet is active
if (!bullet->isActive)
return false;
for (int i = 0; i < maxEnemies; ++i)
{
if (enemies[i].active)
{
for (int j = 0; j < 3; ++j) // Check each part of the enemy
{
int enemyX = enemies[i].x;
int enemyY = enemies[i].y + j;
// Check if the bullet hits any part of the enemy
if (bullet->x >= enemyX && bullet->x < enemyX + strlen(enemies[i].design[0]) && bullet->y == enemyY)
{
// Determine which row the enemy is in and assign the appropriate score
int row = (enemies[i].y - 10) / 4; // Calculate the row (0 for first row, 1 for second, 2 for third)
int points = 0;
switch (row)
{
case 0:
points = 30; // First row
break;
case 1:
points = 20; // Second row
break;
case 2:
points = 10; // Third row
break;
default:
points = 0; // Default (should not happen)
break;
}
// پاک کردن دشمن برخورد کرده
for (int k = 0; k < 3; ++k)
{
gotoxy(enemies[i].x, enemies[i].y + k);
cout << " "; // فضای خالی برای پاک کردن
}
enemies[i].active = false; // Deactivate the enemy
bullet->isActive = false; // Deactivate the bullet
score.increase(points); // Increase the score based on the row
return true; // Exit after the first hit
}
}
}
}
return false; // هیچ برخوردی شناسایی نشد
}
// تابع برای نمایش جانها
void drawLives(struct Player *player)
{
gotoxy(1, 1);
cout << "Lives: " << player->lives;
}
// تابع برای بررسی برخورد تیرهای دشمن با بازیکن
bool checkCollisionWithPlayerBullets(struct Player *player, Enemy enemies[], int maxEnemies)
{
for (int i = 0; i < maxEnemies; ++i)
{
if (enemies[i].bullet.isActive)
{
// Check if the bullet is within the player's x bounds
if (enemies[i].bullet.x >= player->x && enemies[i].bullet.x <= player->x + player->width)
{
// Check if the bullet is within the player's y bounds
if (enemies[i].bullet.y >= player->y && enemies[i].bullet.y <= player->y + 1) // Player height is 2 rows
{
enemies[i].bullet.isActive = false; // Deactivate the bullet
player->lives--; // Reduce player lives
drawLives(player); // Update lives display
return true; // Collision detected
}
}
}
}
return false; // No collision detected
}
// تابع برای بررسی برخورد دشمن با بازیکن
bool checkCollisionWithPlayer(struct Player *player, Enemy enemies[], int maxEnemies)
{
for (int i = 0; i < maxEnemies; ++i)
{
if (enemies[i].active)
{
for (int j = 0; j < 3; ++j)
{
int enemyX = enemies[i].x;
int enemyY = enemies[i].y + j;
// Check if the enemy hits any part of the player
if (player->x + 1 >= enemyX && player->x + 1 < enemyX + player->width && player->y == enemyY)
{
player->lives--;
return true;
}
}
}
}
return false;
}
// تابع برای رسم فضاپیما
void drawSpaceship(Spaceship &spaceship)
{
// Draw the spaceship's body
if (spaceship.active)
{
for (int i = 0; i < 3; ++i)
{
gotoxy(spaceship.x, spaceship.y + i);
cout << spaceship.design[i];
}
}
}
// تابع برای پاک کردن فضاپیما
void eraseSpaceship(Spaceship &spaceship)
{
if (spaceship.active)
{
for (int i = 0; i < 3; ++i)
{
gotoxy(spaceship.x, spaceship.y + i);
cout << " "; // فضای خالی برای پاک کردن
}
}
}
// تابع برای حرکت فضاپیما
void moveSpaceship(Spaceship &spaceship)
{
if (spaceship.active)
{
// قبل از حرکت فضاپیما از صفحه حذف میشود
eraseSpaceship(spaceship);
spaceship.x++; // حرکت به سمت راست
drawSpaceship(spaceship); // دوباره رسم فضاپیما در موقعیت جدید
}
}
// تابع برای اسپاون فضاپیما
void spawnSpaceship(Spaceship &spaceship, unsigned long &lastSpawnTime)
{
unsigned long currentTime = time(0); // زمان جاری به ثانیه
if (currentTime - lastSpawnTime >= 30)
{ // اگر 30 ثانیه گذشته باشد
if (!spaceship.active)
{
spaceship.x = 2; // مکان ابتدایی فضاپیما
spaceship.y = 5; // مکان ابتدایی فضاپیما
spaceship.active = true;
lastSpawnTime = currentTime;
}
}
}
// تابع برای حذف فضاپیما بعد از رسیدن به آخر صفحه
void checkAndRemoveSpaceship(Spaceship &spaceship)
{
if (spaceship.active && spaceship.x >= screenWidth - 18)
{ // If spaceship reaches the end of the screen
eraseSpaceship(spaceship); // Erase spaceship before deactivating
spaceship.active = false;
spaceship.bullet.isActive = false; // Deactivate spaceship's bullet
}
}
// تابع برای بررسی برخورد تیرهای فضاپیما با بازیکن
// Function to check collision with the spaceship
bool checkCollisionWithSpaceship(struct Bullet *bullet, Spaceship &spaceship, Score &score)
{
if (bullet->isActive && spaceship.active) // Check if bullet is active and spaceship is active
{
// Check if bullet is within the x bounds of the spaceship
if (bullet->x >= spaceship.x + 2 && bullet->x <= spaceship.x + 4) // Only middle area
{
// Check if bullet is within the y bounds of the spaceship
if (bullet->y >= spaceship.y && bullet->y <= spaceship.y + 2) // Check y range
{
// Clear the spaceship from the screen
for (int i = 0; i < 3; ++i)
{
gotoxy(spaceship.x, spaceship.y + i);
cout << " "; // Clear space for spaceship
}
bullet->isActive = false; // Deactivate player bullet
spaceship.active = false; // Deactivate spaceship (destroy it)
spaceship.bullet.isActive = false; // Deactivate spaceship's bullet
// Random score between 75 to 200
int randomScore = rand() % 126 + 75; // Generates random score
score.increase(randomScore); // Increase score
return true; // Collision detected
}
}
}
return false; // No collision detected
}
// تابع برای بررسی برخورد تیرهای دشمن با موانع
bool checkBulletCollisionWithBarrier(Bullet *bullet, Barrier &barrier)
{
if (bullet->isActive && barrier.active) // Only check if the barrier is active
{
// Calculate the relative position of the bullet within the barrier
int relativeX = bullet->x - barrier.x;
int relativeY = bullet->y - barrier.y;
// Check if the bullet is within the bounds of the barrier
if (relativeX >= 0 && relativeX < barrier.width &&
relativeY >= 0 && relativeY < barrier.height)
{
// Check if the specific block is active
if (barrier.blocks[relativeX][relativeY])
{
// Deactivate the specific block
barrier.blocks[relativeX][relativeY] = false;
// Clear the block from the screen
gotoxy(bullet->x, bullet->y);
cout << " ";
bullet->isActive = false; // Deactivate the bullet
return true; // Collision detected
}
}
}
return false; // No collision detected
}
// تابع برای رسم موانع
void drawBarrier(Barrier &barrier)
{
if (barrier.active)
{
for (int i = 0; i < barrier.height; i++)
{
gotoxy(barrier.x, barrier.y + i);
for (int j = 0; j < barrier.width; j++)
{
if (barrier.blocks[j][i]) // Only draw active blocks
{
cout << "█";
}
else
{
cout << " "; // Draw space for inactive blocks
}
}
}
}
}
// تابع برای بررسی اینکه آیا فضاپیما باید شلیک کند
void checkSpaceshipShoot(Spaceship &spaceship, unsigned long &lastShootTime)
{
unsigned long currentTime = time(0) * 1000; // Current time in milliseconds
// Check if enough time has passed to shoot
if (currentTime - lastShootTime >= 2000) // 2 seconds interval
{
if (spaceship.active)
{
spaceship.bullet.x = spaceship.x + 2; // Center bullet
spaceship.bullet.y = spaceship.y + 3; // Just below spaceship
spaceship.bullet.isActive = true; // Activate bullet
}
lastShootTime = currentTime; // Update last shoot time
}
}
void moveSpaceshipBullet(Spaceship &spaceship, Player &player)
{
if (spaceship.bullet.isActive)
{
gotoxy(spaceship.bullet.x, spaceship.bullet.y);
cout << " "; // Clear previous bullet position
spaceship.bullet.y++; // Move the bullet down
// Deactivate the bullet if it passes the player's y-position
if (spaceship.bullet.y >= player.y + 1)
{
spaceship.bullet.isActive = false;
}
else
{
gotoxy(spaceship.bullet.x, spaceship.bullet.y);
cout << "*"; // Draw the bullet
}
}
}
void hideCursor()
{