-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspaceinvaders.cpp
More file actions
827 lines (703 loc) · 28.2 KB
/
Copy pathspaceinvaders.cpp
File metadata and controls
827 lines (703 loc) · 28.2 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
#include "help.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;
const int MAX_ALIENS_X = 9;
const int MAX_ALIENS_Y = 5;
const int MAX_BULLETS = 10;
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int LEFT_BOUNDARY = 55;
const int RIGHT_BOUNDARY = SCREEN_WIDTH - 50;
const int TOP_BOUNDARY = 55;
const int BOTTOM_BOUNDARY = SCREEN_HEIGHT - 55;
enum GameState {
MAIN_MENU,
SETTINGS_MENU,
HIGH_SCORES_MENU,
CONTROLS_MENU,
ABOUT_MENU,
PLAYING,
PAUSED,
GAME_OVER,
WIN,
INSTRUCTIONS
};
struct Bullet {
int x,y;
bool active;
bool isAlienBullet;
};
struct Alien {
int x, y;
bool active;
};
struct Player {
int x, y;
int lives;
int score;
};
class SpaceInvaders {
private:
Player player;
Alien aliens[MAX_ALIENS_Y][MAX_ALIENS_X];
Bullet bullets[MAX_BULLETS];
Bullet alienBullets[MAX_BULLETS];
GameState gameState;
int alienDirection;
int alienMoveCounter;
int alienShootCounter;
int aliensRemaining;
bool keyPressed[256];
int menuSelection;
int animationCounter;
int highScore;
bool soundEnabled;
int difficulty;
public:
SpaceInvaders() {
gameState = MAIN_MENU;
alienDirection = 1;
alienMoveCounter = 0;
alienShootCounter = 0;
menuSelection = 0;
animationCounter = 0;
highScore = 0;
soundEnabled = true;
difficulty = 1; // Normal difficulty
for (int i = 0; i < 256; i++) {
keyPressed[i] = false;
}
initializeGame();
}
void initializeGame() {
// Player setup
player.x = SCREEN_WIDTH / 2 - 15;
player.y = SCREEN_HEIGHT - 100;
player.lives = 3;
player.score = 0;
// Aliens setup
aliensRemaining = MAX_ALIENS_X * MAX_ALIENS_Y;
for (int i = 0; i < MAX_ALIENS_Y; i++) {
for (int j = 0; j < MAX_ALIENS_X; j++) {
aliens[i][j].x = 100 + j * 70;
aliens[i][j].y = 100 + i * 50;
aliens[i][j].active = true;
}
}
// Bullets setup
for (int i = 0; i < MAX_BULLETS; i++) {
bullets[i].active = false;
bullets[i].isAlienBullet = false;
alienBullets[i].active = false;
alienBullets[i].isAlienBullet = true;
}
alienDirection = 1;
alienMoveCounter = 0;
alienShootCounter = 0;
}
void showMainMenu() {
clearScreen();
for (int i = 0; i < 20; i++) {
int x = 50 + (i * 37 + animationCounter) % 700;
int y = 50 + (i * 23) % 400;
int brightness = 150 + (animationCounter + i * 10) % 105;
myEllipse(x, y, x + 2, y + 2, brightness, brightness, brightness, brightness, brightness, brightness);
}
// Main title with glow effect
myRect(150, 80, 650, 160, 0, 255, 255, 0, 30, 60);
myRect(160, 90, 640, 150, 255, 255, 0, 0, 0, 0);
drawGraphicsText(220, 110, "SPACE INVADERS", 255, 255, 0);
// Subtitle
drawGraphicsText(320, 180, "CLASSIC EDITION", 0, 255, 255);
myRect(200, 220, 600, 480, 255, 255, 255, 0, 0, 50);
myRect(210, 230, 590, 470, 100, 100, 100, 20, 20, 40);
const char* menuOptions[] = {
"1. START GAME",
"2. HIGH SCORES",
"3. SETTINGS",
"4. CONTROLS",
"5. ABOUT",
"6. EXIT"
};
for (int i = 0; i < 6; i++) {
int yPos = 250 + i * 35;
if (i == menuSelection) {
// Animated selection highlight
int glow = 200 + (animationCounter % 55);
myRect(220, yPos - 3, 580, yPos + 25, 255, 255, 0, glow, glow, 0);
drawGraphicsText(230, yPos, menuOptions[i], 0, 0, 0);
// Selection arrow
myRect(200, yPos + 8, 210, yPos + 18, 255, 0, 0, 255, 0, 0);
} else {
drawGraphicsText(230, yPos, menuOptions[i], 255, 255, 255);
}
}
// Footer info
drawGraphicsText(150, 500, "Use UP/DOWN arrows to navigate, ENTER to select", 150, 150, 150);
// High score display
char scoreText[50];
sprintf_s(scoreText, "High Score: %d", highScore);
drawGraphicsText(500, 520, scoreText, 255, 255, 0);
// Animated decorative elements
int offset = (animationCounter / 3) % 30;
// Left alien
myRect(80 - offset, 300, 120 - offset, 340, 255, 0, 0, 200, 0, 0);
myRect(85 - offset, 310, 95 - offset, 320, 255, 255, 0, 255, 255, 0);
myRect(105 - offset, 310, 115 - offset, 320, 255, 255, 0, 255, 255, 0);
// Right alien
myRect(680 + offset, 300, 720 + offset, 340, 255, 0, 0, 200, 0, 0);
myRect(685 + offset, 310, 695 + offset, 320, 255, 255, 0, 255, 255, 0);
myRect(705 + offset, 310, 715 + offset, 320, 255, 255, 0, 255, 255, 0);
// Player ship at bottom
myRect(375, 540, 425, 570, 0, 255, 0, 0, 200, 0);
myRect(395, 525, 405, 540, 0, 255, 0, 0, 200, 0);
// Animated thruster
if ((animationCounter / 5) % 2 == 0) {
myRect(390, 570, 410, 580, 255, 100, 0, 255, 100, 0);
} else {
myRect(392, 570, 408, 578, 255, 200, 0, 255, 200, 0);
}
}
void showHighScoresMenu() {
clearScreen();
myRect(250, 50, 550, 100, 255, 215, 0, 100, 100, 0);
drawGraphicsText(320, 65, "HIGH SCORES", 255, 255, 255);
// High score display
myRect(200, 150, 600, 400, 50, 50, 100, 20, 20, 40);
drawGraphicsText(250, 180, "CURRENT HIGH SCORE:", 255, 255, 255);
drawGraphicsNumber(350, 220, highScore, 255, 255, 0);
// Difficulty levels
drawGraphicsText(250, 280, "DIFFICULTY RECORDS:", 200, 200, 200);
drawGraphicsText(250, 320, "Easy: 500", 0, 255, 0);
drawGraphicsText(250, 350, "Normal: 750", 255, 255, 0);
drawGraphicsText(250, 380, "Hard: 1000", 255, 0, 0);
drawGraphicsText(250, 450, "Press ESCAPE to return", 0, 255, 255);
// Animated background
for (int i = 0; i < 15; i++) {
int x = 100 + (i * 40 + animationCounter) % 600;
int y = 100 + (i * 30) % 300;
myEllipse(x, y, x + 2, y + 2, 100, 100, 100, 100, 100, 100);
}
}
void showSettingsMenu() {
clearScreen();
// Title
myRect(250, 50, 550, 100, 100, 255, 100, 50, 100, 50);
drawGraphicsText(340, 65, "SETTINGS", 255, 255, 255);
// Settings box
myRect(200, 150, 600, 450, 100, 100, 100, 30, 30, 50);
// Sound setting
drawGraphicsText(230, 180, "SOUND EFFECTS:", 255, 255, 255);
if (soundEnabled) {
drawGraphicsText(450, 180, "ON", 0, 255, 0);
} else {
drawGraphicsText(450, 180, "OFF", 255, 0, 0);
}
// Difficulty setting
drawGraphicsText(230, 220, "DIFFICULTY:", 255, 255, 255);
const char* diffNames[] = {"EASY", "NORMAL", "HARD"};
int diffColors[][3] = {{0, 255, 0}, {255, 255, 0}, {255, 0, 0}};
drawGraphicsText(450, 220, diffNames[difficulty],
diffColors[difficulty][0], diffColors[difficulty][1], diffColors[difficulty][2]);
// Controls info
drawGraphicsText(230, 280, "CONTROLS:", 200, 200, 200);
drawGraphicsText(250, 310, "1/2 - Toggle Sound", 150, 150, 150);
drawGraphicsText(250, 340, "3/4 - Change Difficulty", 150, 150, 150);
drawGraphicsText(250, 370, "R - Reset High Score", 150, 150, 150);
drawGraphicsText(250, 480, "Press ESCAPE to return", 0, 255, 255);
}
void showControlsMenu() {
clearScreen();
// Title
myRect(250, 50, 550, 100, 0, 255, 0, 0, 100, 0);
drawGraphicsText(320, 65, "CONTROLS", 255, 255, 255);
// Controls box
myRect(150, 130, 650, 480, 100, 100, 100, 30, 30, 50);
// Game controls
drawGraphicsText(180, 160, "GAME CONTROLS:", 255, 255, 0);
drawGraphicsText(200, 200, "LEFT ARROW - Move ship left", 255, 255, 255);
drawGraphicsText(200, 230, "RIGHT ARROW - Move ship right", 255, 255, 255);
drawGraphicsText(200, 260, "SPACEBAR - Fire laser", 255, 255, 255);
drawGraphicsText(200, 290, "P - Pause game", 255, 255, 255);
drawGraphicsText(200, 320, "ESCAPE - Return to menu", 255, 255, 255);
// Menu controls
drawGraphicsText(180, 370, "MENU CONTROLS:", 255, 255, 0);
drawGraphicsText(200, 400, "UP/DOWN - Navigate menus", 255, 255, 255);
drawGraphicsText(200, 430, "ENTER - Select option", 255, 255, 255);
drawGraphicsText(250, 510, "Press ESCAPE to return", 0, 255, 255);
// Show sample controls visually
myRect(500, 200, 530, 230, 255, 255, 255, 200, 200, 200);
drawGraphicsText(505, 210, "<", 0, 0, 0);
myRect(540, 200, 570, 230, 255, 255, 255, 200, 200, 200);
drawGraphicsText(545, 210, ">", 0, 0, 0);
myRect(520, 240, 580, 270, 255, 255, 255, 200, 200, 200);
drawGraphicsText(530, 250, "SPACE", 0, 0, 0);
}
void showAboutMenu() {
clearScreen();
// Title
myRect(250, 50, 550, 100, 255, 0, 255, 100, 0, 100);
drawGraphicsText(340, 65, "ABOUT", 255, 255, 255);
// About box
myRect(150, 130, 650, 480, 100, 100, 100, 30, 30, 50);
drawGraphicsText(300, 160, "SPACE INVADERS", 255, 255, 0);
drawGraphicsText(320, 190, "Classic Edition", 0, 255, 255);
drawGraphicsText(180, 240, "A faithful recreation of the classic", 255, 255, 255);
drawGraphicsText(180, 270, "arcade game Space Invaders.", 255, 255, 255);
drawGraphicsText(180, 320, "FEATURES:", 200, 200, 200);
drawGraphicsText(200, 350, "• Classic gameplay mechanics", 150, 150, 150);
drawGraphicsText(200, 380, "• Smooth graphics and animation", 150, 150, 150);
drawGraphicsText(200, 410, "• Multiple difficulty levels", 150, 150, 150);
drawGraphicsText(200, 440, "• High score tracking", 150, 150, 150);
drawGraphicsText(250, 510, "Press ESCAPE to return", 0, 255, 255);
// Version info
drawGraphicsText(600, 520, "v1.0", 100, 100, 100);
// Animated elements
for (int i = 0; i < 10; i++) {
int x = 200 + (i * 40 + animationCounter) % 400;
int y = 500 + sin((animationCounter + i * 20) * 0.1) * 10;
myEllipse(x, y, x + 2, y + 2, 0, 255, 255, 0, 255, 255);
}
}
void showPauseMenu() {
// Semi-transparent overlay
myRect(200, 200, 600, 400, 255, 255, 255, 50, 50, 50);
myRect(210, 210, 590, 390, 100, 100, 100, 30, 30, 50);
drawGraphicsText(350, 240, "PAUSED", 255, 255, 0);
drawGraphicsText(250, 290, "P - Resume Game", 255, 255, 255);
drawGraphicsText(250, 320, "ESC - Return to Main Menu", 255, 255, 255);
drawGraphicsText(250, 350, "Q - Quit Game", 255, 255, 255);
}
void showInstructions() {
clearScreen();
drawGraphicsText(300, 50, "INSTRUCTIONS", 255, 255, 0);
drawGraphicsText(50, 150, "LEFT/RIGHT ARROWS - Move ship", 255, 255, 255);
drawGraphicsText(50, 200, "SPACEBAR - Fire", 255, 255, 255);
drawGraphicsText(50, 250, "ESCAPE - Return to menu", 255, 255, 255);
drawGraphicsText(50, 350, "Destroy all aliens to win!", 255, 255, 255);
drawGraphicsText(50, 400, "Don't let them reach the bottom!", 255, 255, 255);
drawGraphicsText(250, 500, "Press ESCAPE to return", 0, 255, 255);
}
void drawGameArea() {
clearScreen();
// Draw the boundary walls - THICK AND SOLID
myRect(50, 50, SCREEN_WIDTH - 50, SCREEN_HEIGHT - 50, 255, 255, 255);
myRect(51, 51, SCREEN_WIDTH - 51, SCREEN_HEIGHT - 51, 255, 255, 255);
myRect(52, 52, SCREEN_WIDTH - 52, SCREEN_HEIGHT - 52, 255, 255, 255);
// UI
drawGraphicsText(60, 10, "SCORE:", 255, 255, 0);
drawGraphicsNumber(140, 10, player.score, 255, 255, 0);
drawGraphicsText(300, 10, "LIVES:", 255, 0, 0);
drawGraphicsNumber(370, 10, player.lives, 255, 0, 0);
drawGraphicsText(500, 10, "ALIENS:", 0, 255, 0);
drawGraphicsNumber(580, 10, aliensRemaining, 0, 255, 0);
}
void drawPlayer() {
myRect(player.x, player.y, player.x + 30, player.y + 20, 0, 255, 0, 0, 255, 0);
myRect(player.x + 12, player.y - 5, player.x + 18, player.y, 0, 255, 0, 0, 255, 0);
}
void erasePlayer() {
myRect(player.x, player.y - 5, player.x + 30, player.y + 20, 0, 0, 0, 0, 0, 0);
}
void drawAliens() {
for (int i = 0; i < MAX_ALIENS_Y; i++) {
for (int j = 0; j < MAX_ALIENS_X; j++) {
if (aliens[i][j].active) {
myRect(aliens[i][j].x, aliens[i][j].y,
aliens[i][j].x + 25, aliens[i][j].y + 20,
255, 0, 0, 200, 0, 0);
// Eyes
myRect(aliens[i][j].x + 5, aliens[i][j].y + 5,
aliens[i][j].x + 10, aliens[i][j].y + 10,
255, 255, 0, 255, 255, 0);
myRect(aliens[i][j].x + 15, aliens[i][j].y + 5,
aliens[i][j].x + 20, aliens[i][j].y + 10,
255, 255, 0, 255, 255, 0);
}
}
}
}
void eraseAliens() {
for (int i = 0; i < MAX_ALIENS_Y; i++) {
for (int j = 0; j < MAX_ALIENS_X; j++) {
if (aliens[i][j].active) {
myRect(aliens[i][j].x, aliens[i][j].y,
aliens[i][j].x + 25, aliens[i][j].y + 20,
0, 0, 0, 0, 0, 0);
}
}
}
}
void moveAliens() {
alienMoveCounter++;
if (alienMoveCounter < 20) return;
alienMoveCounter = 0;
eraseAliens();
bool shouldMoveDown = false;
// Check if any alien hits the side boundaries
for (int i = 0; i < MAX_ALIENS_Y; i++) {
for (int j = 0; j < MAX_ALIENS_X; j++) {
if (aliens[i][j].active) {
if ((aliens[i][j].x <= LEFT_BOUNDARY + 10 && alienDirection == -1) ||
(aliens[i][j].x >= RIGHT_BOUNDARY - 35 && alienDirection == 1)) {
shouldMoveDown = true;
break;
}
}
}
if (shouldMoveDown) break;
}
if (shouldMoveDown) {
// Move all aliens down
for (int i = 0; i < MAX_ALIENS_Y; i++) {
for (int j = 0; j < MAX_ALIENS_X; j++) {
if (aliens[i][j].active) {
aliens[i][j].y += 30;
if (aliens[i][j].y >= player.y - 30) {
gameState = GAME_OVER;
return;
}
}
}
}
alienDirection *= -1;
} else {
// Move horizontally
for (int i = 0; i < MAX_ALIENS_Y; i++) {
for (int j = 0; j < MAX_ALIENS_X; j++) {
if (aliens[i][j].active) {
aliens[i][j].x += alienDirection * 15;
}
}
}
}
drawAliens();
}
void shootBullet() {
for (int i = 0; i < MAX_BULLETS; i++) {
if (!bullets[i].active) {
bullets[i].x = player.x + 15;
bullets[i].y = player.y - 5;
bullets[i].active = true;
bullets[i].isAlienBullet = false;
break;
}
}
}
void alienShoot() {
alienShootCounter++;
if (alienShootCounter < 80) return;
alienShootCounter = 0;
// Find a random active alien to shoot
for (int attempt = 0; attempt < 50; attempt++) {
int row = rand() % MAX_ALIENS_Y;
int col = rand() % MAX_ALIENS_X;
if (aliens[row][col].active) {
for (int i = 0; i < MAX_BULLETS; i++) {
if (!alienBullets[i].active) {
alienBullets[i].x = aliens[row][col].x + 12;
alienBullets[i].y = aliens[row][col].y + 20;
alienBullets[i].active = true;
alienBullets[i].isAlienBullet = true;
return;
}
}
}
}
}
void updateBullets() {
// Player bullets
for (int i = 0; i < MAX_BULLETS; i++) {
if (bullets[i].active) {
// Erase old position
myRect(bullets[i].x, bullets[i].y, bullets[i].x + 4, bullets[i].y + 10, 0, 0, 0, 0, 0, 0);
// Move up
bullets[i].y -= 15;
// Check if hit top boundary - PROPER BOUNDARY CHECK
if (bullets[i].y <= TOP_BOUNDARY) {
bullets[i].active = false;
continue;
}
// Draw at new position
myRect(bullets[i].x, bullets[i].y, bullets[i].x + 4, bullets[i].y + 10, 255, 255, 0, 255, 255, 0);
}
}
// Alien bullets - THE MAIN FIX
for (int i = 0; i < MAX_BULLETS; i++) {
if (alienBullets[i].active) {
// Erase old position
myRect(alienBullets[i].x, alienBullets[i].y, alienBullets[i].x + 4, alienBullets[i].y + 10, 0, 0, 0, 0, 0, 0);
// Move down
alienBullets[i].y += 8;
// STRICT BOUNDARY CHECK - deactivate if approaching boundary
if (alienBullets[i].y >= BOTTOM_BOUNDARY - 15 ||
alienBullets[i].x <= LEFT_BOUNDARY ||
alienBullets[i].x >= RIGHT_BOUNDARY) {
alienBullets[i].active = false;
continue;
}
// Draw at new position
myRect(alienBullets[i].x, alienBullets[i].y, alienBullets[i].x + 4, alienBullets[i].y + 10, 255, 0, 255, 255, 0, 255);
}
}
}
void checkCollisions() {
// Player bullets vs aliens
for (int i = 0; i < MAX_BULLETS; i++) {
if (bullets[i].active) {
for (int row = 0; row < MAX_ALIENS_Y; row++) {
for (int col = 0; col < MAX_ALIENS_X; col++) {
if (aliens[row][col].active) {
if (bullets[i].x >= aliens[row][col].x &&
bullets[i].x <= aliens[row][col].x + 25 &&
bullets[i].y >= aliens[row][col].y &&
bullets[i].y <= aliens[row][col].y + 20) {
// Hit!
bullets[i].active = false;
aliens[row][col].active = false;
aliensRemaining--;
player.score += 10;
// Clean up graphics
myRect(bullets[i].x, bullets[i].y, bullets[i].x + 4, bullets[i].y + 10, 0, 0, 0, 0, 0, 0);
myRect(aliens[row][col].x, aliens[row][col].y,
aliens[row][col].x + 25, aliens[row][col].y + 20, 0, 0, 0, 0, 0, 0);
// Update UI
myRect(140, 10, 200, 35, 0, 0, 0, 0, 0, 0);
myRect(580, 10, 640, 35, 0, 0, 0, 0, 0, 0);
drawGraphicsNumber(140, 10, player.score, 255, 255, 0);
drawGraphicsNumber(580, 10, aliensRemaining, 0, 255, 0);
if (aliensRemaining == 0) {
gameState = WIN;
}
return;
}
}
}
}
}
}
// Alien bullets vs player
for (int i = 0; i < MAX_BULLETS; i++) {
if (alienBullets[i].active) {
if (alienBullets[i].x >= player.x &&
alienBullets[i].x <= player.x + 30 &&
alienBullets[i].y >= player.y &&
alienBullets[i].y <= player.y + 20) {
// Player hit!
alienBullets[i].active = false;
player.lives--;
myRect(alienBullets[i].x, alienBullets[i].y, alienBullets[i].x + 4, alienBullets[i].y + 10, 0, 0, 0, 0, 0, 0);
// Update lives UI
myRect(370, 10, 430, 35, 0, 0, 0, 0, 0, 0);
drawGraphicsNumber(370, 10, player.lives, 255, 0, 0);
if (player.lives <= 0) {
gameState = GAME_OVER;
}
return;
}
}
}
}
bool isKeyCurrentlyPressed(int vkCode) {
bool currentState = (GetAsyncKeyState(vkCode) & 0x8000) != 0;
bool wasPressed = keyPressed[vkCode];
keyPressed[vkCode] = currentState;
return currentState && !wasPressed;
}
void handleInput() {
switch (gameState) {
case MAIN_MENU:
if (isKeyCurrentlyPressed(VK_UP)) {
menuSelection = (menuSelection - 1 + 6) % 6;
showMainMenu();
} else if (isKeyCurrentlyPressed(VK_DOWN)) {
menuSelection = (menuSelection + 1) % 6;
showMainMenu();
} else if (isKeyCurrentlyPressed(VK_RETURN)) {
switch (menuSelection) {
case 0: // Start Game
initializeGame();
gameState = PLAYING;
drawGameArea();
drawPlayer();
drawAliens();
break;
case 1: // High Scores
gameState = HIGH_SCORES_MENU;
showHighScoresMenu();
break;
case 2: // Settings
gameState = SETTINGS_MENU;
showSettingsMenu();
break;
case 3: // Controls
gameState = CONTROLS_MENU;
showControlsMenu();
break;
case 4: // About
gameState = ABOUT_MENU;
showAboutMenu();
break;
case 5: // Exit
exit(0);
break;
}
}
break;
case HIGH_SCORES_MENU:
case CONTROLS_MENU:
case ABOUT_MENU:
if (isKeyCurrentlyPressed(VK_ESCAPE)) {
gameState = MAIN_MENU;
menuSelection = 0;
showMainMenu();
}
break;
case SETTINGS_MENU:
if (isKeyCurrentlyPressed(VK_ESCAPE)) {
gameState = MAIN_MENU;
menuSelection = 0;
showMainMenu();
} else if (isKeyCurrentlyPressed('1')) {
soundEnabled = true;
showSettingsMenu();
} else if (isKeyCurrentlyPressed('2')) {
soundEnabled = false;
showSettingsMenu();
} else if (isKeyCurrentlyPressed('3')) {
difficulty = (difficulty + 1) % 3;
showSettingsMenu();
} else if (isKeyCurrentlyPressed('4')) {
difficulty = (difficulty - 1 + 3) % 3;
showSettingsMenu();
} else if (isKeyCurrentlyPressed('R')) {
highScore = 0;
showSettingsMenu();
}
break;
case PLAYING:
// Player movement
if (GetAsyncKeyState(VK_LEFT) & 0x8000) {
if (player.x > LEFT_BOUNDARY + 5) {
erasePlayer();
player.x -= 8;
drawPlayer();
}
}
if (GetAsyncKeyState(VK_RIGHT) & 0x8000) {
if (player.x < RIGHT_BOUNDARY - 35) {
erasePlayer();
player.x += 8;
drawPlayer();
}
}
if (isKeyCurrentlyPressed(VK_SPACE)) {
shootBullet();
}
if (isKeyCurrentlyPressed('P')) {
gameState = PAUSED;
showPauseMenu();
}
if (isKeyCurrentlyPressed(VK_ESCAPE)) {
gameState = MAIN_MENU;
menuSelection = 0;
showMainMenu();
}
break;
case PAUSED:
if (isKeyCurrentlyPressed('P')) {
gameState = PLAYING;
drawGameArea();
drawPlayer();
drawAliens();
} else if (isKeyCurrentlyPressed(VK_ESCAPE)) {
gameState = MAIN_MENU;
menuSelection = 0;
showMainMenu();
} else if (isKeyCurrentlyPressed('Q')) {
exit(0);
}
break;
case GAME_OVER:
case WIN:
if (isKeyCurrentlyPressed(VK_RETURN)) {
initializeGame();
gameState = PLAYING;
drawGameArea();
drawPlayer();
drawAliens();
} else if (isKeyCurrentlyPressed(VK_ESCAPE)) {
gameState = MAIN_MENU;
menuSelection = 0;
showMainMenu();
}
break;
}
}
void showGameOver() {
clearScreen();
myRect(250, 150, 550, 230, 255, 0, 0, 100, 0, 0);
drawGraphicsText(320, 180, "GAME OVER", 255, 255, 255);
myRect(200, 280, 600, 380, 100, 100, 100, 50, 50, 50);
drawGraphicsText(250, 300, "FINAL SCORE:", 255, 255, 255);
drawGraphicsNumber(400, 300, player.score, 255, 255, 0);
drawGraphicsText(200, 450, "PRESS ENTER - Play Again", 255, 255, 255);
drawGraphicsText(200, 480, "PRESS ESCAPE - Main Menu", 255, 255, 255);
}
void showWin() {
clearScreen();
myRect(250, 150, 550, 230, 0, 255, 0, 0, 150, 0);
drawGraphicsText(320, 180, "YOU WIN!", 255, 255, 255);
myRect(200, 280, 600, 380, 255, 255, 0, 150, 150, 0);
drawGraphicsText(250, 300, "FINAL SCORE:", 0, 0, 0);
drawGraphicsNumber(400, 300, player.score, 0, 0, 0);
drawGraphicsText(200, 450, "PRESS ENTER - Play Again", 255, 255, 255);
drawGraphicsText(200, 480, "PRESS ESCAPE - Main Menu", 255, 255, 255);
}
void run() {
setupConsole();
srand(time(0));
showMainMenu();
while (true) {
animationCounter++;
handleInput();
// Update high score
if (player.score > highScore) {
highScore = player.score;
}
if (gameState == PLAYING) {
moveAliens();
alienShoot();
updateBullets();
checkCollisions();
// REDRAW BOUNDARIES EVERY FRAME
myRect(50, 50, SCREEN_WIDTH - 50, SCREEN_HEIGHT - 50, 255, 255, 255);
myRect(51, 51, SCREEN_WIDTH - 51, SCREEN_HEIGHT - 51, 255, 255, 255);
myRect(52, 52, SCREEN_WIDTH - 52, SCREEN_HEIGHT - 52, 255, 255, 255);
if (gameState == GAME_OVER) {
showGameOver();
} else if (gameState == WIN) {
showWin();
}
} else if (gameState == MAIN_MENU && animationCounter % 3 == 0) {
// Refresh main menu for animations
showMainMenu();
}
Sleep(50);
}
}
};
int main() {
try {
SpaceInvaders game;
game.run();
}
catch (const std::exception& e) {
cout << "Error: " << e.what() << endl;
system("pause");
}
return 0;
}