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.

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