-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
gold_fever.c
279 lines (233 loc) · 9.48 KB
/
gold_fever.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
/*******************************************************************************************
*
* raylib - classic game: gold fever
*
* 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"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
typedef struct Player {
Vector2 position;
Vector2 speed;
int radius;
} Player;
typedef struct Enemy {
Vector2 position;
Vector2 speed;
int radius;
int radiusBounds;
bool moveRight;
} Enemy;
typedef struct Points {
Vector2 position;
int radius;
int value;
bool active;
} Points;
typedef struct Home {
Rectangle rec;
bool active;
bool save;
Color color;
} Home;
//------------------------------------------------------------------------------------
// Global Variables Declaration
//------------------------------------------------------------------------------------
static const int screenWidth = 800;
static const int screenHeight = 450;
static bool gameOver = false;
static bool pause = false;
static int score = 0;
static int hiScore = 0;
static Player player = { 0 };
static Enemy enemy = { 0 };
static Points points = { 0 };
static Home home = { 0 };
static bool follow = false;
//------------------------------------------------------------------------------------
// 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: gold fever");
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)
{
pause = false;
score = 0;
player.position = (Vector2){50, 50};
player.radius = 20;
player.speed = (Vector2){5, 5};
enemy.position = (Vector2){screenWidth - 50, screenHeight/2};
enemy.radius = 20;
enemy.radiusBounds = 150;
enemy.speed = (Vector2){3, 3};
enemy.moveRight = true;
follow = false;
points.radius = 10;
points.position = (Vector2){GetRandomValue(points.radius, screenWidth - points.radius), GetRandomValue(points.radius, screenHeight - points.radius)};
points.value = 100;
points.active = true;
home.rec.width = 50;
home.rec.height = 50;
home.rec.x = GetRandomValue(0, screenWidth - home.rec.width);
home.rec.y = GetRandomValue(0, screenHeight - home.rec.height);
home.active = false;
home.save = false;
}
// Update game (one frame)
void UpdateGame(void)
{
if (!gameOver)
{
if (IsKeyPressed('P')) pause = !pause;
if (!pause)
{
// Control player
if (IsKeyDown(KEY_RIGHT)) player.position.x += player.speed.x;
if (IsKeyDown(KEY_LEFT)) player.position.x -= player.speed.x;
if (IsKeyDown(KEY_UP)) player.position.y -= player.speed.y;
if (IsKeyDown(KEY_DOWN)) player.position.y += player.speed.y;
// Wall behaviour player
if (player.position.x - player.radius <= 0) player.position.x = player.radius;
if (player.position.x + player.radius >= screenWidth) player.position.x = screenWidth - player.radius;
if (player.position.y - player.radius <= 0) player.position.y = player.radius;
if (player.position.y + player.radius >= screenHeight) player.position.y = screenHeight - player.radius;
// IA Enemy
if ( (follow || CheckCollisionCircles(player.position, player.radius, enemy.position, enemy.radiusBounds)) && !home.save)
{
if (player.position.x > enemy.position.x) enemy.position.x += enemy.speed.x;
if (player.position.x < enemy.position.x) enemy.position.x -= enemy.speed.x;
if (player.position.y > enemy.position.y) enemy.position.y += enemy.speed.y;
if (player.position.y < enemy.position.y) enemy.position.y -= enemy.speed.y;
}
else
{
if (enemy.moveRight) enemy.position.x += enemy.speed.x;
else enemy.position.x -= enemy.speed.x;
}
// Wall behaviour enemy
if (enemy.position.x - enemy.radius <= 0) enemy.moveRight = true;
if (enemy.position.x + enemy.radius >= screenWidth) enemy.moveRight = false;
if (enemy.position.x - enemy.radius <= 0) enemy.position.x = enemy.radius;
if (enemy.position.x + enemy.radius >= screenWidth) enemy.position.x = screenWidth - enemy.radius;
if (enemy.position.y - enemy.radius <= 0) enemy.position.y = enemy.radius;
if (enemy.position.y + enemy.radius >= screenHeight) enemy.position.y = screenHeight - enemy.radius;
// Collisions
if (CheckCollisionCircles(player.position, player.radius, points.position, points.radius) && points.active)
{
follow = true;
points.active = false;
home.active = true;
}
if (CheckCollisionCircles(player.position, player.radius, enemy.position, enemy.radius) && !home.save)
{
gameOver = true;
if (hiScore < score) hiScore = score;
}
if (CheckCollisionCircleRec(player.position, player.radius, home.rec))
{
follow = false;
if (!points.active)
{
score += points.value;
points.active = true;
enemy.speed.x += 0.5;
enemy.speed.y += 0.5;
points.position = (Vector2){GetRandomValue(points.radius, screenWidth - points.radius), GetRandomValue(points.radius, screenHeight - points.radius)};
}
home.save = true;
}
else home.save = false;
}
}
else
{
if (IsKeyPressed(KEY_ENTER))
{
InitGame();
gameOver = false;
}
}
}
// Draw game (one frame)
void DrawGame(void)
{
BeginDrawing();
ClearBackground(RAYWHITE);
if (!gameOver)
{
if (follow)
{
DrawRectangle(0, 0, screenWidth, screenHeight, RED);
DrawRectangle(10, 10, screenWidth - 20, screenHeight - 20, RAYWHITE);
}
DrawRectangleLines(home.rec.x, home.rec.y, home.rec.width, home.rec.height, BLUE);
DrawCircleLines(enemy.position.x, enemy.position.y, enemy.radiusBounds, RED);
DrawCircleV(enemy.position, enemy.radius, MAROON);
DrawCircleV(player.position, player.radius, GRAY);
if (points.active) DrawCircleV(points.position, points.radius, GOLD);
DrawText(TextFormat("SCORE: %04i", score), 20, 15, 20, GRAY);
DrawText(TextFormat("HI-SCORE: %04i", hiScore), 300, 15, 20, GRAY);
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();
}