25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

495 satır
20 KiB

9 yıl önce
9 yıl önce
  1. /*******************************************************************************************
  2. *
  3. * raylib game - Dr. Turtle & Mr. Gamera
  4. *
  5. * Welcome to raylib!
  6. *
  7. * To test examples, just press F6 and execute raylib_compile_execute script
  8. * Note that compiled executable is placed in the same folder as .c file
  9. *
  10. * You can find all basic examples on C:\raylib\raylib\examples folder or
  11. * raylib official webpage: www.raylib.com
  12. *
  13. * Enjoy using raylib. :)
  14. *
  15. * This game has been created using raylib 1.6 (www.raylib.com)
  16. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  17. *
  18. * Copyright (c) 2014 Ramon Santamaria (@raysan5)
  19. *
  20. ********************************************************************************************/
  21. #include "raylib.h"
  22. #include <math.h> // Used for sinf()
  23. #define MAX_ENEMIES 10
  24. typedef enum { TITLE, GAMEPLAY, ENDING } GameScreen;
  25. int main()
  26. {
  27. // Initialization
  28. //--------------------------------------------------------------------------------------
  29. const int screenWidth = 1280;
  30. const int screenHeight = 720;
  31. // Init window
  32. InitWindow(screenWidth, screenHeight, "Dr. Turtle & Mr. GAMERA");
  33. // Initialize audio device
  34. InitAudioDevice();
  35. // Load game resources: textures
  36. Texture2D sky = LoadTexture("resources/sky.png");
  37. Texture2D mountains = LoadTexture("resources/mountains.png");
  38. Texture2D sea = LoadTexture("resources/sea.png");
  39. Texture2D title = LoadTexture("resources/title.png");
  40. Texture2D turtle = LoadTexture("resources/turtle.png");
  41. Texture2D gamera = LoadTexture("resources/gamera.png");
  42. Texture2D shark = LoadTexture("resources/shark.png");
  43. Texture2D orca = LoadTexture("resources/orca.png");
  44. Texture2D swhale = LoadTexture("resources/swhale.png");
  45. Texture2D fish = LoadTexture("resources/fish.png");
  46. Texture2D gframe = LoadTexture("resources/gframe.png");
  47. // Load game resources: fonts
  48. Font font = LoadFont("resources/komika.png");
  49. // Load game resources: sounds
  50. Sound eat = LoadSound("resources/eat.wav");
  51. Sound die = LoadSound("resources/die.wav");
  52. Sound growl = LoadSound("resources/gamera.wav");
  53. // Load music stream and start playing music
  54. Music music = LoadMusicStream("resources/speeding.ogg");
  55. PlayMusicStream(music);
  56. // Define scrolling variables
  57. int backScrolling = 0;
  58. int seaScrolling = 0;
  59. // Define current screen
  60. GameScreen currentScreen = TITLE;
  61. // Define player variables
  62. int playerRail = 1;
  63. Rectangle playerBounds = { 30 + 14, playerRail*120 + 90 + 14, 100, 100 };
  64. bool gameraMode = false;
  65. // Define enemies variables
  66. Rectangle enemyBounds[MAX_ENEMIES];
  67. int enemyRail[MAX_ENEMIES];
  68. int enemyType[MAX_ENEMIES];
  69. bool enemyActive[MAX_ENEMIES];
  70. float enemySpeed = 10;
  71. // Init enemies variables
  72. for (int i = 0; i < MAX_ENEMIES; i++)
  73. {
  74. // Define enemy type (all same probability)
  75. //enemyType[i] = GetRandomValue(0, 3);
  76. // Probability system for enemies type
  77. int enemyProb = GetRandomValue(0, 100);
  78. if (enemyProb < 30) enemyType[i] = 0;
  79. else if (enemyProb < 60) enemyType[i] = 1;
  80. else if (enemyProb < 90) enemyType[i] = 2;
  81. else enemyType[i] = 3;
  82. // define enemy rail
  83. enemyRail[i] = GetRandomValue(0, 4);
  84. // Make sure not two consecutive enemies in the same row
  85. if (i > 0) while (enemyRail[i] == enemyRail[i - 1]) enemyRail[i] = GetRandomValue(0, 4);
  86. enemyBounds[i] = (Rectangle){ screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 };
  87. enemyActive[i] = false;
  88. }
  89. // Define additional game variables
  90. int score = 0;
  91. float distance = 0.0f;
  92. int hiscore = 0;
  93. float hidistance = 0.0f;
  94. int foodBar = 0;
  95. int framesCounter = 0;
  96. unsigned char blue = 200;
  97. float timeCounter = 0;
  98. SetTargetFPS(60); // Setup game frames per second
  99. //--------------------------------------------------------------------------------------
  100. // Main game loop
  101. while (!WindowShouldClose()) // Detect window close button or ESC key
  102. {
  103. // Update
  104. //----------------------------------------------------------------------------------
  105. UpdateMusicStream(music); // Refill music stream buffers (if required)
  106. framesCounter++;
  107. // Sea color tint effect
  108. blue = 210 + 25 * sinf(timeCounter);
  109. timeCounter += 0.01;
  110. // Game screens management
  111. switch (currentScreen)
  112. {
  113. case TITLE:
  114. {
  115. // Sea scrolling
  116. seaScrolling -= 2;
  117. if (seaScrolling <= -screenWidth) seaScrolling = 0;
  118. // Press enter to change to gameplay screen
  119. if (IsKeyPressed(KEY_ENTER))
  120. {
  121. currentScreen = GAMEPLAY;
  122. framesCounter = 0;
  123. }
  124. } break;
  125. case GAMEPLAY:
  126. {
  127. // Background scrolling logic
  128. backScrolling--;
  129. if (backScrolling <= -screenWidth) backScrolling = 0;
  130. // Sea scrolling logic
  131. seaScrolling -= (enemySpeed - 2);
  132. if (seaScrolling <= -screenWidth) seaScrolling = 0;
  133. // Player movement logic
  134. if (IsKeyPressed(KEY_DOWN)) playerRail++;
  135. else if (IsKeyPressed(KEY_UP)) playerRail--;
  136. // Check player not out of rails
  137. if (playerRail > 4) playerRail = 4;
  138. else if (playerRail < 0) playerRail = 0;
  139. // Update player bounds
  140. playerBounds = (Rectangle){ 30 + 14, playerRail*120 + 90 + 14, 100, 100 };
  141. // Enemies activation logic (every 40 frames)
  142. if (framesCounter > 40)
  143. {
  144. for (int i = 0; i < MAX_ENEMIES; i++)
  145. {
  146. if (enemyActive[i] == false)
  147. {
  148. enemyActive[i] = true;
  149. i = MAX_ENEMIES;
  150. }
  151. }
  152. framesCounter = 0;
  153. }
  154. // Enemies logic
  155. for (int i = 0; i < MAX_ENEMIES; i++)
  156. {
  157. if (enemyActive[i])
  158. {
  159. enemyBounds[i].x -= enemySpeed;
  160. }
  161. // Check enemies out of screen
  162. if (enemyBounds[i].x <= 0 - 128)
  163. {
  164. enemyActive[i] = false;
  165. enemyType[i] = GetRandomValue(0, 3);
  166. enemyRail[i] = GetRandomValue(0, 4);
  167. // Make sure not two consecutive enemies in the same row
  168. if (i > 0) while (enemyRail[i] == enemyRail[i - 1]) enemyRail[i] = GetRandomValue(0, 4);
  169. enemyBounds[i] = (Rectangle){ screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 };
  170. }
  171. }
  172. if (!gameraMode) enemySpeed += 0.005;
  173. // Check collision player vs enemies
  174. for (int i = 0; i < MAX_ENEMIES; i++)
  175. {
  176. if (enemyActive[i])
  177. {
  178. if (CheckCollisionRecs(playerBounds, enemyBounds[i]))
  179. {
  180. if (enemyType[i] < 3) // Bad enemies
  181. {
  182. if (gameraMode)
  183. {
  184. if (enemyType[i] == 0) score += 50;
  185. else if (enemyType[i] == 1) score += 150;
  186. else if (enemyType[i] == 2) score += 300;
  187. foodBar += 15;
  188. enemyActive[i] = false;
  189. // After enemy deactivation, reset enemy parameters to be reused
  190. enemyType[i] = GetRandomValue(0, 3);
  191. enemyRail[i] = GetRandomValue(0, 4);
  192. // Make sure not two consecutive enemies in the same row
  193. if (i > 0) while (enemyRail[i] == enemyRail[i - 1]) enemyRail[i] = GetRandomValue(0, 4);
  194. enemyBounds[i] = (Rectangle){ screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 };
  195. PlaySound(eat);
  196. }
  197. else
  198. {
  199. // Player die logic
  200. PlaySound(die);
  201. currentScreen = ENDING;
  202. framesCounter = 0;
  203. // Save hiscore and hidistance for next game
  204. if (score > hiscore) hiscore = score;
  205. if (distance > hidistance) hidistance = distance;
  206. }
  207. }
  208. else // Sweet fish
  209. {
  210. enemyActive[i] = false;
  211. enemyType[i] = GetRandomValue(0, 3);
  212. enemyRail[i] = GetRandomValue(0, 4);
  213. // Make sure not two consecutive enemies in the same row
  214. if (i > 0) while (enemyRail[i] == enemyRail[i - 1]) enemyRail[i] = GetRandomValue(0, 4);
  215. enemyBounds[i] = (Rectangle){ screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 };
  216. if (!gameraMode) foodBar += 80;
  217. else foodBar += 25;
  218. score += 10;
  219. if (foodBar == 400)
  220. {
  221. gameraMode = true;
  222. PlaySound(growl);
  223. }
  224. PlaySound(eat);
  225. }
  226. }
  227. }
  228. }
  229. // Gamera mode logic
  230. if (gameraMode)
  231. {
  232. foodBar--;
  233. if (foodBar <= 0)
  234. {
  235. gameraMode = false;
  236. enemySpeed -= 2;
  237. if (enemySpeed < 10) enemySpeed = 10;
  238. }
  239. }
  240. // Update distance counter
  241. distance += 0.5f;
  242. } break;
  243. case ENDING:
  244. {
  245. // Press enter to play again
  246. if (IsKeyPressed(KEY_ENTER))
  247. {
  248. currentScreen = GAMEPLAY;
  249. // Reset player
  250. playerRail = 1;
  251. playerBounds = (Rectangle){ 30 + 14, playerRail*120 + 90 + 14, 100, 100 };
  252. gameraMode = false;
  253. // Reset enemies data
  254. for (int i = 0; i < MAX_ENEMIES; i++)
  255. {
  256. int enemyProb = GetRandomValue(0, 100);
  257. if (enemyProb < 30) enemyType[i] = 0;
  258. else if (enemyProb < 60) enemyType[i] = 1;
  259. else if (enemyProb < 90) enemyType[i] = 2;
  260. else enemyType[i] = 3;
  261. //enemyType[i] = GetRandomValue(0, 3);
  262. enemyRail[i] = GetRandomValue(0, 4);
  263. // Make sure not two consecutive enemies in the same row
  264. if (i > 0) while (enemyRail[i] == enemyRail[i - 1]) enemyRail[i] = GetRandomValue(0, 4);
  265. enemyBounds[i] = (Rectangle){ screenWidth + 14, 120*enemyRail[i] + 90 + 14, 100, 100 };
  266. enemyActive[i] = false;
  267. }
  268. enemySpeed = 10;
  269. // Reset game variables
  270. score = 0;
  271. distance = 0.0;
  272. foodBar = 0;
  273. framesCounter = 0;
  274. }
  275. } break;
  276. default: break;
  277. }
  278. //----------------------------------------------------------------------------------
  279. // Draw
  280. //----------------------------------------------------------------------------------
  281. BeginDrawing();
  282. ClearBackground(RAYWHITE);
  283. // Draw background (common to all screens)
  284. DrawTexture(sky, 0, 0, WHITE);
  285. DrawTexture(mountains, backScrolling, 0, WHITE);
  286. DrawTexture(mountains, screenWidth + backScrolling, 0, WHITE);
  287. if (!gameraMode)
  288. {
  289. DrawTexture(sea, seaScrolling, 0, (Color){ 16, 189, blue, 255});
  290. DrawTexture(sea, screenWidth + seaScrolling, 0, (Color){ 16, 189, blue, 255});
  291. }
  292. else
  293. {
  294. DrawTexture(sea, seaScrolling, 0, (Color){ 255, 113, 66, 255});
  295. DrawTexture(sea, screenWidth + seaScrolling, 0, (Color){ 255, 113, 66, 255});
  296. }
  297. switch (currentScreen)
  298. {
  299. case TITLE:
  300. {
  301. // Draw title
  302. DrawTexture(title, screenWidth/2 - title.width/2, screenHeight/2 - title.height/2 - 80, WHITE);
  303. // Draw blinking text
  304. if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER", (Vector2){ screenWidth/2 - 150, 480 }, font.baseSize, 0, WHITE);
  305. } break;
  306. case GAMEPLAY:
  307. {
  308. // Draw water lines
  309. for (int i = 0; i < 5; i++) DrawRectangle(0, i*120 + 120, screenWidth, 110, Fade(SKYBLUE, 0.1f));
  310. // Draw player
  311. if (!gameraMode) DrawTexture(turtle, playerBounds.x - 14, playerBounds.y - 14, WHITE);
  312. else DrawTexture(gamera, playerBounds.x - 64, playerBounds.y - 64, WHITE);
  313. // Draw player bounding box
  314. //if (!gameraMode) DrawRectangleRec(playerBounds, Fade(GREEN, 0.4f));
  315. //else DrawRectangleRec(playerBounds, Fade(ORANGE, 0.4f));
  316. // Draw enemies
  317. for (int i = 0; i < MAX_ENEMIES; i++)
  318. {
  319. if (enemyActive[i])
  320. {
  321. // Draw enemies
  322. switch(enemyType[i])
  323. {
  324. case 0: DrawTexture(shark, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break;
  325. case 1: DrawTexture(orca, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break;
  326. case 2: DrawTexture(swhale, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break;
  327. case 3: DrawTexture(fish, enemyBounds[i].x - 14, enemyBounds[i].y - 14, WHITE); break;
  328. default: break;
  329. }
  330. // Draw enemies bounding boxes
  331. /*
  332. switch(enemyType[i])
  333. {
  334. case 0: DrawRectangleRec(enemyBounds[i], Fade(RED, 0.5f)); break;
  335. case 1: DrawRectangleRec(enemyBounds[i], Fade(RED, 0.5f)); break;
  336. case 2: DrawRectangleRec(enemyBounds[i], Fade(RED, 0.5f)); break;
  337. case 3: DrawRectangleRec(enemyBounds[i], Fade(GREEN, 0.5f)); break;
  338. default: break;
  339. }
  340. */
  341. }
  342. }
  343. // Draw gameplay interface
  344. DrawRectangle(20, 20, 400, 40, Fade(GRAY, 0.4f));
  345. DrawRectangle(20, 20, foodBar, 40, ORANGE);
  346. DrawRectangleLines(20, 20, 400, 40, BLACK);
  347. DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ screenWidth - 300, 20 }, font.baseSize, -2, ORANGE);
  348. DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 550, 20 }, font.baseSize, -2, ORANGE);
  349. if (gameraMode)
  350. {
  351. DrawText("GAMERA MODE", 60, 22, 40, GRAY);
  352. DrawTexture(gframe, 0, 0, Fade(WHITE, 0.5f));
  353. }
  354. } break;
  355. case ENDING:
  356. {
  357. // Draw a transparent black rectangle that covers all screen
  358. DrawRectangle(0, 0, screenWidth, screenHeight, Fade(BLACK, 0.4f));
  359. DrawTextEx(font, "GAME OVER", (Vector2){ 300, 160 }, font.baseSize*3, -2, MAROON);
  360. DrawTextEx(font, FormatText("SCORE: %04i", score), (Vector2){ 680, 350 }, font.baseSize, -2, GOLD);
  361. DrawTextEx(font, FormatText("DISTANCE: %04i", (int)distance), (Vector2){ 290, 350 }, font.baseSize, -2, GOLD);
  362. DrawTextEx(font, FormatText("HISCORE: %04i", hiscore), (Vector2){ 665, 400 }, font.baseSize, -2, ORANGE);
  363. DrawTextEx(font, FormatText("HIDISTANCE: %04i", (int)hidistance), (Vector2){ 270, 400 }, font.baseSize, -2, ORANGE);
  364. // Draw blinking text
  365. if ((framesCounter/30) % 2) DrawTextEx(font, "PRESS ENTER to REPLAY", (Vector2){ screenWidth/2 - 250, 520 }, font.baseSize, -2, LIGHTGRAY);
  366. } break;
  367. default: break;
  368. }
  369. EndDrawing();
  370. //----------------------------------------------------------------------------------
  371. }
  372. // De-Initialization
  373. //--------------------------------------------------------------------------------------
  374. // Unload textures
  375. UnloadTexture(sky);
  376. UnloadTexture(mountains);
  377. UnloadTexture(sea);
  378. UnloadTexture(gframe);
  379. UnloadTexture(title);
  380. UnloadTexture(turtle);
  381. UnloadTexture(shark);
  382. UnloadTexture(orca);
  383. UnloadTexture(swhale);
  384. UnloadTexture(fish);
  385. UnloadTexture(gamera);
  386. // Unload font texture
  387. UnloadFont(font);
  388. // Unload sounds
  389. UnloadSound(eat);
  390. UnloadSound(die);
  391. UnloadSound(growl);
  392. UnloadMusicStream(music); // Unload music
  393. CloseAudioDevice(); // Close audio device
  394. CloseWindow(); // Close window and OpenGL context
  395. //--------------------------------------------------------------------------------------
  396. return 0;
  397. }