-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpace_Invaders.cpp
More file actions
284 lines (252 loc) · 8.59 KB
/
Copy pathSpace_Invaders.cpp
File metadata and controls
284 lines (252 loc) · 8.59 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
#include "raylib.h"
struct Player {
Vector2 position;
float speed;
float health;
float dimension;
bool active = true;
Texture2D texture;
};
struct Bullet {
Vector2 position;
float speed;
bool active;
float radius;
float damage = 25.0f;
bool fromPlayer;
};
struct Enemy {
Vector2 position;
float speed;
float health;
bool active;
float dimension;
float damage = 25.0f;
float shootTimer = (float)GetRandomValue(1, 4);
Texture2D texture;
};
struct Explosion {
Vector2 position;
float timer;
bool active;
};
int main()
{
InitWindow(800, 800, "Space invaders");
InitAudioDevice();
const int maxEnemy = 18;
Enemy enemies[maxEnemy] = { 0 };
int enemyCount = 0;
int waveNumber = 1;
const int MAX_BULLETS = 20000;
Bullet bullets[MAX_BULLETS] = { 0 };
Explosion explosions[300] = { 0 };
int explosionCount = 0;
// Declare all sounds
Sound blaster = LoadSound("assets/Sounds/Blaster.mp3");
Sound explosion = LoadSound("assets/Sounds/EnemyDeath.mp3");
Sound playerDeath = LoadSound("assets/Sounds/PlayerDeath.mp3");
Music backgroundMusic = LoadMusicStream("assets/Sounds/Cockpit_Audio.mp3");
SetSoundVolume(explosion, 3.5f);
PlayMusicStream(backgroundMusic);
// Load textures for player and enemies, background too
Texture2D greenAlien = LoadTexture("assets/Sprites/SpaceInvaders/greenAlien.png");
Texture2D playerTex = LoadTexture("assets/Sprites/SpaceInvaders/player.png");
Texture2D orangeAlien = LoadTexture("assets/Sprites/SpaceInvaders/orangeAlien.png");
Texture2D enemyShip = LoadTexture("assets/Sprites/SpaceInvaders/enemyShip.png");
Texture2D squid = LoadTexture("assets/Sprites/SpaceInvaders/squid.png");
Texture2D explosionTex = LoadTexture("assets/explosion.png");
Image bluePreview = LoadImage("assets/blue-preview.png");
Texture2D background = LoadTextureFromImage(bluePreview);
Player player = { {400, 600}, 6.0f, 100.0f, 50.0f, true, playerTex };
// For drawing enemies in a grid pattern
const int rows = 3;
const int cols = 6;
int index = 0;
int bulletCount = 0;
for (int i = 0; i < maxEnemy; i++) {
enemies[i].active = true;
enemies[i].health = 25.0f;
enemies[i].texture = greenAlien;
enemies[i].position = { 100.0f + i * 100.0f, 100.0f };
enemies[i].dimension = 50.0f;
enemies[i].speed = 3.5f;
enemies[i].damage = 25.0f;
enemyCount++;
}
// Grid setup
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (index < maxEnemy) {
enemies[index].position = { 100.0f + col * 100.0f, 100.0f + row * 100.0f };
index++;
}
}
}
SetTargetFPS(60);
while (!WindowShouldClose())
{
if (IsKeyDown(KEY_D) && player.position.x < 800 - player.dimension) {
player.position.x += player.speed;
}
if (IsKeyDown(KEY_A) && player.position.x > 0) {
player.position.x -= player.speed;
}
if (IsKeyPressed(KEY_SPACE)) {
bullets[bulletCount].position.x = player.position.x + player.dimension / 2;
bullets[bulletCount].position.y = player.position.y;
bullets[bulletCount].speed = 10.0f;
bullets[bulletCount].active = true;
bullets[bulletCount].radius = 5.0f;
bullets[bulletCount].fromPlayer = true;
bullets[bulletCount].damage = 25.0f;
bulletCount++;
PlaySound(blaster);
}
// Update bullets
for (int i = 0; i < bulletCount; i++) {
if (bullets[i].active) {
bullets[i].position.y -= bullets[i].speed;
if (bullets[i].position.y < 0) {
bullets[i].active = false;
bulletCount--;
}
}
}
//Decide when enemy should shoot
for (int i = 0; i < maxEnemy; i++) {
if (enemies[i].active) {
enemies[i].shootTimer -= GetFrameTime();
if (enemies[i].shootTimer <= 0) {
bullets[bulletCount].position.x = enemies[i].position.x + enemies[i].dimension / 2;
bullets[bulletCount].position.y = enemies[i].position.y + enemies[i].dimension;
bullets[bulletCount].speed = -10.0f; // Negative speed for downward movement
bullets[bulletCount].active = true;
bullets[bulletCount].radius = 5.0f;
bullets[bulletCount].fromPlayer = false;
bullets[bulletCount].damage = enemies[i].damage;
bulletCount++;
PlaySound(blaster); // Play shooting sound
enemies[i].shootTimer = 5.0f; // Reset shoot timer
}
}
}
// Delete enemy if hit by Bullet and Health = 0
for (int b = 0; b < bulletCount; b++) {
for (int e = 0; e < maxEnemy; e++) {
if (bullets[b].active && enemies[e].active && bullets[b].fromPlayer) {
if (CheckCollisionCircleRec(bullets[b].position, bullets[b].radius, { enemies[e].position.x, enemies[e].position.y, enemies[e].dimension, enemies[e].dimension })) {
bullets[b].active = false;
enemies[e].health -= bullets[b].damage;
if (enemies[e].health <= 0) {
enemies[e].active = false;
enemyCount--;
player.health += 10.0f; // Increase player health by 10 points
// Animated Explosion
explosions[explosionCount].position = { enemies[e].position.x + enemies[e].dimension / 2,
enemies[e].position.y + enemies[e].dimension / 2 };
explosions[explosionCount].timer = 0.3f; // lasts 0.3 seconds
explosions[explosionCount].active = true;
explosionCount++;
PlaySound(explosion);
}
}
}
}
}
// Handle wave progression
if (enemyCount <= 0 && waveNumber < 4) {
waveNumber++;
for (int i = 0; i < maxEnemy; i++) {
enemies[i].active = true;
enemies[i].position = { 100.0f + (i % cols) * 100.0f, 100.0f + (i / cols) * 100.0f };
enemies[i].speed = 3.5f + (waveNumber - 1) * 0.5f; // Increase speed with waves
enemies[i].shootTimer = (float)GetRandomValue(1, 4);
// Update health and texture based on wave
if (waveNumber == 2) {
enemies[i].health = 50.0f;
enemies[i].texture = orangeAlien;
enemies[i].damage = 50.0f; // Increase damage for wave 2
}
else if (waveNumber == 3) {
enemies[i].health = 75.0f;
enemies[i].texture = enemyShip;
enemies[i].damage = 75.0f; // Increase damage for wave 3
}
else if (waveNumber == 4) {
enemies[i].health = 100.0f;
enemies[i].texture = squid;
enemies[i].damage = 100.0f; // Increase damage for wave 4
}
}
enemyCount = maxEnemy;
}
for (int i = 0; i < bulletCount; i++) {
if (bullets[i].active && !bullets[i].fromPlayer) {
if (CheckCollisionCircleRec(bullets[i].position, bullets[i].radius, { player.position.x, player.position.y, player.dimension, player.dimension })) {
bullets[i].active = false;
player.health -= bullets[i].damage;
if (player.health <= 0) {
player.active = false;
PlaySound(playerDeath);
for (int j = 0; j < maxEnemy; j++) {
enemies[j].active = false;
}
}
}
}
}
// Make enemies move
for (int i = 0; i < maxEnemy; i++) {
if (enemies[i].active) {
enemies[i].position.x += enemies[i].speed;
if (enemies[i].position.x <= 0 || enemies[i].position.x + enemies[i].dimension >= 800) {
enemies[i].speed *= -1; // Change direction
enemies[i].position.y += 10; // Move down when changing direction, to create urgency
}
}
}
BeginDrawing();
ClearBackground(BLACK);
DrawTextureEx(background, { 0, 0 }, 0.0f,2.0, WHITE);
UpdateMusicStream(backgroundMusic);
DrawText(TextFormat("Player Health: %.0f", player.health), 10, 10, 20, GREEN);
if (player.active) {
DrawTextureEx(player.texture, player.position, 0.0f, 1.5f, WHITE);
for (int i = 0; i < maxEnemy; i++) {
if (enemies[i].active) {
DrawTextureEx(enemies[i].texture, enemies[i].position, 0.0f, 1.5f, WHITE);
}
}
// Draw bullets
for (int i = 0; i < bulletCount; i++) {
if (bullets[i].active) {
DrawCircle(bullets[i].position.x, bullets[i].position.y, bullets[i].radius, RED);
}
}
for (int i = 0; i < explosionCount; i++) {
if (explosions[i].active) {
explosions[i].timer -= GetFrameTime();
if (explosions[i].timer > 0) {
DrawTextureEx(explosionTex, explosions[i].position, 0.0f, 0.1f, WHITE);
}
else {
explosions[i].active = false;
}
}
}
}
else {
DrawText("Game Over", 350, 400, 20, RED);
}
EndDrawing();
}
// Cleanup audio
UnloadSound(blaster);
UnloadSound(explosion);
UnloadSound(playerDeath);
UnloadMusicStream(backgroundMusic);
CloseAudioDevice();
CloseWindow();
return 0;
}