Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

390 řádky
14 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib - sample game: asteroids survival
  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. #include <math.h>
  15. #if defined(PLATFORM_ANDROID)
  16. #include "android_native_app_glue.h"
  17. #endif
  18. #if defined(PLATFORM_WEB)
  19. #include <emscripten/emscripten.h>
  20. #endif
  21. //----------------------------------------------------------------------------------
  22. // Some Defines
  23. //----------------------------------------------------------------------------------
  24. #define PLAYER_BASE_SIZE 20.0f
  25. #define PLAYER_SPEED 6.0f
  26. #define PLAYER_MAX_SHOOTS 10
  27. #define METEORS_SPEED 2
  28. #define MAX_MEDIUM_METEORS 8
  29. #define MAX_SMALL_METEORS 16
  30. //----------------------------------------------------------------------------------
  31. // Types and Structures Definition
  32. //----------------------------------------------------------------------------------
  33. typedef struct Player {
  34. Vector2 position;
  35. Vector2 speed;
  36. float acceleration;
  37. float rotation;
  38. Vector3 collider;
  39. Color color;
  40. } Player;
  41. typedef struct Meteor {
  42. Vector2 position;
  43. Vector2 speed;
  44. float radius;
  45. bool active;
  46. Color color;
  47. } Meteor;
  48. //------------------------------------------------------------------------------------
  49. // Global Variables Declaration
  50. //------------------------------------------------------------------------------------
  51. static int screenWidth = 800;
  52. static int screenHeight = 450;
  53. static int framesCounter;
  54. static bool gameOver;
  55. static bool pause;
  56. // NOTE: Defined triangle is isosceles with common angles of 70 degrees.
  57. static float shipHeight;
  58. static Player player;
  59. static Meteor mediumMeteor[MAX_MEDIUM_METEORS];
  60. static Meteor smallMeteor[MAX_SMALL_METEORS];
  61. //------------------------------------------------------------------------------------
  62. // Module Functions Declaration (local)
  63. //------------------------------------------------------------------------------------
  64. static void InitGame(void); // Initialize game
  65. static void UpdateGame(void); // Update game (one frame)
  66. static void DrawGame(void); // Draw game (one frame)
  67. static void UnloadGame(void); // Unload game
  68. static void UpdateDrawFrame(void); // Update and Draw (one frame)
  69. //------------------------------------------------------------------------------------
  70. // Program main entry point
  71. //------------------------------------------------------------------------------------
  72. #if defined(PLATFORM_ANDROID)
  73. void android_main(struct android_app *app)
  74. #else
  75. int main(void)
  76. #endif
  77. {
  78. // Initialization
  79. //---------------------------------------------------------
  80. #if defined(PLATFORM_ANDROID)
  81. InitWindow(screenWidth, screenHeight, app);
  82. #else
  83. InitWindow(screenWidth, screenHeight, "sample game: asteroids survival");
  84. #endif
  85. InitGame();
  86. #if defined(PLATFORM_WEB)
  87. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  88. #else
  89. SetTargetFPS(60);
  90. //--------------------------------------------------------------------------------------
  91. // Main game loop
  92. while (!WindowShouldClose()) // Detect window close button or ESC key
  93. {
  94. // Update and Draw
  95. //----------------------------------------------------------------------------------
  96. UpdateDrawFrame();
  97. //----------------------------------------------------------------------------------
  98. }
  99. #endif
  100. // De-Initialization
  101. //--------------------------------------------------------------------------------------
  102. UnloadGame(); // Unload loaded data (textures, sounds, models...)
  103. CloseWindow(); // Close window and OpenGL context
  104. //--------------------------------------------------------------------------------------
  105. #if !defined(PLATFORM_ANDROID)
  106. return 0;
  107. #endif
  108. }
  109. //------------------------------------------------------------------------------------
  110. // Module Functions Definitions (local)
  111. //------------------------------------------------------------------------------------
  112. // Initialize game variables
  113. void InitGame(void)
  114. {
  115. int posx, posy;
  116. int velx, vely;
  117. bool correctRange = false;
  118. pause = false;
  119. framesCounter = 0;
  120. shipHeight = (PLAYER_BASE_SIZE/2)/tanf(20*DEG2RAD);
  121. // Initialization player
  122. player.position = (Vector2){screenWidth/2, screenHeight/2 - shipHeight/2};
  123. player.speed = (Vector2){0, 0};
  124. player.acceleration = 0;
  125. player.rotation = 0;
  126. player.collider = (Vector3){player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight/2.5f), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight/2.5f), 12};
  127. player.color = LIGHTGRAY;
  128. for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
  129. {
  130. posx = GetRandomValue(0, screenWidth);
  131. while(!correctRange)
  132. {
  133. if (posx > screenWidth/2 - 150 && posx < screenWidth/2 + 150) posx = GetRandomValue(0, screenWidth);
  134. else correctRange = true;
  135. }
  136. correctRange = false;
  137. posy = GetRandomValue(0, screenHeight);
  138. while(!correctRange)
  139. {
  140. if (posy > screenHeight/2 - 150 && posy < screenHeight/2 + 150) posy = GetRandomValue(0, screenHeight);
  141. else correctRange = true;
  142. }
  143. correctRange = false;
  144. velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  145. vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  146. while(!correctRange)
  147. {
  148. if (velx == 0 && vely == 0)
  149. {
  150. velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  151. vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  152. }
  153. else correctRange = true;
  154. }
  155. mediumMeteor[i].position = (Vector2){posx, posy};
  156. mediumMeteor[i].speed = (Vector2){velx, vely};
  157. mediumMeteor[i].radius = 20;
  158. mediumMeteor[i].active = true;
  159. mediumMeteor[i].color = GREEN;
  160. }
  161. for (int i = 0; i < MAX_SMALL_METEORS; i++)
  162. {
  163. posx = GetRandomValue(0, screenWidth);
  164. while(!correctRange)
  165. {
  166. if (posx > screenWidth/2 - 150 && posx < screenWidth/2 + 150) posx = GetRandomValue(0, screenWidth);
  167. else correctRange = true;
  168. }
  169. correctRange = false;
  170. posy = GetRandomValue(0, screenHeight);
  171. while(!correctRange)
  172. {
  173. if (posy > screenHeight/2 - 150 && posy < screenHeight/2 + 150) posy = GetRandomValue(0, screenHeight);
  174. else correctRange = true;
  175. }
  176. correctRange = false;
  177. velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  178. vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  179. while(!correctRange)
  180. {
  181. if (velx == 0 && vely == 0)
  182. {
  183. velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  184. vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  185. }
  186. else correctRange = true;
  187. }
  188. smallMeteor[i].position = (Vector2){posx, posy};
  189. smallMeteor[i].speed = (Vector2){velx, vely};
  190. smallMeteor[i].radius = 10;
  191. smallMeteor[i].active = true;
  192. smallMeteor[i].color = YELLOW;
  193. }
  194. }
  195. // Update game (one frame)
  196. void UpdateGame(void)
  197. {
  198. if (!gameOver)
  199. {
  200. if (IsKeyPressed('P')) pause = !pause;
  201. if (!pause)
  202. {
  203. framesCounter++;
  204. // Player logic
  205. // Rotation
  206. if (IsKeyDown(KEY_LEFT)) player.rotation -= 5;
  207. if (IsKeyDown(KEY_RIGHT)) player.rotation += 5;
  208. // Speed
  209. player.speed.x = sin(player.rotation*DEG2RAD)*PLAYER_SPEED;
  210. player.speed.y = cos(player.rotation*DEG2RAD)*PLAYER_SPEED;
  211. // Controller
  212. if (IsKeyDown(KEY_UP))
  213. {
  214. if (player.acceleration < 1) player.acceleration += 0.04f;
  215. }
  216. else
  217. {
  218. if (player.acceleration > 0) player.acceleration -= 0.02f;
  219. else if (player.acceleration < 0) player.acceleration = 0;
  220. }
  221. if (IsKeyDown(KEY_DOWN))
  222. {
  223. if (player.acceleration > 0) player.acceleration -= 0.04f;
  224. else if (player.acceleration < 0) player.acceleration = 0;
  225. }
  226. // Movement
  227. player.position.x += (player.speed.x*player.acceleration);
  228. player.position.y -= (player.speed.y*player.acceleration);
  229. // Wall behaviour for player
  230. if (player.position.x > screenWidth + shipHeight) player.position.x = -(shipHeight);
  231. else if (player.position.x < -(shipHeight)) player.position.x = screenWidth + shipHeight;
  232. if (player.position.y > (screenHeight + shipHeight)) player.position.y = -(shipHeight);
  233. else if (player.position.y < -(shipHeight)) player.position.y = screenHeight + shipHeight;
  234. // Collision Player to meteors
  235. player.collider = (Vector3){player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight/2.5f), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight/2.5f), 12};
  236. for (int a = 0; a < MAX_MEDIUM_METEORS; a++)
  237. {
  238. if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, mediumMeteor[a].position, mediumMeteor[a].radius) && mediumMeteor[a].active) gameOver = true;
  239. }
  240. for (int a = 0; a < MAX_SMALL_METEORS; a++)
  241. {
  242. if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, smallMeteor[a].position, smallMeteor[a].radius) && smallMeteor[a].active) gameOver = true;
  243. }
  244. // Meteor logic
  245. for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
  246. {
  247. if (mediumMeteor[i].active)
  248. {
  249. // movement
  250. mediumMeteor[i].position.x += mediumMeteor[i].speed.x;
  251. mediumMeteor[i].position.y += mediumMeteor[i].speed.y;
  252. // wall behaviour
  253. if (mediumMeteor[i].position.x > screenWidth + mediumMeteor[i].radius) mediumMeteor[i].position.x = -(mediumMeteor[i].radius);
  254. else if (mediumMeteor[i].position.x < 0 - mediumMeteor[i].radius) mediumMeteor[i].position.x = screenWidth + mediumMeteor[i].radius;
  255. if (mediumMeteor[i].position.y > screenHeight + mediumMeteor[i].radius) mediumMeteor[i].position.y = -(mediumMeteor[i].radius);
  256. else if (mediumMeteor[i].position.y < 0 - mediumMeteor[i].radius) mediumMeteor[i].position.y = screenHeight + mediumMeteor[i].radius;
  257. }
  258. }
  259. for (int i = 0; i < MAX_SMALL_METEORS; i++)
  260. {
  261. if (smallMeteor[i].active)
  262. {
  263. // movement
  264. smallMeteor[i].position.x += smallMeteor[i].speed.x;
  265. smallMeteor[i].position.y += smallMeteor[i].speed.y;
  266. // wall behaviour
  267. if (smallMeteor[i].position.x > screenWidth + smallMeteor[i].radius) smallMeteor[i].position.x = -(smallMeteor[i].radius);
  268. else if (smallMeteor[i].position.x < 0 - smallMeteor[i].radius) smallMeteor[i].position.x = screenWidth + smallMeteor[i].radius;
  269. if (smallMeteor[i].position.y > screenHeight + smallMeteor[i].radius) smallMeteor[i].position.y = -(smallMeteor[i].radius);
  270. else if (smallMeteor[i].position.y < 0 - smallMeteor[i].radius) smallMeteor[i].position.y = screenHeight + smallMeteor[i].radius;
  271. }
  272. }
  273. }
  274. }
  275. else
  276. {
  277. if (IsKeyPressed(KEY_ENTER))
  278. {
  279. InitGame();
  280. gameOver = false;
  281. }
  282. }
  283. }
  284. // Draw game (one frame)
  285. void DrawGame(void)
  286. {
  287. BeginDrawing();
  288. ClearBackground(RAYWHITE);
  289. if (!gameOver)
  290. {
  291. // Draw spaceship
  292. Vector2 v1 = { player.position.x + sinf(player.rotation*DEG2RAD)*(shipHeight), player.position.y - cosf(player.rotation*DEG2RAD)*(shipHeight) };
  293. Vector2 v2 = { player.position.x - cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y - sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
  294. Vector2 v3 = { player.position.x + cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y + sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
  295. DrawTriangle(v1, v2, v3, MAROON);
  296. // Draw meteor
  297. for (int i = 0;i < MAX_MEDIUM_METEORS; i++)
  298. {
  299. if (mediumMeteor[i].active) DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, GRAY);
  300. else DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
  301. }
  302. for (int i = 0;i < MAX_SMALL_METEORS; i++)
  303. {
  304. if (smallMeteor[i].active) DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, DARKGRAY);
  305. else DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
  306. }
  307. DrawText(FormatText("TIME: %.02f", (float)framesCounter/60), 10, 10, 20, BLACK);
  308. if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
  309. }
  310. else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
  311. EndDrawing();
  312. //----------------------------------------------------------------------------------
  313. }
  314. // Unload game variables
  315. void UnloadGame(void)
  316. {
  317. // TODO: Unload all dynamic loaded data (textures, sounds, models...)
  318. }
  319. // Update and Draw (one frame)
  320. void UpdateDrawFrame(void)
  321. {
  322. UpdateGame();
  323. DrawGame();
  324. }