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.

292 lines
9.7 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib - sample game: snake
  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. // Some Defines
  19. //----------------------------------------------------------------------------------
  20. #define SNAKE_LENGTH 256
  21. #define SQUARE_SIZE 31
  22. //----------------------------------------------------------------------------------
  23. // Types and Structures Definition
  24. //----------------------------------------------------------------------------------
  25. typedef struct Snake {
  26. Vector2 position;
  27. Vector2 size;
  28. Vector2 speed;
  29. Color color;
  30. } Snake;
  31. typedef struct Food {
  32. Vector2 position;
  33. Vector2 size;
  34. bool active;
  35. Color color;
  36. } Food;
  37. //------------------------------------------------------------------------------------
  38. // Global Variables Declaration
  39. //------------------------------------------------------------------------------------
  40. static int screenWidth = 800;
  41. static int screenHeight = 450;
  42. static int framesCounter;
  43. static bool gameOver;
  44. static bool pause;
  45. static Food fruit;
  46. static Snake snake[SNAKE_LENGTH];
  47. static Vector2 snakePosition[SNAKE_LENGTH];
  48. static bool allowMove;
  49. static Vector2 offset;
  50. static int counterTail;
  51. //------------------------------------------------------------------------------------
  52. // Module Functions Declaration (local)
  53. //------------------------------------------------------------------------------------
  54. static void InitGame(void); // Initialize game
  55. static void UpdateGame(void); // Update game (one frame)
  56. static void DrawGame(void); // Draw game (one frame)
  57. static void UnloadGame(void); // Unload game
  58. static void UpdateDrawFrame(void); // Update and Draw (one frame)
  59. //------------------------------------------------------------------------------------
  60. // Program main entry point
  61. //------------------------------------------------------------------------------------
  62. int main()
  63. {
  64. // Initialization
  65. //--------------------------------------------------------------------------------------
  66. InitWindow(screenWidth, screenHeight, "sample game: snake");
  67. InitGame();
  68. #if defined(PLATFORM_WEB)
  69. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  70. #else
  71. SetTargetFPS(60);
  72. //--------------------------------------------------------------------------------------
  73. // Main game loop
  74. while (!WindowShouldClose()) // Detect window close button or ESC key
  75. {
  76. // Update
  77. //----------------------------------------------------------------------------------
  78. UpdateGame();
  79. //----------------------------------------------------------------------------------
  80. // Draw
  81. //----------------------------------------------------------------------------------
  82. DrawGame();
  83. //----------------------------------------------------------------------------------
  84. }
  85. #endif
  86. // De-Initialization
  87. //--------------------------------------------------------------------------------------
  88. UnloadGame(); // Unload loaded data (textures, sounds, models...)
  89. CloseWindow(); // Close window and OpenGL context
  90. //--------------------------------------------------------------------------------------
  91. return 0;
  92. }
  93. //------------------------------------------------------------------------------------
  94. // Module Functions Definitions (local)
  95. //------------------------------------------------------------------------------------
  96. // Initialize game variables
  97. void InitGame(void)
  98. {
  99. framesCounter = 0;
  100. gameOver = false;
  101. pause = false;
  102. counterTail = 1;
  103. allowMove = false;
  104. offset.x = screenWidth%SQUARE_SIZE;
  105. offset.y = screenHeight%SQUARE_SIZE;
  106. for (int i = 0; i < SNAKE_LENGTH; i++)
  107. {
  108. snake[i].position = (Vector2){ offset.x/2, offset.y/2 };
  109. snake[i].size = (Vector2){ SQUARE_SIZE, SQUARE_SIZE };
  110. snake[i].speed = (Vector2){ SQUARE_SIZE, 0 };
  111. if (i == 0) snake[i].color = DARKBLUE;
  112. else snake[i].color = BLUE;
  113. }
  114. for (int i = 0; i < SNAKE_LENGTH; i++)
  115. {
  116. snakePosition[i] = (Vector2){ 0.0f, 0.0f };
  117. }
  118. fruit.size = (Vector2){ SQUARE_SIZE, SQUARE_SIZE };
  119. fruit.color = SKYBLUE;
  120. fruit.active = false;
  121. }
  122. // Update game (one frame)
  123. void UpdateGame(void)
  124. {
  125. if (!gameOver)
  126. {
  127. if (IsKeyPressed('P')) pause = !pause;
  128. if (!pause)
  129. {
  130. // control
  131. if (IsKeyPressed(KEY_RIGHT) && (snake[0].speed.x == 0) && allowMove)
  132. {
  133. snake[0].speed = (Vector2){ SQUARE_SIZE, 0 };
  134. allowMove = false;
  135. }
  136. if (IsKeyPressed(KEY_LEFT) && (snake[0].speed.x == 0) && allowMove)
  137. {
  138. snake[0].speed = (Vector2){ -SQUARE_SIZE, 0 };
  139. allowMove = false;
  140. }
  141. if (IsKeyPressed(KEY_UP) && (snake[0].speed.y == 0) && allowMove)
  142. {
  143. snake[0].speed = (Vector2){ 0, -SQUARE_SIZE };
  144. allowMove = false;
  145. }
  146. if (IsKeyPressed(KEY_DOWN) && (snake[0].speed.y == 0) && allowMove)
  147. {
  148. snake[0].speed = (Vector2){ 0, SQUARE_SIZE };
  149. allowMove = false;
  150. }
  151. // movement
  152. for (int i = 0; i < counterTail; i++) snakePosition[i] = snake[i].position;
  153. if ((framesCounter%5) == 0)
  154. {
  155. for (int i = 0; i < counterTail; i++)
  156. {
  157. if (i == 0)
  158. {
  159. snake[0].position.x += snake[0].speed.x;
  160. snake[0].position.y += snake[0].speed.y;
  161. allowMove = true;
  162. }
  163. else snake[i].position = snakePosition[i-1];
  164. }
  165. }
  166. // wall behaviour
  167. if (((snake[0].position.x) > (screenWidth - offset.x)) ||
  168. ((snake[0].position.y) > (screenHeight - offset.y)) ||
  169. (snake[0].position.x < 0) || (snake[0].position.y < 0))
  170. {
  171. gameOver = true;
  172. }
  173. // collision with yourself
  174. for (int i = 1; i < counterTail; i++)
  175. {
  176. if ((snake[0].position.x == snake[i].position.x) && (snake[0].position.y == snake[i].position.y)) gameOver = true;
  177. }
  178. // TODO: review logic: fruit.position calculation
  179. if (!fruit.active)
  180. {
  181. fruit.active = true;
  182. fruit.position = (Vector2){ GetRandomValue(0, (screenWidth/SQUARE_SIZE) - 1)*SQUARE_SIZE + offset.x/2, GetRandomValue(0, (screenHeight/SQUARE_SIZE) - 1)*SQUARE_SIZE + offset.y/2 };
  183. for (int i = 0; i < counterTail; i++)
  184. {
  185. while ((fruit.position.x == snake[i].position.x) && (fruit.position.y == snake[i].position.y))
  186. {
  187. fruit.position = (Vector2){ GetRandomValue(0, (screenWidth/SQUARE_SIZE) - 1)*SQUARE_SIZE, GetRandomValue(0, (screenHeight/SQUARE_SIZE) - 1)*SQUARE_SIZE };
  188. i = 0;
  189. }
  190. }
  191. }
  192. // collision
  193. if (CheckCollisionRecs((Rectangle){(int)snake[0].position.x, (int)snake[0].position.y, (int)snake[0].size.x, (int)snake[0].size.y},
  194. (Rectangle){(int)fruit.position.x, (int)fruit.position.y, (int)fruit.size.x, (int)fruit.size.y}))
  195. {
  196. snake[counterTail].position = snakePosition[counterTail - 1];
  197. counterTail += 1;
  198. fruit.active = false;
  199. }
  200. framesCounter++;
  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. // Draw grid lines
  220. for (int i = 0; i < screenWidth/SQUARE_SIZE + 1; i++)
  221. {
  222. DrawLineV((Vector2){SQUARE_SIZE*i + offset.x/2, offset.y/2}, (Vector2){SQUARE_SIZE*i + offset.x/2, screenHeight - offset.y/2}, LIGHTGRAY);
  223. }
  224. for (int i = 0; i < screenHeight/SQUARE_SIZE + 1; i++)
  225. {
  226. DrawLineV((Vector2){offset.x/2, SQUARE_SIZE*i + offset.y/2}, (Vector2){screenWidth - offset.x/2, SQUARE_SIZE*i + offset.y/2}, LIGHTGRAY);
  227. }
  228. // Draw snake
  229. for (int i = 0; i < counterTail; i++) DrawRectangleV(snake[i].position, snake[i].size, snake[i].color);
  230. // Draw fruit to pick
  231. DrawRectangleV(fruit.position, fruit.size, fruit.color);
  232. if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
  233. }
  234. else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
  235. EndDrawing();
  236. }
  237. // Unload game variables
  238. void UnloadGame(void)
  239. {
  240. // TODO: Unload all dynamic loaded data (textures, sounds, models...)
  241. }
  242. // Update and Draw (one frame)
  243. void UpdateDrawFrame(void)
  244. {
  245. UpdateGame();
  246. DrawGame();
  247. }