-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
524 lines (456 loc) · 14.8 KB
/
Copy pathmain.cpp
File metadata and controls
524 lines (456 loc) · 14.8 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
#include "raylib.h"
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
// Game constants
const int SCREEN_WIDTH = 1280;
const int SCREEN_HEIGHT = 720;
const float PLAYER_SPEED = 300.0f;
const float PLAYER_SIZE = 40.0f;
const float ENEMY_SPEED = 100.0f;
const float ENEMY_SIZE = 30.0f;
const float BULLET_SPEED = 400.0f;
const float ENEMY_BULLET_SPEED = 200.0f;
const float SPAWN_INTERVAL = 1.5f;
// Game states
enum GameState {
MENU,
PLAYING,
PAUSED,
GAME_OVER
};
// Player structure
struct Player {
Vector2 pos;
Vector2 vel;
float size;
int health;
int score;
float shootCooldown;
};
// Bullet structure
struct Bullet {
Vector2 pos;
Vector2 vel;
float radius;
bool active;
};
// Enemy structure
struct Enemy {
Vector2 pos;
Vector2 vel;
float size;
int health;
float shootCooldown;
float shootInterval;
};
// Power-up structure
struct PowerUp {
Vector2 pos;
Vector2 vel;
float size;
int type; // 0=health, 1=rapid_fire, 2=shield
bool active;
float duration;
};
// Global game state
GameState gameState = MENU;
Player player;
std::vector<Enemy> enemies;
std::vector<Bullet> playerBullets;
std::vector<Bullet> enemyBullets;
std::vector<PowerUp> powerUps;
float spawnTimer = 0.0f;
int waveNumber = 1;
int enemiesKilled = 0;
bool rapidFireActive = false;
float rapidFireDuration = 0.0f;
bool shieldActive = false;
float shieldDuration = 0.0f;
int highScore = 0;
bool waveStarted = false;
// Function declarations
void InitGame();
void UpdateGame(float dt);
void DrawGame();
void HandleInput();
void SpawnEnemy();
void UpdatePlayer(float dt);
void UpdateEnemies(float dt);
void UpdateBullets(float dt);
void UpdatePowerUps(float dt);
void CheckCollisions();
void DrawMenu();
void DrawGameOver();
void DrawHUD();
void DrawPlayerShield();
void SpawnPowerUp(Vector2 pos);
int main() {
srand((unsigned int)time(NULL));
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Space Invaders 2");
SetTargetFPS(60);
InitGame();
while (!WindowShouldClose()) {
float dt = GetFrameTime();
HandleInput();
if (gameState == PLAYING) {
UpdateGame(dt);
}
BeginDrawing();
ClearBackground(BLACK);
if (gameState == MENU) {
DrawMenu();
} else if (gameState == PLAYING || gameState == PAUSED) {
DrawGame();
if (gameState == PAUSED) {
DrawText("PAUSED", SCREEN_WIDTH / 2 - 80, SCREEN_HEIGHT / 2 - 50, 60, YELLOW);
DrawText("Press P to Resume", SCREEN_WIDTH / 2 - 150, SCREEN_HEIGHT / 2 + 50, 30, YELLOW);
}
} else if (gameState == GAME_OVER) {
DrawGameOver();
}
EndDrawing();
}
CloseWindow();
return 0;
}
void InitGame() {
player.pos = {SCREEN_WIDTH / 2.0f, SCREEN_HEIGHT - 80.0f};
player.vel = {0, 0};
player.size = PLAYER_SIZE;
player.health = 3;
player.score = 0;
player.shootCooldown = 0.0f;
enemies.clear();
playerBullets.clear();
enemyBullets.clear();
powerUps.clear();
spawnTimer = 0.0f;
waveNumber = 1;
enemiesKilled = 0;
rapidFireActive = false;
rapidFireDuration = 0.0f;
shieldActive = false;
shieldDuration = 0.0f;
waveStarted = false;
gameState = MENU;
}
void HandleInput() {
if (IsKeyPressed(KEY_ESCAPE)) {
if (gameState == PLAYING) {
gameState = MENU;
}
}
if (gameState == PLAYING) {
if (IsKeyPressed(KEY_P)) {
gameState = PAUSED;
}
} else if (gameState == PAUSED) {
if (IsKeyPressed(KEY_P)) {
gameState = PLAYING;
}
} else if (gameState == MENU) {
if (IsKeyPressed(KEY_SPACE)) {
gameState = PLAYING;
InitGame();
gameState = PLAYING;
}
} else if (gameState == GAME_OVER) {
if (IsKeyPressed(KEY_SPACE)) {
InitGame();
}
}
}
void UpdateGame(float dt) {
UpdatePlayer(dt);
UpdateEnemies(dt);
UpdateBullets(dt);
UpdatePowerUps(dt);
CheckCollisions();
// Spawn enemies
spawnTimer += dt;
if (spawnTimer >= SPAWN_INTERVAL && enemies.size() < 5 + waveNumber) {
SpawnEnemy();
spawnTimer = 0.0f;
waveStarted = true;
}
// Check if wave is complete (only after wave has started)
if (waveStarted && enemies.empty() && enemyBullets.empty()) {
waveNumber++;
spawnTimer = 0.0f;
waveStarted = false;
}
// Update power-up durations
if (rapidFireActive) {
rapidFireDuration -= dt;
if (rapidFireDuration <= 0) {
rapidFireActive = false;
}
}
if (shieldActive) {
shieldDuration -= dt;
if (shieldDuration <= 0) {
shieldActive = false;
}
}
}
void UpdatePlayer(float dt) {
// Movement input
player.vel.x = 0;
if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)) {
player.vel.x = -PLAYER_SPEED;
}
if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) {
player.vel.x = PLAYER_SPEED;
}
// Update position
player.pos.x += player.vel.x * dt;
// Boundary check
if (player.pos.x - player.size / 2 < 0) {
player.pos.x = player.size / 2;
}
if (player.pos.x + player.size / 2 > SCREEN_WIDTH) {
player.pos.x = SCREEN_WIDTH - player.size / 2;
}
// Shooting
player.shootCooldown -= dt;
if (IsKeyDown(KEY_SPACE)) {
float shootInterval = rapidFireActive ? 0.1f : 0.3f;
if (player.shootCooldown <= 0) {
Bullet b;
b.pos = player.pos;
b.vel = {0, -BULLET_SPEED};
b.radius = 5.0f;
b.active = true;
playerBullets.push_back(b);
player.shootCooldown = shootInterval;
}
}
}
void UpdateEnemies(float dt) {
for (auto& enemy : enemies) {
// Update position
enemy.pos.x += enemy.vel.x * dt;
enemy.pos.y += enemy.vel.y * dt;
// Bounce off walls
if (enemy.pos.x - enemy.size / 2 < 0 || enemy.pos.x + enemy.size / 2 > SCREEN_WIDTH) {
enemy.vel.x *= -1;
}
// Shooting
enemy.shootCooldown -= dt;
if (enemy.shootCooldown <= 0) {
Bullet b;
b.pos = enemy.pos;
b.vel = {0, ENEMY_BULLET_SPEED};
b.radius = 4.0f;
b.active = true;
enemyBullets.push_back(b);
enemy.shootCooldown = enemy.shootInterval;
}
}
}
void UpdateBullets(float dt) {
// Player bullets
for (auto& bullet : playerBullets) {
if (bullet.active) {
bullet.pos.y += bullet.vel.y * dt;
if (bullet.pos.y < 0) {
bullet.active = false;
}
}
}
// Enemy bullets
for (auto& bullet : enemyBullets) {
if (bullet.active) {
bullet.pos.y += bullet.vel.y * dt;
if (bullet.pos.y > SCREEN_HEIGHT) {
bullet.active = false;
}
}
}
}
void UpdatePowerUps(float dt) {
for (auto& powerUp : powerUps) {
if (powerUp.active) {
powerUp.pos.y += powerUp.vel.y * dt;
if (powerUp.pos.y > SCREEN_HEIGHT) {
powerUp.active = false;
}
}
}
}
void CheckCollisions() {
// Player bullets vs enemies
for (auto& playerBullet : playerBullets) {
if (!playerBullet.active) continue;
for (auto& enemy : enemies) {
Vector2 diff = {playerBullet.pos.x - enemy.pos.x, playerBullet.pos.y - enemy.pos.y};
float dist = sqrt(diff.x * diff.x + diff.y * diff.y);
if (dist < playerBullet.radius + enemy.size / 2) {
playerBullet.active = false;
enemy.health--;
if (enemy.health <= 0) {
player.score += 100 * waveNumber;
enemiesKilled++;
SpawnPowerUp(enemy.pos);
enemy.pos.x = -1000; // Mark for removal
}
break;
}
}
}
// Remove dead enemies
enemies.erase(
std::remove_if(enemies.begin(), enemies.end(),
[](const Enemy& e) { return e.pos.x < 0; }),
enemies.end()
);
// Enemy bullets vs player
for (auto& enemyBullet : enemyBullets) {
if (!enemyBullet.active) continue;
Vector2 diff = {enemyBullet.pos.x - player.pos.x, enemyBullet.pos.y - player.pos.y};
float dist = sqrt(diff.x * diff.x + diff.y * diff.y);
if (dist < enemyBullet.radius + player.size / 2) {
enemyBullet.active = false;
if (!shieldActive) {
player.health--;
if (player.health <= 0) {
gameState = GAME_OVER;
if (player.score > highScore) {
highScore = player.score;
}
}
} else {
shieldDuration -= 0.5f;
}
}
}
// Player vs power-ups
for (auto& powerUp : powerUps) {
if (!powerUp.active) continue;
Vector2 diff = {powerUp.pos.x - player.pos.x, powerUp.pos.y - player.pos.y};
float dist = sqrt(diff.x * diff.x + diff.y * diff.y);
if (dist < powerUp.size / 2 + player.size / 2) {
powerUp.active = false;
if (powerUp.type == 0) { // Health
player.health = (player.health < 5) ? player.health + 1 : 5;
} else if (powerUp.type == 1) { // Rapid fire
rapidFireActive = true;
rapidFireDuration = 10.0f;
} else if (powerUp.type == 2) { // Shield
shieldActive = true;
shieldDuration = 15.0f;
}
}
}
}
void SpawnEnemy() {
Enemy enemy;
enemy.pos.x = GetRandomValue(50, SCREEN_WIDTH - 50);
enemy.pos.y = GetRandomValue(20, 100);
enemy.vel.x = (GetRandomValue(0, 1) == 0 ? 1 : -1) * ENEMY_SPEED;
enemy.vel.y = 0;
enemy.size = ENEMY_SIZE;
enemy.health = 1 + waveNumber / 3;
enemy.shootCooldown = GetRandomValue(1, 3);
enemy.shootInterval = 2.0f - (waveNumber * 0.1f);
if (enemy.shootInterval < 0.5f) enemy.shootInterval = 0.5f;
enemies.push_back(enemy);
}
void SpawnPowerUp(Vector2 pos) {
if (GetRandomValue(0, 100) < 30) { // 30% spawn chance
PowerUp powerUp;
powerUp.pos = pos;
powerUp.vel = {0, 100.0f};
powerUp.size = 20.0f;
powerUp.type = GetRandomValue(0, 2);
powerUp.active = true;
powerUp.duration = 10.0f;
powerUps.push_back(powerUp);
}
}
void DrawGame() {
// Draw stars background
for (int i = 0; i < 100; i++) {
int x = (i * 137) % SCREEN_WIDTH;
int y = (i * 73) % SCREEN_HEIGHT;
DrawPixel(x, y, WHITE);
}
// Draw player
DrawRectangleLines(player.pos.x - player.size / 2, player.pos.y - player.size / 2,
player.size, player.size, GREEN);
DrawTriangle({player.pos.x - 5, player.pos.y - player.size / 2},
{player.pos.x + 5, player.pos.y - player.size / 2},
{player.pos.x, player.pos.y - player.size / 2 - 10}, GREEN);
if (shieldActive) {
DrawPlayerShield();
}
// Draw player bullets
for (const auto& bullet : playerBullets) {
if (bullet.active) {
DrawCircle(bullet.pos.x, bullet.pos.y, bullet.radius, YELLOW);
}
}
// Draw enemies
for (const auto& enemy : enemies) {
DrawRectangleLines(enemy.pos.x - enemy.size / 2, enemy.pos.y - enemy.size / 2,
enemy.size, enemy.size, RED);
DrawTriangle({enemy.pos.x - 5, enemy.pos.y + enemy.size / 2},
{enemy.pos.x + 5, enemy.pos.y + enemy.size / 2},
{enemy.pos.x, enemy.pos.y + enemy.size / 2 + 8}, RED);
}
// Draw enemy bullets
for (const auto& bullet : enemyBullets) {
if (bullet.active) {
DrawCircle(bullet.pos.x, bullet.pos.y, bullet.radius, ORANGE);
}
}
// Draw power-ups
for (const auto& powerUp : powerUps) {
if (powerUp.active) {
Color color;
if (powerUp.type == 0) color = RED;
else if (powerUp.type == 1) color = YELLOW;
else color = BLUE;
DrawRectangle(powerUp.pos.x - powerUp.size / 2, powerUp.pos.y - powerUp.size / 2,
powerUp.size, powerUp.size, color);
DrawRectangleLines(powerUp.pos.x - powerUp.size / 2, powerUp.pos.y - powerUp.size / 2,
powerUp.size, powerUp.size, WHITE);
}
}
DrawHUD();
}
void DrawPlayerShield() {
float shieldRadius = player.size / 2 + 20;
DrawCircleLines(player.pos.x, player.pos.y, shieldRadius, BLUE);
DrawCircleLines(player.pos.x, player.pos.y, shieldRadius - 3, {173, 216, 230, 255});
}
void DrawHUD() {
DrawText(TextFormat("Health: %d", player.health), 10, 10, 20, GREEN);
DrawText(TextFormat("Score: %d", player.score), 10, 40, 20, YELLOW);
DrawText(TextFormat("Wave: %d", waveNumber), SCREEN_WIDTH - 200, 10, 20, {0, 255, 255, 255});
DrawText(TextFormat("Enemies: %lu", enemies.size()), SCREEN_WIDTH - 200, 40, 20, {0, 255, 255, 255});
if (rapidFireActive) {
DrawText(TextFormat("RAPID FIRE: %.1f", rapidFireDuration), 10, SCREEN_HEIGHT - 30, 20, YELLOW);
}
if (shieldActive) {
DrawText(TextFormat("SHIELD: %.1f", shieldDuration), SCREEN_WIDTH - 250, SCREEN_HEIGHT - 30, 20, BLUE);
}
}
void DrawMenu() {
DrawText("SPACE INVADERS 2", SCREEN_WIDTH / 2 - 200, SCREEN_HEIGHT / 2 - 200, 60, YELLOW);
DrawText("Press SPACE to Start", SCREEN_WIDTH / 2 - 150, SCREEN_HEIGHT / 2, 30, WHITE);
DrawText("Arrow Keys or A/D to Move", SCREEN_WIDTH / 2 - 200, SCREEN_HEIGHT / 2 + 60, 20, GRAY);
DrawText("SPACE to Shoot", SCREEN_WIDTH / 2 - 200, SCREEN_HEIGHT / 2 + 100, 20, GRAY);
DrawText("P to Pause, ESC to Quit", SCREEN_WIDTH / 2 - 200, SCREEN_HEIGHT / 2 + 140, 20, GRAY);
if (highScore > 0) {
DrawText(TextFormat("High Score: %d", highScore), SCREEN_WIDTH / 2 - 150, SCREEN_HEIGHT / 2 + 240, 25, LIME);
}
}
void DrawGameOver() {
DrawText("GAME OVER", SCREEN_WIDTH / 2 - 150, SCREEN_HEIGHT / 2 - 100, 60, RED);
DrawText(TextFormat("Final Score: %d", player.score), SCREEN_WIDTH / 2 - 180, SCREEN_HEIGHT / 2, 30, YELLOW);
DrawText(TextFormat("Wave Reached: %d", waveNumber), SCREEN_WIDTH / 2 - 180, SCREEN_HEIGHT / 2 + 60, 30, YELLOW);
DrawText("Press SPACE to Return to Menu", SCREEN_WIDTH / 2 - 200, SCREEN_HEIGHT / 2 + 150, 25, WHITE);
}