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.

252 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_ANDROID)
  15. #include "android_native_app_glue.h"
  16. #endif
  17. #if defined(PLATFORM_WEB)
  18. #include <emscripten/emscripten.h>
  19. #endif
  20. //----------------------------------------------------------------------------------
  21. // Some Defines
  22. //----------------------------------------------------------------------------------
  23. #define MAX_TUBES 100
  24. #define FLOPPY_RADIUS 24
  25. #define TUBES_WIDTH 80
  26. //----------------------------------------------------------------------------------
  27. // Types and Structures Definition
  28. //----------------------------------------------------------------------------------
  29. typedef struct Floppy {
  30. Vector2 position;
  31. int radius;
  32. Color color;
  33. } Floppy;
  34. typedef struct Tubes {
  35. Rectangle rec;
  36. Color color;
  37. bool active;
  38. } Tubes;
  39. //------------------------------------------------------------------------------------
  40. // Global Variables Declaration
  41. //------------------------------------------------------------------------------------
  42. static int screenWidth = 800;
  43. static int screenHeight = 450;
  44. static bool gameOver;
  45. static bool pause;
  46. static int score;
  47. static int hiScore = 0;
  48. static Floppy floppy;
  49. static Tubes tubes[MAX_TUBES*2];
  50. static Vector2 tubesPos[MAX_TUBES];
  51. static int tubesSpeedX;
  52. static bool superfx;
  53. //------------------------------------------------------------------------------------
  54. // Module Functions Declaration (local)
  55. //------------------------------------------------------------------------------------
  56. static void InitGame(void); // Initialize game
  57. static void UpdateGame(void); // Update game (one frame)
  58. static void DrawGame(void); // Draw game (one frame)
  59. static void UnloadGame(void); // Unload game
  60. static void UpdateDrawFrame(void); // Update and Draw (one frame)
  61. //------------------------------------------------------------------------------------
  62. // Program main entry point
  63. //------------------------------------------------------------------------------------
  64. #if defined(PLATFORM_ANDROID)
  65. void android_main(struct android_app *app)
  66. #else
  67. int main(void)
  68. #endif
  69. {
  70. // Initialization
  71. //---------------------------------------------------------
  72. #if defined(PLATFORM_ANDROID)
  73. InitWindow(screenWidth, screenHeight, app);
  74. #else
  75. InitWindow(screenWidth, screenHeight, "sample game: floppy");
  76. #endif
  77. InitGame();
  78. #if defined(PLATFORM_WEB)
  79. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  80. #else
  81. SetTargetFPS(60);
  82. //--------------------------------------------------------------------------------------
  83. // Main game loop
  84. while (!WindowShouldClose()) // Detect window close button or ESC key
  85. {
  86. // Update and Draw
  87. //----------------------------------------------------------------------------------
  88. UpdateDrawFrame();
  89. //----------------------------------------------------------------------------------
  90. }
  91. #endif
  92. // De-Initialization
  93. //--------------------------------------------------------------------------------------
  94. UnloadGame(); // Unload loaded data (textures, sounds, models...)
  95. CloseWindow(); // Close window and OpenGL context
  96. //--------------------------------------------------------------------------------------
  97. #if !defined(PLATFORM_ANDROID)
  98. return 0;
  99. #endif
  100. }
  101. //------------------------------------------------------------------------------------
  102. // Module Functions Definitions (local)
  103. //------------------------------------------------------------------------------------
  104. // Initialize game variables
  105. void InitGame(void)
  106. {
  107. floppy.radius = FLOPPY_RADIUS;
  108. floppy.position = (Vector2){80, screenHeight/2 - floppy.radius};
  109. tubesSpeedX = 2;
  110. for (int i = 0; i < MAX_TUBES; i++)
  111. {
  112. tubesPos[i].x = 400 + 280*i;
  113. tubesPos[i].y = -GetRandomValue(0, 120);
  114. }
  115. for (int i = 0; i < MAX_TUBES*2; i += 2)
  116. {
  117. tubes[i].rec.x = tubesPos[i/2].x;
  118. tubes[i].rec.y = tubesPos[i/2].y;
  119. tubes[i].rec.width = TUBES_WIDTH;
  120. tubes[i].rec.height = 255;
  121. tubes[i+1].rec.x = tubesPos[i/2].x;
  122. tubes[i+1].rec.y = 600 + tubesPos[i/2].y - 255;
  123. tubes[i+1].rec.width = TUBES_WIDTH;
  124. tubes[i+1].rec.height = 255;
  125. tubes[i/2].active = true;
  126. }
  127. score = 0;
  128. gameOver = false;
  129. superfx = false;
  130. pause = false;
  131. }
  132. // Update game (one frame)
  133. void UpdateGame(void)
  134. {
  135. if (!gameOver)
  136. {
  137. if (IsKeyPressed('P')) pause = !pause;
  138. if (!pause)
  139. {
  140. for (int i = 0; i < MAX_TUBES; i++) tubesPos[i].x -= tubesSpeedX;
  141. for (int i = 0; i < MAX_TUBES*2; i += 2)
  142. {
  143. tubes[i].rec.x = tubesPos[i/2].x;
  144. tubes[i+1].rec.x = tubesPos[i/2].x;
  145. }
  146. if (IsKeyDown(KEY_SPACE) && !gameOver) floppy.position.y -= 3;
  147. else floppy.position.y += 1;
  148. // Check Collisions
  149. for (int i = 0; i < MAX_TUBES*2; i++)
  150. {
  151. if (CheckCollisionCircleRec(floppy.position, floppy.radius, tubes[i].rec))
  152. {
  153. gameOver = true;
  154. pause = false;
  155. }
  156. else if ((tubesPos[i/2].x < floppy.position.x) && tubes[i/2].active && !gameOver)
  157. {
  158. score += 100;
  159. tubes[i/2].active = false;
  160. superfx = true;
  161. if (score > hiScore) hiScore = score;
  162. }
  163. }
  164. }
  165. }
  166. else
  167. {
  168. if (IsKeyPressed(KEY_ENTER))
  169. {
  170. InitGame();
  171. gameOver = false;
  172. }
  173. }
  174. }
  175. // Draw game (one frame)
  176. void DrawGame(void)
  177. {
  178. BeginDrawing();
  179. ClearBackground(RAYWHITE);
  180. if (!gameOver)
  181. {
  182. DrawCircle(floppy.position.x, floppy.position.y, floppy.radius, DARKGRAY);
  183. // Draw tubes
  184. for (int i = 0; i < MAX_TUBES; i++)
  185. {
  186. DrawRectangle(tubes[i*2].rec.x, tubes[i*2].rec.y, tubes[i*2].rec.width, tubes[i*2].rec.height, GRAY);
  187. 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);
  188. }
  189. // Draw flashing fx (one frame only)
  190. if (superfx)
  191. {
  192. DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
  193. superfx = false;
  194. }
  195. DrawText(FormatText("%04i", score), 20, 20, 40, GRAY);
  196. DrawText(FormatText("HI-SCORE: %04i", hiScore), 20, 70, 20, LIGHTGRAY);
  197. if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
  198. }
  199. else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
  200. EndDrawing();
  201. }
  202. // Unload game variables
  203. void UnloadGame(void)
  204. {
  205. // TODO: Unload all dynamic loaded data (textures, sounds, models...)
  206. }
  207. // Update and Draw (one frame)
  208. void UpdateDrawFrame(void)
  209. {
  210. UpdateGame();
  211. DrawGame();
  212. }