Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

565 righe
23 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib - sample game: asteroids
  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_WEB)
  16. #include <emscripten/emscripten.h>
  17. #endif
  18. //----------------------------------------------------------------------------------
  19. // Some Defines
  20. //----------------------------------------------------------------------------------
  21. #define PLAYER_BASE_SIZE 20.0f
  22. #define PLAYER_SPEED 6.0f
  23. #define PLAYER_MAX_SHOOTS 10
  24. #define METEORS_SPEED 2
  25. #define MAX_BIG_METEORS 4
  26. #define MAX_MEDIUM_METEORS 8
  27. #define MAX_SMALL_METEORS 16
  28. //----------------------------------------------------------------------------------
  29. // Types and Structures Definition
  30. //----------------------------------------------------------------------------------
  31. typedef struct Player {
  32. Vector2 position;
  33. Vector2 speed;
  34. float acceleration;
  35. float rotation;
  36. Vector3 collider;
  37. Color color;
  38. } Player;
  39. typedef struct Shoot {
  40. Vector2 position;
  41. Vector2 speed;
  42. float radius;
  43. float rotation;
  44. int lifeSpawn;
  45. bool active;
  46. Color color;
  47. } Shoot;
  48. typedef struct Meteor {
  49. Vector2 position;
  50. Vector2 speed;
  51. float radius;
  52. bool active;
  53. Color color;
  54. } Meteor;
  55. //------------------------------------------------------------------------------------
  56. // Global Variables Declaration
  57. //------------------------------------------------------------------------------------
  58. static const int screenWidth = 800;
  59. static const int screenHeight = 450;
  60. static bool gameOver = false;
  61. static bool pause = false;
  62. static bool victory = false;
  63. // NOTE: Defined triangle is isosceles with common angles of 70 degrees.
  64. static float shipHeight = 0.0f;
  65. static Player player = { 0 };
  66. static Shoot shoot[PLAYER_MAX_SHOOTS] = { 0 };
  67. static Meteor bigMeteor[MAX_BIG_METEORS] = { 0 };
  68. static Meteor mediumMeteor[MAX_MEDIUM_METEORS] = { 0 };
  69. static Meteor smallMeteor[MAX_SMALL_METEORS] = { 0 };
  70. static int midMeteorsCount = 0;
  71. static int smallMeteorsCount = 0;
  72. static int destroyedMeteorsCount = 0;
  73. //------------------------------------------------------------------------------------
  74. // Module Functions Declaration (local)
  75. //------------------------------------------------------------------------------------
  76. static void InitGame(void); // Initialize game
  77. static void UpdateGame(void); // Update game (one frame)
  78. static void DrawGame(void); // Draw game (one frame)
  79. static void UnloadGame(void); // Unload game
  80. static void UpdateDrawFrame(void); // Update and Draw (one frame)
  81. //------------------------------------------------------------------------------------
  82. // Program main entry point
  83. //------------------------------------------------------------------------------------
  84. int main(void)
  85. {
  86. // Initialization (Note windowTitle is unused on Android)
  87. //---------------------------------------------------------
  88. InitWindow(screenWidth, screenHeight, "sample game: asteroids");
  89. InitGame();
  90. #if defined(PLATFORM_WEB)
  91. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  92. #else
  93. SetTargetFPS(60);
  94. //--------------------------------------------------------------------------------------
  95. // Main game loop
  96. while (!WindowShouldClose()) // Detect window close button or ESC key
  97. {
  98. // Update and Draw
  99. //----------------------------------------------------------------------------------
  100. UpdateDrawFrame();
  101. //----------------------------------------------------------------------------------
  102. }
  103. #endif
  104. // De-Initialization
  105. //--------------------------------------------------------------------------------------
  106. UnloadGame(); // Unload loaded data (textures, sounds, models...)
  107. CloseWindow(); // Close window and OpenGL context
  108. //--------------------------------------------------------------------------------------
  109. return 0;
  110. }
  111. //------------------------------------------------------------------------------------
  112. // Module Functions Definitions (local)
  113. //------------------------------------------------------------------------------------
  114. // Initialize game variables
  115. void InitGame(void)
  116. {
  117. int posx, posy;
  118. int velx, vely;
  119. bool correctRange = false;
  120. victory = false;
  121. pause = false;
  122. shipHeight = (PLAYER_BASE_SIZE/2)/tanf(20*DEG2RAD);
  123. // Initialization player
  124. player.position = (Vector2){screenWidth/2, screenHeight/2 - shipHeight/2};
  125. player.speed = (Vector2){0, 0};
  126. player.acceleration = 0;
  127. player.rotation = 0;
  128. player.collider = (Vector3){player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight/2.5f), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight/2.5f), 12};
  129. player.color = LIGHTGRAY;
  130. destroyedMeteorsCount = 0;
  131. // Initialization shoot
  132. for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
  133. {
  134. shoot[i].position = (Vector2){0, 0};
  135. shoot[i].speed = (Vector2){0, 0};
  136. shoot[i].radius = 2;
  137. shoot[i].active = false;
  138. shoot[i].lifeSpawn = 0;
  139. shoot[i].color = WHITE;
  140. }
  141. for (int i = 0; i < MAX_BIG_METEORS; i++)
  142. {
  143. posx = GetRandomValue(0, screenWidth);
  144. while(!correctRange)
  145. {
  146. if (posx > screenWidth/2 - 150 && posx < screenWidth/2 + 150) posx = GetRandomValue(0, screenWidth);
  147. else correctRange = true;
  148. }
  149. correctRange = false;
  150. posy = GetRandomValue(0, screenHeight);
  151. while(!correctRange)
  152. {
  153. if (posy > screenHeight/2 - 150 && posy < screenHeight/2 + 150) posy = GetRandomValue(0, screenHeight);
  154. else correctRange = true;
  155. }
  156. bigMeteor[i].position = (Vector2){posx, posy};
  157. correctRange = false;
  158. velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  159. vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  160. while(!correctRange)
  161. {
  162. if (velx == 0 && vely == 0)
  163. {
  164. velx = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  165. vely = GetRandomValue(-METEORS_SPEED, METEORS_SPEED);
  166. }
  167. else correctRange = true;
  168. }
  169. bigMeteor[i].speed = (Vector2){velx, vely};
  170. bigMeteor[i].radius = 40;
  171. bigMeteor[i].active = true;
  172. bigMeteor[i].color = BLUE;
  173. }
  174. for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
  175. {
  176. mediumMeteor[i].position = (Vector2){-100, -100};
  177. mediumMeteor[i].speed = (Vector2){0,0};
  178. mediumMeteor[i].radius = 20;
  179. mediumMeteor[i].active = false;
  180. mediumMeteor[i].color = BLUE;
  181. }
  182. for (int i = 0; i < MAX_SMALL_METEORS; i++)
  183. {
  184. smallMeteor[i].position = (Vector2){-100, -100};
  185. smallMeteor[i].speed = (Vector2){0,0};
  186. smallMeteor[i].radius = 10;
  187. smallMeteor[i].active = false;
  188. smallMeteor[i].color = BLUE;
  189. }
  190. midMeteorsCount = 0;
  191. smallMeteorsCount = 0;
  192. }
  193. // Update game (one frame)
  194. void UpdateGame(void)
  195. {
  196. if (!gameOver)
  197. {
  198. if (IsKeyPressed('P')) pause = !pause;
  199. if (!pause)
  200. {
  201. // Player logic: rotation
  202. if (IsKeyDown(KEY_LEFT)) player.rotation -= 5;
  203. if (IsKeyDown(KEY_RIGHT)) player.rotation += 5;
  204. // Player logic: speed
  205. player.speed.x = sin(player.rotation*DEG2RAD)*PLAYER_SPEED;
  206. player.speed.y = cos(player.rotation*DEG2RAD)*PLAYER_SPEED;
  207. // Player logic: acceleration
  208. if (IsKeyDown(KEY_UP))
  209. {
  210. if (player.acceleration < 1) player.acceleration += 0.04f;
  211. }
  212. else
  213. {
  214. if (player.acceleration > 0) player.acceleration -= 0.02f;
  215. else if (player.acceleration < 0) player.acceleration = 0;
  216. }
  217. if (IsKeyDown(KEY_DOWN))
  218. {
  219. if (player.acceleration > 0) player.acceleration -= 0.04f;
  220. else if (player.acceleration < 0) player.acceleration = 0;
  221. }
  222. // Player logic: movement
  223. player.position.x += (player.speed.x*player.acceleration);
  224. player.position.y -= (player.speed.y*player.acceleration);
  225. // Collision logic: player vs walls
  226. if (player.position.x > screenWidth + shipHeight) player.position.x = -(shipHeight);
  227. else if (player.position.x < -(shipHeight)) player.position.x = screenWidth + shipHeight;
  228. if (player.position.y > (screenHeight + shipHeight)) player.position.y = -(shipHeight);
  229. else if (player.position.y < -(shipHeight)) player.position.y = screenHeight + shipHeight;
  230. // Player shoot logic
  231. if (IsKeyPressed(KEY_SPACE))
  232. {
  233. for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
  234. {
  235. if (!shoot[i].active)
  236. {
  237. shoot[i].position = (Vector2){ player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight) };
  238. shoot[i].active = true;
  239. shoot[i].speed.x = 1.5*sin(player.rotation*DEG2RAD)*PLAYER_SPEED;
  240. shoot[i].speed.y = 1.5*cos(player.rotation*DEG2RAD)*PLAYER_SPEED;
  241. shoot[i].rotation = player.rotation;
  242. break;
  243. }
  244. }
  245. }
  246. // Shoot life timer
  247. for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
  248. {
  249. if (shoot[i].active) shoot[i].lifeSpawn++;
  250. }
  251. // Shot logic
  252. for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
  253. {
  254. if (shoot[i].active)
  255. {
  256. // Movement
  257. shoot[i].position.x += shoot[i].speed.x;
  258. shoot[i].position.y -= shoot[i].speed.y;
  259. // Collision logic: shoot vs walls
  260. if (shoot[i].position.x > screenWidth + shoot[i].radius)
  261. {
  262. shoot[i].active = false;
  263. shoot[i].lifeSpawn = 0;
  264. }
  265. else if (shoot[i].position.x < 0 - shoot[i].radius)
  266. {
  267. shoot[i].active = false;
  268. shoot[i].lifeSpawn = 0;
  269. }
  270. if (shoot[i].position.y > screenHeight + shoot[i].radius)
  271. {
  272. shoot[i].active = false;
  273. shoot[i].lifeSpawn = 0;
  274. }
  275. else if (shoot[i].position.y < 0 - shoot[i].radius)
  276. {
  277. shoot[i].active = false;
  278. shoot[i].lifeSpawn = 0;
  279. }
  280. // Life of shoot
  281. if (shoot[i].lifeSpawn >= 60)
  282. {
  283. shoot[i].position = (Vector2){0, 0};
  284. shoot[i].speed = (Vector2){0, 0};
  285. shoot[i].lifeSpawn = 0;
  286. shoot[i].active = false;
  287. }
  288. }
  289. }
  290. // Collision logic: player vs meteors
  291. player.collider = (Vector3){player.position.x + sin(player.rotation*DEG2RAD)*(shipHeight/2.5f), player.position.y - cos(player.rotation*DEG2RAD)*(shipHeight/2.5f), 12};
  292. for (int a = 0; a < MAX_BIG_METEORS; a++)
  293. {
  294. if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, bigMeteor[a].position, bigMeteor[a].radius) && bigMeteor[a].active) gameOver = true;
  295. }
  296. for (int a = 0; a < MAX_MEDIUM_METEORS; a++)
  297. {
  298. if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, mediumMeteor[a].position, mediumMeteor[a].radius) && mediumMeteor[a].active) gameOver = true;
  299. }
  300. for (int a = 0; a < MAX_SMALL_METEORS; a++)
  301. {
  302. if (CheckCollisionCircles((Vector2){player.collider.x, player.collider.y}, player.collider.z, smallMeteor[a].position, smallMeteor[a].radius) && smallMeteor[a].active) gameOver = true;
  303. }
  304. // Meteors logic: big meteors
  305. for (int i = 0; i < MAX_BIG_METEORS; i++)
  306. {
  307. if (bigMeteor[i].active)
  308. {
  309. // Movement
  310. bigMeteor[i].position.x += bigMeteor[i].speed.x;
  311. bigMeteor[i].position.y += bigMeteor[i].speed.y;
  312. // Collision logic: meteor vs wall
  313. if (bigMeteor[i].position.x > screenWidth + bigMeteor[i].radius) bigMeteor[i].position.x = -(bigMeteor[i].radius);
  314. else if (bigMeteor[i].position.x < 0 - bigMeteor[i].radius) bigMeteor[i].position.x = screenWidth + bigMeteor[i].radius;
  315. if (bigMeteor[i].position.y > screenHeight + bigMeteor[i].radius) bigMeteor[i].position.y = -(bigMeteor[i].radius);
  316. else if (bigMeteor[i].position.y < 0 - bigMeteor[i].radius) bigMeteor[i].position.y = screenHeight + bigMeteor[i].radius;
  317. }
  318. }
  319. // Meteors logic: medium meteors
  320. for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
  321. {
  322. if (mediumMeteor[i].active)
  323. {
  324. // Movement
  325. mediumMeteor[i].position.x += mediumMeteor[i].speed.x;
  326. mediumMeteor[i].position.y += mediumMeteor[i].speed.y;
  327. // Collision logic: meteor vs wall
  328. if (mediumMeteor[i].position.x > screenWidth + mediumMeteor[i].radius) mediumMeteor[i].position.x = -(mediumMeteor[i].radius);
  329. else if (mediumMeteor[i].position.x < 0 - mediumMeteor[i].radius) mediumMeteor[i].position.x = screenWidth + mediumMeteor[i].radius;
  330. if (mediumMeteor[i].position.y > screenHeight + mediumMeteor[i].radius) mediumMeteor[i].position.y = -(mediumMeteor[i].radius);
  331. else if (mediumMeteor[i].position.y < 0 - mediumMeteor[i].radius) mediumMeteor[i].position.y = screenHeight + mediumMeteor[i].radius;
  332. }
  333. }
  334. // Meteors logic: small meteors
  335. for (int i = 0; i < MAX_SMALL_METEORS; i++)
  336. {
  337. if (smallMeteor[i].active)
  338. {
  339. // Movement
  340. smallMeteor[i].position.x += smallMeteor[i].speed.x;
  341. smallMeteor[i].position.y += smallMeteor[i].speed.y;
  342. // Collision logic: meteor vs wall
  343. if (smallMeteor[i].position.x > screenWidth + smallMeteor[i].radius) smallMeteor[i].position.x = -(smallMeteor[i].radius);
  344. else if (smallMeteor[i].position.x < 0 - smallMeteor[i].radius) smallMeteor[i].position.x = screenWidth + smallMeteor[i].radius;
  345. if (smallMeteor[i].position.y > screenHeight + smallMeteor[i].radius) smallMeteor[i].position.y = -(smallMeteor[i].radius);
  346. else if (smallMeteor[i].position.y < 0 - smallMeteor[i].radius) smallMeteor[i].position.y = screenHeight + smallMeteor[i].radius;
  347. }
  348. }
  349. // Collision logic: player-shoots vs meteors
  350. for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
  351. {
  352. if ((shoot[i].active))
  353. {
  354. for (int a = 0; a < MAX_BIG_METEORS; a++)
  355. {
  356. if (bigMeteor[a].active && CheckCollisionCircles(shoot[i].position, shoot[i].radius, bigMeteor[a].position, bigMeteor[a].radius))
  357. {
  358. shoot[i].active = false;
  359. shoot[i].lifeSpawn = 0;
  360. bigMeteor[a].active = false;
  361. destroyedMeteorsCount++;
  362. for (int j = 0; j < 2; j ++)
  363. {
  364. if (midMeteorsCount%2 == 0)
  365. {
  366. mediumMeteor[midMeteorsCount].position = (Vector2){bigMeteor[a].position.x, bigMeteor[a].position.y};
  367. mediumMeteor[midMeteorsCount].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1};
  368. }
  369. else
  370. {
  371. mediumMeteor[midMeteorsCount].position = (Vector2){bigMeteor[a].position.x, bigMeteor[a].position.y};
  372. mediumMeteor[midMeteorsCount].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED};
  373. }
  374. mediumMeteor[midMeteorsCount].active = true;
  375. midMeteorsCount ++;
  376. }
  377. //bigMeteor[a].position = (Vector2){-100, -100};
  378. bigMeteor[a].color = RED;
  379. a = MAX_BIG_METEORS;
  380. }
  381. }
  382. for (int b = 0; b < MAX_MEDIUM_METEORS; b++)
  383. {
  384. if (mediumMeteor[b].active && CheckCollisionCircles(shoot[i].position, shoot[i].radius, mediumMeteor[b].position, mediumMeteor[b].radius))
  385. {
  386. shoot[i].active = false;
  387. shoot[i].lifeSpawn = 0;
  388. mediumMeteor[b].active = false;
  389. destroyedMeteorsCount++;
  390. for (int j = 0; j < 2; j ++)
  391. {
  392. if (smallMeteorsCount%2 == 0)
  393. {
  394. smallMeteor[smallMeteorsCount].position = (Vector2){mediumMeteor[b].position.x, mediumMeteor[b].position.y};
  395. smallMeteor[smallMeteorsCount].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED*-1};
  396. }
  397. else
  398. {
  399. smallMeteor[smallMeteorsCount].position = (Vector2){mediumMeteor[b].position.x, mediumMeteor[b].position.y};
  400. smallMeteor[smallMeteorsCount].speed = (Vector2){cos(shoot[i].rotation*DEG2RAD)*METEORS_SPEED, sin(shoot[i].rotation*DEG2RAD)*METEORS_SPEED};
  401. }
  402. smallMeteor[smallMeteorsCount].active = true;
  403. smallMeteorsCount ++;
  404. }
  405. //mediumMeteor[b].position = (Vector2){-100, -100};
  406. mediumMeteor[b].color = GREEN;
  407. b = MAX_MEDIUM_METEORS;
  408. }
  409. }
  410. for (int c = 0; c < MAX_SMALL_METEORS; c++)
  411. {
  412. if (smallMeteor[c].active && CheckCollisionCircles(shoot[i].position, shoot[i].radius, smallMeteor[c].position, smallMeteor[c].radius))
  413. {
  414. shoot[i].active = false;
  415. shoot[i].lifeSpawn = 0;
  416. smallMeteor[c].active = false;
  417. destroyedMeteorsCount++;
  418. smallMeteor[c].color = YELLOW;
  419. // smallMeteor[c].position = (Vector2){-100, -100};
  420. c = MAX_SMALL_METEORS;
  421. }
  422. }
  423. }
  424. }
  425. }
  426. if (destroyedMeteorsCount == MAX_BIG_METEORS + MAX_MEDIUM_METEORS + MAX_SMALL_METEORS) victory = true;
  427. }
  428. else
  429. {
  430. if (IsKeyPressed(KEY_ENTER))
  431. {
  432. InitGame();
  433. gameOver = false;
  434. }
  435. }
  436. }
  437. // Draw game (one frame)
  438. void DrawGame(void)
  439. {
  440. BeginDrawing();
  441. ClearBackground(RAYWHITE);
  442. if (!gameOver)
  443. {
  444. // Draw spaceship
  445. Vector2 v1 = { player.position.x + sinf(player.rotation*DEG2RAD)*(shipHeight), player.position.y - cosf(player.rotation*DEG2RAD)*(shipHeight) };
  446. Vector2 v2 = { player.position.x - cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y - sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
  447. Vector2 v3 = { player.position.x + cosf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2), player.position.y + sinf(player.rotation*DEG2RAD)*(PLAYER_BASE_SIZE/2) };
  448. DrawTriangle(v1, v2, v3, MAROON);
  449. // Draw meteors
  450. for (int i = 0; i < MAX_BIG_METEORS; i++)
  451. {
  452. if (bigMeteor[i].active) DrawCircleV(bigMeteor[i].position, bigMeteor[i].radius, DARKGRAY);
  453. else DrawCircleV(bigMeteor[i].position, bigMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
  454. }
  455. for (int i = 0; i < MAX_MEDIUM_METEORS; i++)
  456. {
  457. if (mediumMeteor[i].active) DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, GRAY);
  458. else DrawCircleV(mediumMeteor[i].position, mediumMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
  459. }
  460. for (int i = 0; i < MAX_SMALL_METEORS; i++)
  461. {
  462. if (smallMeteor[i].active) DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, GRAY);
  463. else DrawCircleV(smallMeteor[i].position, smallMeteor[i].radius, Fade(LIGHTGRAY, 0.3f));
  464. }
  465. // Draw shoot
  466. for (int i = 0; i < PLAYER_MAX_SHOOTS; i++)
  467. {
  468. if (shoot[i].active) DrawCircleV(shoot[i].position, shoot[i].radius, BLACK);
  469. }
  470. if (victory) DrawText("VICTORY", screenWidth/2 - MeasureText("VICTORY", 20)/2, screenHeight/2, 20, LIGHTGRAY);
  471. if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
  472. }
  473. else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
  474. EndDrawing();
  475. }
  476. // Unload game variables
  477. void UnloadGame(void)
  478. {
  479. // TODO: Unload all dynamic loaded data (textures, sounds, models...)
  480. }
  481. // Update and Draw (one frame)
  482. void UpdateDrawFrame(void)
  483. {
  484. UpdateGame();
  485. DrawGame();
  486. }