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ů.

211 řádky
7.5 KiB

  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.1 (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. #define MAX_ENEMIES 10
  23. typedef enum { TITLE, GAMEPLAY, ENDING } GameScreen;
  24. int main()
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. const int screenWidth = 1280;
  29. const int screenHeight = 720;
  30. // Init window
  31. InitWindow(screenWidth, screenHeight, "Dr. Turtle & Mr. GAMERA");
  32. // Load game resources: textures
  33. Texture2D sky = LoadTexture("resources/sky.png");
  34. Texture2D mountains = LoadTexture("resources/mountains.png");
  35. Texture2D sea = LoadTexture("resources/sea.png");
  36. Texture2D title = LoadTexture("resources/title.png");
  37. Texture2D turtle = LoadTexture("resources/turtle.png");
  38. Texture2D gamera = LoadTexture("resources/gamera.png");
  39. // Define scrolling variables
  40. int backScrolling = 0;
  41. int seaScrolling = 0;
  42. // Define current screen
  43. GameScreen currentScreen = TITLE;
  44. // Define player variables
  45. int playerRail = 1;
  46. Rectangle playerBounds = { 30 + 14, playerRail*120 + 90 + 14, 100, 100 };
  47. bool gameraMode = false;
  48. // Define additional game variables
  49. int framesCounter = 0;
  50. SetTargetFPS(60); // Setup game frames per second
  51. //--------------------------------------------------------------------------------------
  52. // Main game loop
  53. while (!WindowShouldClose()) // Detect window close button or ESC key
  54. {
  55. // Update
  56. //----------------------------------------------------------------------------------
  57. framesCounter++;
  58. // Game screens management
  59. switch (currentScreen)
  60. {
  61. case TITLE:
  62. {
  63. // Sea scrolling
  64. seaScrolling -= 2;
  65. if (seaScrolling <= -screenWidth) seaScrolling = 0;
  66. // Press enter to change to gameplay screen
  67. if (IsKeyPressed(KEY_ENTER))
  68. {
  69. currentScreen = GAMEPLAY;
  70. framesCounter = 0;
  71. }
  72. } break;
  73. case GAMEPLAY:
  74. {
  75. // Background scrolling logic
  76. backScrolling--;
  77. if (backScrolling <= -screenWidth) backScrolling = 0;
  78. // Sea scrolling logic
  79. seaScrolling -= 8;
  80. if (seaScrolling <= -screenWidth) seaScrolling = 0;
  81. // Player movement logic
  82. if (IsKeyPressed(KEY_DOWN)) playerRail++;
  83. else if (IsKeyPressed(KEY_UP)) playerRail--;
  84. // Check player not out of rails
  85. if (playerRail > 4) playerRail = 4;
  86. else if (playerRail < 0) playerRail = 0;
  87. // Update player bounds
  88. playerBounds = (Rectangle){ 30 + 14, playerRail*120 + 90 + 14, 100, 100 };
  89. if (IsKeyPressed(KEY_SPACE)) gameraMode = !gameraMode;
  90. if (IsKeyPressed(KEY_ENTER)) currentScreen = ENDING;
  91. } break;
  92. case ENDING:
  93. {
  94. // Press enter to play again
  95. if (IsKeyPressed(KEY_ENTER))
  96. {
  97. currentScreen = GAMEPLAY;
  98. // Reset player
  99. playerRail = 1;
  100. playerBounds = (Rectangle){ 30 + 14, playerRail*120 + 90 + 14, 100, 100 };
  101. gameraMode = false;
  102. framesCounter = 0;
  103. }
  104. } break;
  105. default: break;
  106. }
  107. //----------------------------------------------------------------------------------
  108. // Draw
  109. //----------------------------------------------------------------------------------
  110. BeginDrawing();
  111. ClearBackground(RAYWHITE);
  112. // Draw background (common to all screens)
  113. DrawTexture(sky, 0, 0, WHITE);
  114. DrawTexture(mountains, backScrolling, 0, WHITE);
  115. DrawTexture(mountains, screenWidth + backScrolling, 0, WHITE);
  116. if (!gameraMode)
  117. {
  118. DrawTexture(sea, seaScrolling, 0, BLUE);
  119. DrawTexture(sea, screenWidth + seaScrolling, 0, BLUE);
  120. }
  121. else
  122. {
  123. DrawTexture(sea, seaScrolling, 0, RED);
  124. DrawTexture(sea, screenWidth + seaScrolling, 0, RED);
  125. }
  126. switch (currentScreen)
  127. {
  128. case TITLE:
  129. {
  130. // Draw title
  131. //DrawTexture(title, screenWidth/2 - title.width/2, screenHeight/2 - title.height/2 - 80, WHITE);
  132. DrawRectangle(380, 140, 500, 300, GRAY);
  133. // Draw blinking text
  134. if ((framesCounter/30) % 2) DrawText("PRESS ENTER", 480, 480, 40, BLACK);
  135. } break;
  136. case GAMEPLAY:
  137. {
  138. // Draw player
  139. //if (!gameraMode) DrawTexture(turtle, playerBounds.x - 14, playerBounds.y - 14, WHITE);
  140. //else DrawTexture(gamera, playerBounds.x - 64, playerBounds.y - 64, WHITE);
  141. // Draw player bounding box
  142. if (!gameraMode) DrawRectangleRec(playerBounds, GREEN);
  143. else DrawRectangleRec(playerBounds, ORANGE);
  144. } break;
  145. case ENDING:
  146. {
  147. // Draw a transparent black rectangle that covers all screen
  148. DrawRectangle(0, 0, screenWidth, screenHeight, Fade(BLACK, 0.4f));
  149. DrawText("GAME OVER", 300, 200, 100, MAROON);
  150. // Draw blinking text
  151. if ((framesCounter/30) % 2) DrawText("PRESS ENTER to REPLAY", 400, 420, 30, LIGHTGRAY);
  152. } break;
  153. default: break;
  154. }
  155. EndDrawing();
  156. //----------------------------------------------------------------------------------
  157. }
  158. // De-Initialization
  159. //--------------------------------------------------------------------------------------
  160. // Unload textures
  161. UnloadTexture(sky);
  162. UnloadTexture(mountains);
  163. UnloadTexture(sea);
  164. UnloadTexture(title);
  165. UnloadTexture(turtle);
  166. UnloadTexture(gamera);
  167. CloseWindow(); // Close window and OpenGL context
  168. //--------------------------------------------------------------------------------------
  169. return 0;
  170. }