You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
9.8 KiB

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