-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
asteroids_survival.c
375 lines (311 loc) · 13.8 KB
/
asteroids_survival.c
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
/*******************************************************************************************
*
* raylib - classic game: asteroids survival
*
* Sample game developed by Ian Eito, Albert Martos and Ramon Santamaria
*
* This game has been created using raylib v1.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include <math.h>
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
//----------------------------------------------------------------------------------
// Some Defines
//----------------------------------------------------------------------------------
#define PLAYER_BASE_SIZE 20.0f
#define PLAYER_SPEED 6.0f
#define PLAYER_MAX_SHOOTS 10
#define METEORS_SPEED 2
#define MAX_MEDIUM_METEORS 8
#define MAX_SMALL_METEORS 16
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
typedef struct Player {
Vector2 position;
Vector2 speed;
float acceleration;
float rotation;
Vector3 collider;
Color color;
} Player;
typedef struct Meteor {
Vector2 position;
Vector2 speed;
float radius;
bool active;
Color color;
} Meteor;
//------------------------------------------------------------------------------------
// Global Variables Declaration
//------------------------------------------------------------------------------------
static const int screenWidth = 800;
static const int screenHeight = 450;
static int framesCounter = 0;
static bool gameOver = false;
static bool pause = false;
// NOTE: Defined triangle is isosceles with common angles of 70 degrees.
static float shipHeight = 0.0f;
static Player player = { 0 };
static Meteor mediumMeteor[MAX_MEDIUM_METEORS] = { 0 };
static Meteor smallMeteor[MAX_SMALL_METEORS] = { 0 };
//------------------------------------------------------------------------------------
// Module Functions Declaration (local)
//------------------------------------------------------------------------------------
static void InitGame(void); // Initialize game
static void UpdateGame(void); // Update game (one frame)
static void DrawGame(void); // Draw game (one frame)
static void UnloadGame(void); // Unload game
static void UpdateDrawFrame(void); // Update and Draw (one frame)
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization (Note windowTitle is unused on Android)
//---------------------------------------------------------
InitWindow(screenWidth, screenHeight, "classic game: asteroids survival");
InitGame();
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 60, 1);
#else
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update and Draw
//----------------------------------------------------------------------------------
UpdateDrawFrame();
//----------------------------------------------------------------------------------
}
#endif
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadGame(); // Unload loaded data (textures, sounds, models...)
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//------------------------------------------------------------------------------------
// Module Functions Definitions (local)
//------------------------------------------------------------------------------------
// Initialize game variables
void InitGame(void)
{
int posx, posy;
int velx, vely;
bool correctRange = false;
pause = false;
framesCounter = 0;
shipHeight = (PLAYER_BASE_SIZE/2)/tanf(20*DEG2RAD);
// Initialization player
player.position = (Vector2){screenWidth/2, screenHeight/2 - shipHeight/2};
player.speed = (Vector2){0, 0};
player.acceleration = 0;
player.rotation = 0;
player.collider = (Vector3){player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight/2.5f), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight/2.5f), 12};
player.color = LIGHTGRAY;
for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
{
posx = GetRandomValue(0, screenWidth);
while (!correctRange)
{
if (posx > screenWidth/2 - 150 && posx < screenWidth/2 + 150) posx = GetRandomValue(0, screenWidth);
else correctRange = true;
}
correctRange = false;
posy = GetRandomValue(0, screenHeight);
while (!correctRange)
{
if (posy > screenHeight/2 - 150 && posy < screenHeight/2 + 150) posy = GetRandomValue(0, screenHeight);
else correctRange = true;
}
correctRange = false;
velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
while (!correctRange)
{
if (velx == 0 && vely == 0)
{
velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
}
else correctRange = true;
}
mediumMeteor[i].position = (Vector2){posx, posy};
mediumMeteor[i].speed = (Vector2){velx, vely};
mediumMeteor[i].radius = 20;
mediumMeteor[i].active = true;
mediumMeteor[i].color = GREEN;
}
for (int i = 0; i < MAX_SMALL_METEORS; i++)
{
posx = GetRandomValue(0, screenWidth);
while (!correctRange)
{
if (posx > screenWidth/2 - 150 && posx < screenWidth/2 + 150) posx = GetRandomValue(0, screenWidth);
else correctRange = true;
}
correctRange = false;
posy = GetRandomValue(0, screenHeight);
while (!correctRange)
{
if (posy > screenHeight/2 - 150 && posy < screenHeight/2 + 150) posy = GetRandomValue(0, screenHeight);
else correctRange = true;
}
correctRange = false;
velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
while (!correctRange)
{
if (velx == 0 && vely == 0)
{
velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
}
else correctRange = true;
}
smallMeteor[i].position = (Vector2){posx, posy};
smallMeteor[i].speed = (Vector2){velx, vely};
smallMeteor[i].radius = 10;
smallMeteor[i].active = true;
smallMeteor[i].color = YELLOW;
}
}
// Update game (one frame)
void UpdateGame(void)
{
if (!gameOver)
{
if (IsKeyPressed('P')) pause = !pause;
if (!pause)
{
framesCounter++;
// Player logic
// Rotation
if (IsKeyDown(KEY_LEFT)) player.rotation -= 5;
if (IsKeyDown(KEY_RIGHT)) player.rotation += 5;
// Speed
player.speed.x = sin(player.rotation*DEG2RAD)*PLAYER_SPEED;
player.speed.y = cos(player.rotation*DEG2RAD)*PLAYER_SPEED;
// Controller
if (IsKeyDown(KEY_UP))
{
if (player.acceleration < 1) player.acceleration += 0.04f;
}
else
{
if (player.acceleration > 0) player.acceleration -= 0.02f;
else if (player.acceleration < 0) player.acceleration = 0;
}
if (IsKeyDown(KEY_DOWN))
{
if (player.acceleration > 0) player.acceleration -= 0.04f;
else if (player.acceleration < 0) player.acceleration = 0;
}
// Movement
player.position.x += (player.speed.x*player.acceleration);
player.position.y -= (player.speed.y*player.acceleration);
// Wall behaviour for player
if (player.position.x > screenWidth + shipHeight) player.position.x = -(shipHeight);
else if (player.position.x < -(shipHeight)) player.position.x = screenWidth + shipHeight;
if (player.position.y > (screenHeight + shipHeight)) player.position.y = -(shipHeight);
else if (player.position.y < -(shipHeight)) player.position.y = screenHeight + shipHeight;
// Collision Player to meteors
player.collider = (Vector3){player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight/2.5f), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight/2.5f), 12};
for (int a = 0; a < MAX_MEDIUM_METEORS; a++)
{
if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, mediumMeteor[a].position, mediumMeteor[a].radius) && mediumMeteor[a].active) gameOver = true;
}
for (int a = 0; a < MAX_SMALL_METEORS; a++)
{
if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, smallMeteor[a].position, smallMeteor[a].radius) && smallMeteor[a].active) gameOver = true;
}
// Meteor logic
for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
{
if (mediumMeteor[i].active)
{
// movement
mediumMeteor[i].position.x += mediumMeteor[i].speed.x;
mediumMeteor[i].position.y += mediumMeteor[i].speed.y;
// wall behaviour
if (mediumMeteor[i].position.x > screenWidth + mediumMeteor[i].radius) mediumMeteor[i].position.x = -(mediumMeteor[i].radius);
else if (mediumMeteor[i].position.x < 0 - mediumMeteor[i].radius) mediumMeteor[i].position.x = screenWidth + mediumMeteor[i].radius;
if (mediumMeteor[i].position.y > screenHeight + mediumMeteor[i].radius) mediumMeteor[i].position.y = -(mediumMeteor[i].radius);
else if (mediumMeteor[i].position.y < 0 - mediumMeteor[i].radius) mediumMeteor[i].position.y = screenHeight + mediumMeteor[i].radius;
}
}
for (int i = 0; i < MAX_SMALL_METEORS; i++)
{
if (smallMeteor[i].active)
{
// movement
smallMeteor[i].position.x += smallMeteor[i].speed.x;
smallMeteor[i].position.y += smallMeteor[i].speed.y;
// wall behaviour
if (smallMeteor[i].position.x > screenWidth + smallMeteor[i].radius) smallMeteor[i].position.x = -(smallMeteor[i].radius);
else if (smallMeteor[i].position.x < 0 - smallMeteor[i].radius) smallMeteor[i].position.x = screenWidth + smallMeteor[i].radius;
if (smallMeteor[i].position.y > screenHeight + smallMeteor[i].radius) smallMeteor[i].position.y = -(smallMeteor[i].radius);
else if (smallMeteor[i].position.y < 0 - smallMeteor[i].radius) smallMeteor[i].position.y = screenHeight + smallMeteor[i].radius;
}
}
}
}
else
{
if (IsKeyPressed(KEY_ENTER))
{
InitGame();
gameOver = false;
}
}
}
// Draw game (one frame)
void DrawGame(void)
{
BeginDrawing();
ClearBackground(RAYWHITE);
if (!gameOver)
{
// Draw spaceship
Vector2 v1 = { player.position.x + sinf(player.rotation*DEG2RAD)*(shipHeight), player.position.y - cosf(player.rotation*DEG2RAD)*(shipHeight) };
Vector2 v2 = { player.position.x - cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y - sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
Vector2 v3 = { player.position.x + cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y + sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
DrawTriangle(v1, v2, v3, MAROON);
// Draw meteor
for (int i = 0;i < MAX_MEDIUM_METEORS; i++)
{
if (mediumMeteor[i].active) DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, GRAY);
else DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
}
for (int i = 0;i < MAX_SMALL_METEORS; i++)
{
if (smallMeteor[i].active) DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, DARKGRAY);
else DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
}
DrawText(TextFormat("TIME: %.02f", (float)framesCounter/60), 10, 10, 20, BLACK);
if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
}
else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// Unload game variables
void UnloadGame(void)
{
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
}
// Update and Draw (one frame)
void UpdateDrawFrame(void)
{
UpdateGame();
DrawGame();
}