1 | /******************************************************************************************* |
2 | * |
3 | * raylib - sample game: gold fever |
4 | * |
5 | * Sample game developed by Ian Eito, Albert Martos and Ramon Santamaria |
6 | * |
7 | * This game has been created using raylib v1.3 (www.raylib.com) |
8 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
9 | * |
10 | * Copyright (c) 2015 Ramon Santamaria (@raysan5) |
11 | * |
12 | ********************************************************************************************/ |
13 | |
14 | #include "raylib.h" |
15 | |
16 | #if defined(PLATFORM_WEB) |
17 | #include <emscripten/emscripten.h> |
18 | #endif |
19 | |
20 | //---------------------------------------------------------------------------------- |
21 | // Types and Structures Definition |
22 | //---------------------------------------------------------------------------------- |
23 | typedef struct Player { |
24 | Vector2 position; |
25 | Vector2 speed; |
26 | int radius; |
27 | } Player; |
28 | |
29 | typedef struct Enemy { |
30 | Vector2 position; |
31 | Vector2 speed; |
32 | int radius; |
33 | int radiusBounds; |
34 | bool moveRight; |
35 | } Enemy; |
36 | |
37 | typedef struct Points { |
38 | Vector2 position; |
39 | int radius; |
40 | int value; |
41 | bool active; |
42 | } Points; |
43 | |
44 | typedef struct Home { |
45 | Rectangle rec; |
46 | bool active; |
47 | bool save; |
48 | Color color; |
49 | } Home; |
50 | |
51 | //------------------------------------------------------------------------------------ |
52 | // Global Variables Declaration |
53 | //------------------------------------------------------------------------------------ |
54 | static const int screenWidth = 800; |
55 | static const int screenHeight = 450; |
56 | |
57 | static bool gameOver = false; |
58 | static bool pause = false; |
59 | static int score = 0; |
60 | static int hiScore = 0; |
61 | |
62 | static Player player = { 0 }; |
63 | static Enemy enemy = { 0 }; |
64 | static Points points = { 0 }; |
65 | static Home home = { 0 }; |
66 | static bool follow = false; |
67 | |
68 | //------------------------------------------------------------------------------------ |
69 | // Module Functions Declaration (local) |
70 | //------------------------------------------------------------------------------------ |
71 | static void InitGame(void); // Initialize game |
72 | static void UpdateGame(void); // Update game (one frame) |
73 | static void DrawGame(void); // Draw game (one frame) |
74 | static void UnloadGame(void); // Unload game |
75 | static void UpdateDrawFrame(void); // Update and Draw (one frame) |
76 | |
77 | //------------------------------------------------------------------------------------ |
78 | // Program main entry point |
79 | //------------------------------------------------------------------------------------ |
80 | int main(void) |
81 | { |
82 | // Initialization (Note windowTitle is unused on Android) |
83 | //--------------------------------------------------------- |
84 | InitWindow(screenWidth, screenHeight, "sample game: gold fever" ); |
85 | |
86 | InitGame(); |
87 | |
88 | #if defined(PLATFORM_WEB) |
89 | emscripten_set_main_loop(UpdateDrawFrame, 0, 1); |
90 | #else |
91 | SetTargetFPS(60); |
92 | //-------------------------------------------------------------------------------------- |
93 | |
94 | // Main game loop |
95 | while (!WindowShouldClose()) // Detect window close button or ESC key |
96 | { |
97 | // Update and Draw |
98 | //---------------------------------------------------------------------------------- |
99 | UpdateDrawFrame(); |
100 | //---------------------------------------------------------------------------------- |
101 | } |
102 | #endif |
103 | // De-Initialization |
104 | //-------------------------------------------------------------------------------------- |
105 | UnloadGame(); // Unload loaded data (textures, sounds, models...) |
106 | |
107 | CloseWindow(); // Close window and OpenGL context |
108 | //-------------------------------------------------------------------------------------- |
109 | |
110 | return 0; |
111 | } |
112 | |
113 | //------------------------------------------------------------------------------------ |
114 | // Module Functions Definitions (local) |
115 | //------------------------------------------------------------------------------------ |
116 | |
117 | // Initialize game variables |
118 | void InitGame(void) |
119 | { |
120 | pause = false; |
121 | score = 0; |
122 | |
123 | player.position = (Vector2){50, 50}; |
124 | player.radius = 20; |
125 | player.speed = (Vector2){5, 5}; |
126 | |
127 | enemy.position = (Vector2){screenWidth - 50, screenHeight/2}; |
128 | enemy.radius = 20; |
129 | enemy.radiusBounds = 150; |
130 | enemy.speed = (Vector2){3, 3}; |
131 | enemy.moveRight = true; |
132 | follow = false; |
133 | |
134 | points.radius = 10; |
135 | points.position = (Vector2){GetRandomValue(points.radius, screenWidth - points.radius), GetRandomValue(points.radius, screenHeight - points.radius)}; |
136 | points.value = 100; |
137 | points.active = true; |
138 | |
139 | home.rec.width = 50; |
140 | home.rec.height = 50; |
141 | home.rec.x = GetRandomValue(0, screenWidth - home.rec.width); |
142 | home.rec.y = GetRandomValue(0, screenHeight - home.rec.height); |
143 | home.active = false; |
144 | home.save = false; |
145 | } |
146 | |
147 | // Update game (one frame) |
148 | void UpdateGame(void) |
149 | { |
150 | if (!gameOver) |
151 | { |
152 | if (IsKeyPressed('P')) pause = !pause; |
153 | |
154 | if (!pause) |
155 | { |
156 | // Control player |
157 | if (IsKeyDown(KEY_RIGHT)) player.position.x += player.speed.x; |
158 | if (IsKeyDown(KEY_LEFT)) player.position.x -= player.speed.x; |
159 | if (IsKeyDown(KEY_UP)) player.position.y -= player.speed.y; |
160 | if (IsKeyDown(KEY_DOWN)) player.position.y += player.speed.y; |
161 | |
162 | // Wall behaviour player |
163 | if (player.position.x - player.radius <= 0) player.position.x = player.radius; |
164 | if (player.position.x + player.radius >= screenWidth) player.position.x = screenWidth - player.radius; |
165 | if (player.position.y - player.radius <= 0) player.position.y = player.radius; |
166 | if (player.position.y + player.radius >= screenHeight) player.position.y = screenHeight - player.radius; |
167 | |
168 | // IA Enemy |
169 | if ( (follow || CheckCollisionCircles(player.position, player.radius, enemy.position, enemy.radiusBounds)) && !home.save) |
170 | { |
171 | if (player.position.x > enemy.position.x) enemy.position.x += enemy.speed.x; |
172 | if (player.position.x < enemy.position.x) enemy.position.x -= enemy.speed.x; |
173 | |
174 | if (player.position.y > enemy.position.y) enemy.position.y += enemy.speed.y; |
175 | if (player.position.y < enemy.position.y) enemy.position.y -= enemy.speed.y; |
176 | } |
177 | else |
178 | { |
179 | if (enemy.moveRight) enemy.position.x += enemy.speed.x; |
180 | else enemy.position.x -= enemy.speed.x; |
181 | } |
182 | |
183 | // Wall behaviour enemy |
184 | if (enemy.position.x - enemy.radius <= 0) enemy.moveRight = true; |
185 | if (enemy.position.x + enemy.radius >= screenWidth) enemy.moveRight = false; |
186 | |
187 | if (enemy.position.x - enemy.radius <= 0) enemy.position.x = enemy.radius; |
188 | if (enemy.position.x + enemy.radius >= screenWidth) enemy.position.x = screenWidth - enemy.radius; |
189 | if (enemy.position.y - enemy.radius <= 0) enemy.position.y = enemy.radius; |
190 | if (enemy.position.y + enemy.radius >= screenHeight) enemy.position.y = screenHeight - enemy.radius; |
191 | |
192 | // Collisions |
193 | if (CheckCollisionCircles(player.position, player.radius, points.position, points.radius) && points.active) |
194 | { |
195 | follow = true; |
196 | points.active = false; |
197 | home.active = true; |
198 | } |
199 | |
200 | if (CheckCollisionCircles(player.position, player.radius, enemy.position, enemy.radius) && !home.save) |
201 | { |
202 | gameOver = true; |
203 | |
204 | if (hiScore < score) hiScore = score; |
205 | } |
206 | |
207 | if (CheckCollisionCircleRec(player.position, player.radius, home.rec)) |
208 | { |
209 | follow = false; |
210 | |
211 | if (!points.active) |
212 | { |
213 | score += points.value; |
214 | points.active = true; |
215 | enemy.speed.x += 0.5; |
216 | enemy.speed.y += 0.5; |
217 | points.position = (Vector2){GetRandomValue(points.radius, screenWidth - points.radius), GetRandomValue(points.radius, screenHeight - points.radius)}; |
218 | } |
219 | |
220 | home.save = true; |
221 | } |
222 | else home.save = false; |
223 | } |
224 | } |
225 | else |
226 | { |
227 | if (IsKeyPressed(KEY_ENTER)) |
228 | { |
229 | InitGame(); |
230 | gameOver = false; |
231 | } |
232 | } |
233 | } |
234 | |
235 | // Draw game (one frame) |
236 | void DrawGame(void) |
237 | { |
238 | BeginDrawing(); |
239 | |
240 | ClearBackground(RAYWHITE); |
241 | |
242 | if (!gameOver) |
243 | { |
244 | if (follow) |
245 | { |
246 | DrawRectangle(0, 0, screenWidth, screenHeight, RED); |
247 | DrawRectangle(10, 10, screenWidth - 20, screenHeight - 20, RAYWHITE); |
248 | } |
249 | |
250 | DrawRectangleLines(home.rec.x, home.rec.y, home.rec.width, home.rec.height, BLUE); |
251 | |
252 | DrawCircleLines(enemy.position.x, enemy.position.y, enemy.radiusBounds, RED); |
253 | DrawCircleV(enemy.position, enemy.radius, MAROON); |
254 | |
255 | DrawCircleV(player.position, player.radius, GRAY); |
256 | if (points.active) DrawCircleV(points.position, points.radius, GOLD); |
257 | |
258 | DrawText(TextFormat("SCORE: %04i" , score), 20, 15, 20, GRAY); |
259 | DrawText(TextFormat("HI-SCORE: %04i" , hiScore), 300, 15, 20, GRAY); |
260 | |
261 | if (pause) DrawText("GAME PAUSED" , screenWidth/2 - MeasureText("GAME PAUSED" , 40)/2, screenHeight/2 - 40, 40, GRAY); |
262 | } |
263 | else DrawText("PRESS [ENTER] TO PLAY AGAIN" , GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN" , 20)/2, GetScreenHeight()/2 - 50, 20, GRAY); |
264 | |
265 | EndDrawing(); |
266 | } |
267 | |
268 | // Unload game variables |
269 | void UnloadGame(void) |
270 | { |
271 | // TODO: Unload all dynamic loaded data (textures, sounds, models...) |
272 | } |
273 | |
274 | // Update and Draw (one frame) |
275 | void UpdateDrawFrame(void) |
276 | { |
277 | UpdateGame(); |
278 | DrawGame(); |
279 | } |
280 | |