|
| 1 | +#include "raylib.h" |
| 2 | +#include <vector> |
| 3 | + |
| 4 | +int main() { |
| 5 | + // --- Initialization --- |
| 6 | + const int screenW = 800; |
| 7 | + const int screenH = 450; |
| 8 | + InitWindow(screenW, screenH, "Jump to Survive!"); |
| 9 | + |
| 10 | + // --- Player Setup --- |
| 11 | + Rectangle player = { 100, screenH - 80, 40, 60 }; |
| 12 | + float vy = 0; |
| 13 | + const float gravity = 1000.0f; |
| 14 | + const float jumpImpulse = -450.0f; |
| 15 | + |
| 16 | + // --- Ground Definition --- |
| 17 | + const int groundY = screenH - 20; |
| 18 | + |
| 19 | + // --- Obstacle Management --- |
| 20 | + std::vector<Rectangle> obstacles; |
| 21 | + float spawnTimer = 0.0f; |
| 22 | + float spawnInterval = 1.2f; |
| 23 | + const float obstacleSpeed = 300.0f; |
| 24 | + |
| 25 | + const float minSpawnInterval = 0.8f; |
| 26 | + const float maxSpawnInterval = 1.6f; |
| 27 | + |
| 28 | + const int minObsWidth = 40; |
| 29 | + const int maxObsWidth = 120; |
| 30 | + |
| 31 | + // --- Game State Variables --- |
| 32 | + int score = 0; |
| 33 | + bool gameOver = false; |
| 34 | + |
| 35 | + SetTargetFPS(60); |
| 36 | + |
| 37 | + while (!WindowShouldClose()) { |
| 38 | + float dt = GetFrameTime(); |
| 39 | + |
| 40 | + if (!gameOver) { |
| 41 | + // Jump logic |
| 42 | + if (IsKeyPressed(KEY_SPACE) && player.y + player.height >= groundY) { |
| 43 | + vy = jumpImpulse; |
| 44 | + } |
| 45 | + vy += gravity * dt; |
| 46 | + player.y += vy * dt; |
| 47 | + if (player.y + player.height > groundY) { |
| 48 | + player.y = groundY - player.height; |
| 49 | + vy = 0; |
| 50 | + } |
| 51 | + |
| 52 | + // Spawn obstacles with random width & interval |
| 53 | + spawnTimer += dt; |
| 54 | + if (spawnTimer >= spawnInterval) { |
| 55 | + spawnTimer = 0.0f; |
| 56 | + // recalc next interval |
| 57 | + spawnInterval = GetRandomValue(int(minSpawnInterval*100), int(maxSpawnInterval*100)) / 100.0f; |
| 58 | + // random width |
| 59 | + int w = GetRandomValue(minObsWidth, maxObsWidth); |
| 60 | + obstacles.push_back({ float(screenW), float(groundY - 40), float(w), 40.0f }); |
| 61 | + } |
| 62 | + |
| 63 | + // Move & collide obstacles |
| 64 | + for (int i = 0; i < (int)obstacles.size(); i++) { |
| 65 | + obstacles[i].x -= obstacleSpeed * dt; |
| 66 | + if (CheckCollisionRecs(player, obstacles[i])) { |
| 67 | + gameOver = true; |
| 68 | + } |
| 69 | + } |
| 70 | + // Remove off-screen & score |
| 71 | + if (!obstacles.empty() && obstacles.front().x + obstacles.front().width < 0) { |
| 72 | + obstacles.erase(obstacles.begin()); |
| 73 | + score++; |
| 74 | + } |
| 75 | + } |
| 76 | + else { |
| 77 | + if (IsKeyPressed(KEY_R)) { |
| 78 | + // reset everything |
| 79 | + player.y = screenH - 80; |
| 80 | + vy = 0; |
| 81 | + obstacles.clear(); |
| 82 | + spawnTimer = 0.0f; |
| 83 | + spawnInterval = 1.2f; |
| 84 | + score = 0; |
| 85 | + gameOver = false; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // --- Drawing --- |
| 90 | + BeginDrawing(); |
| 91 | + ClearBackground(RAYWHITE); |
| 92 | + |
| 93 | + DrawRectangle(0, groundY, screenW, 20, DARKGRAY); |
| 94 | + DrawRectangleRec(player, BLUE); |
| 95 | + for (auto &obs : obstacles) DrawRectangleRec(obs, RED); |
| 96 | + |
| 97 | + DrawText(TextFormat("Score: %d", score), 10, 10, 20, BLACK); |
| 98 | + |
| 99 | + if (gameOver) { |
| 100 | + DrawText("GAME OVER! Press R to restart", 200, screenH/2 - 20, 20, MAROON); |
| 101 | + } |
| 102 | + |
| 103 | + EndDrawing(); |
| 104 | + } |
| 105 | + |
| 106 | + CloseWindow(); |
| 107 | + return 0; |
| 108 | +} |
0 commit comments