Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

238 строки
7.3 KiB

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