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.

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