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.

245 lines
7.5 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib - sample game: floppy
  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 MAX_TUBES 100
  21. #define FLOPPY_RADIUS 24
  22. #define TUBES_WIDTH 80
  23. //----------------------------------------------------------------------------------
  24. // Types and Structures Definition
  25. //----------------------------------------------------------------------------------
  26. typedef struct Floppy {
  27. Vector2 position;
  28. int radius;
  29. Color color;
  30. } Floppy;
  31. typedef struct Tubes {
  32. Rectangle rec;
  33. Color color;
  34. bool active;
  35. } Tubes;
  36. //------------------------------------------------------------------------------------
  37. // Global Variables Declaration
  38. //------------------------------------------------------------------------------------
  39. static int screenWidth = 800;
  40. static int screenHeight = 450;
  41. static int framesCounter;
  42. static bool gameOver;
  43. static bool pause;
  44. static int score;
  45. static int hiScore = 0;
  46. static Floppy floppy;
  47. static Tubes tubes[MAX_TUBES*2];
  48. static Vector2 tubesPos[MAX_TUBES];
  49. static int tubesSpeedX;
  50. static bool superfx;
  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: floppy");
  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. floppy.radius = FLOPPY_RADIUS;
  100. floppy.position = (Vector2){80, screenHeight/2 - floppy.radius};
  101. tubesSpeedX = 2;
  102. for (int i = 0; i < MAX_TUBES; i++)
  103. {
  104. tubesPos[i].x = 400 + 280*i;
  105. tubesPos[i].y = -GetRandomValue(0, 120);
  106. }
  107. for (int i = 0; i < MAX_TUBES*2; i += 2)
  108. {
  109. tubes[i].rec.x = tubesPos[i/2].x;
  110. tubes[i].rec.y = tubesPos[i/2].y;
  111. tubes[i].rec.width = TUBES_WIDTH;
  112. tubes[i].rec.height = 255;
  113. tubes[i+1].rec.x = tubesPos[i/2].x;
  114. tubes[i+1].rec.y = 600 + tubesPos[i/2].y - 255;
  115. tubes[i+1].rec.width = TUBES_WIDTH;
  116. tubes[i+1].rec.height = 255;
  117. tubes[i/2].active = true;
  118. }
  119. score = 0;
  120. gameOver = false;
  121. superfx = false;
  122. pause = 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. for (int i = 0; i < MAX_TUBES; i++) tubesPos[i].x -= tubesSpeedX;
  133. for (int i = 0; i < MAX_TUBES*2; i += 2)
  134. {
  135. tubes[i].rec.x = tubesPos[i/2].x;
  136. tubes[i+1].rec.x = tubesPos[i/2].x;
  137. }
  138. if (IsKeyDown(KEY_SPACE) && !gameOver) floppy.position.y -= 3;
  139. else floppy.position.y += 1;
  140. // Check Collisions
  141. for (int i = 0; i < MAX_TUBES*2; i++)
  142. {
  143. if (CheckCollisionCircleRec(floppy.position, floppy.radius, tubes[i].rec))
  144. {
  145. gameOver = true;
  146. pause = false;
  147. }
  148. else if ((tubesPos[i/2].x < floppy.position.x) && tubes[i/2].active && !gameOver)
  149. {
  150. score += 100;
  151. tubes[i/2].active = false;
  152. superfx = true;
  153. if (score > hiScore) hiScore = score;
  154. }
  155. }
  156. }
  157. }
  158. else
  159. {
  160. if (IsKeyPressed(KEY_ENTER))
  161. {
  162. InitGame();
  163. gameOver = false;
  164. }
  165. }
  166. }
  167. // Draw game (one frame)
  168. void DrawGame(void)
  169. {
  170. BeginDrawing();
  171. ClearBackground(RAYWHITE);
  172. if (!gameOver)
  173. {
  174. DrawCircle(floppy.position.x, floppy.position.y, floppy.radius, DARKGRAY);
  175. // Draw tubes
  176. for (int i = 0; i < MAX_TUBES; i++)
  177. {
  178. DrawRectangle(tubes[i*2].rec.x, tubes[i*2].rec.y, tubes[i*2].rec.width, tubes[i*2].rec.height, GRAY);
  179. DrawRectangle(tubes[i*2 + 1].rec.x, tubes[i*2 + 1].rec.y, tubes[i*2 + 1].rec.width, tubes[i*2 + 1].rec.height, GRAY);
  180. }
  181. // Draw flashing fx (one frame only)
  182. if (superfx)
  183. {
  184. DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
  185. superfx = false;
  186. }
  187. DrawText(FormatText("%04i", score), 20, 20, 40, GRAY);
  188. DrawText(FormatText("HI-SCORE: %04i", hiScore), 20, 70, 20, LIGHTGRAY);
  189. if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
  190. }
  191. else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
  192. EndDrawing();
  193. }
  194. // Unload game variables
  195. void UnloadGame(void)
  196. {
  197. // TODO: Unload all dynamic loaded data (textures, sounds, models...)
  198. }
  199. // Update and Draw (one frame)
  200. void UpdateDrawFrame(void)
  201. {
  202. UpdateGame();
  203. DrawGame();
  204. }