#include <SFML/Graphics.hpp> #include #include #include

const int WINDOW_WIDTH = 800; const int WINDOW_HEIGHT = 200; const int GROUND_Y = 150; const float GRAVITY = 0.5f; const float JUMP_FORCE = 10.0f; // 初始障碍物生成间隔 const float INITIAL_SPAWN_TIME = 2.0f; // 障碍物生成间隔减少的速率 const float SPAWN_TIME_DECREASE_RATE = 0.01f; // 初始障碍物移动速度 const float INITIAL_OBSTACLE_VELOCITY = -3.0f; // 障碍物移动速度增加的速率 const float OBSTACLE_VELOCITY_INCREASE_RATE = 0.05f; // 增加速度增加的幅度 // 初始障碍物尺寸 const sf::Vector2f INITIAL_OBSTACLE_SIZE = sf::Vector2f(20, 40); // 障碍物尺寸增加的速率 const float OBSTACLE_SIZE_INCREASE_RATE = 0.1f; // 得分增加间隔(秒) const float SCORE_INCREASE_INTERVAL = 1.0f; // 速度增加间隔(秒) const float VELOCITY_INCREASE_INTERVAL = 1.0f; // 加快速度增加的频率

class Dinosaur { public: sf::RectangleShape shape; float velocityY; bool isJumping;

Dinosaur() {
    shape.setSize(sf::Vector2f(30, 50));
    shape.setFillColor(sf::Color::Green);
    shape.setPosition(50, GROUND_Y - shape.getSize().y);
    velocityY = 0;
    isJumping = false;
}

void jump() {
    if (!isJumping) {
        velocityY = -JUMP_FORCE;
        isJumping = true;
    }
}

void update() {
    if (isJumping) {
        velocityY += GRAVITY;
        shape.move(0, velocityY);

        if (shape.getPosition().y >= GROUND_Y - shape.getSize().y) {
            shape.setPosition(shape.getPosition().x, GROUND_Y - shape.getSize().y);
            isJumping = false;
        }
    }
}

};

class Obstacle { public: sf::RectangleShape shape; float velocityX;

Obstacle(float velX, const sf::Vector2f& size) {
    shape.setSize(size);
    shape.setFillColor(sf::Color::Red);
    shape.setPosition(WINDOW_WIDTH, GROUND_Y - shape.getSize().y);
    velocityX = velX;
}

void update() {
    shape.move(velocityX, 0);
}

};

bool isCollision(const sf::RectangleShape& dino, const sf::RectangleShape& obstacle) { return dino.getGlobalBounds().intersects(obstacle.getGlobalBounds()); }

int main() { sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Google Dinosaur Game");

Dinosaur dino;
std::vector<Obstacle> obstacles;
sf::Clock clock;
float spawnTimer = 0;
float currentSpawnTime = INITIAL_SPAWN_TIME;
float currentObstacleVelocity = INITIAL_OBSTACLE_VELOCITY;
sf::Vector2f currentObstacleSize = INITIAL_OBSTACLE_SIZE;
// 得分变量
int score = 0;
// 得分计时器
float scoreTimer = 0;
// 速度增加计时器
float velocityTimer = 0;

// 加载字体
sf::Font font;
if (!font.loadFromFile("arial.ttf")) {
    std::cerr << "Failed to load font!" << std::endl;
    return -1;
}

// 创建得分文本对象
sf::Text scoreText;
scoreText.setFont(font);
scoreText.setCharacterSize(24);
scoreText.setFillColor(sf::Color::Black);
scoreText.setPosition(10, 10);

// 创建游戏结束文本对象
sf::Text gameOverText;
gameOverText.setFont(font);
gameOverText.setCharacterSize(48);
gameOverText.setFillColor(sf::Color::Red);
gameOverText.setString("Game Over!");
gameOverText.setPosition((WINDOW_WIDTH - gameOverText.getLocalBounds().width) / 2, 
                         (WINDOW_HEIGHT - gameOverText.getLocalBounds().height) / 2 - 50);

// 创建最终得分文本对象
sf::Text finalScoreText;
finalScoreText.setFont(font);
finalScoreText.setCharacterSize(36);
finalScoreText.setFillColor(sf::Color::Black);
finalScoreText.setPosition((WINDOW_WIDTH - finalScoreText.getLocalBounds().width) / 2, 
                           (WINDOW_HEIGHT - finalScoreText.getLocalBounds().height) / 2 + 20);

bool gameOver = false;

while (window.isOpen()) {
    sf::Event event;
    while (window.pollEvent(event)) {
        if (event.type == sf::Event::Closed)
            window.close();
        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space &&!gameOver) {
            dino.jump();
        }
    }

    if (!gameOver) {
        float deltaTime = clock.restart().asSeconds();
        spawnTimer += deltaTime;
        scoreTimer += deltaTime;
        velocityTimer += deltaTime;

        // 定时增加得分
        if (scoreTimer >= SCORE_INCREASE_INTERVAL) {
            score++;
            scoreTimer = 0;
        }

        // 更新得分文本
        scoreText.setString("Score: " + std::to_string(score));

        // 定时增加障碍物速度
        if (velocityTimer >= VELOCITY_INCREASE_INTERVAL) {
            currentObstacleVelocity -= OBSTACLE_VELOCITY_INCREASE_RATE;
            velocityTimer = 0;
        }

        if (spawnTimer >= currentSpawnTime) {
            obstacles.emplace_back(currentObstacleVelocity, currentObstacleSize);
            spawnTimer = 0;
            // 缩短障碍物生成间隔
            currentSpawnTime = std::max(currentSpawnTime - SPAWN_TIME_DECREASE_RATE, 0.5f);
            // 增加障碍物尺寸
            currentObstacleSize.x += OBSTACLE_SIZE_INCREASE_RATE;
            currentObstacleSize.y += OBSTACLE_SIZE_INCREASE_RATE;
        }

        dino.update();

        for (auto it = obstacles.begin(); it != obstacles.end();) {
            it->update();
            if (it->shape.getPosition().x + it->shape.getSize().x < 0) {
                it = obstacles.erase(it);
            }
            else if (isCollision(dino.shape, it->shape)) {
                gameOver = true;
                // 设置最终得分文本
                finalScoreText.setString("Final Score: " + std::to_string(score));
                finalScoreText.setPosition((WINDOW_WIDTH - finalScoreText.getLocalBounds().width) / 2, 
                                           (WINDOW_HEIGHT - finalScoreText.getLocalBounds().height) / 2 + 20);
                break;
            }
            else {
                ++it;
            }
        }
    }

    window.clear(sf::Color::White);

    if (!gameOver) {
        sf::RectangleShape ground(sf::Vector2f(WINDOW_WIDTH, 10));
        ground.setFillColor(sf::Color::Black);
        ground.setPosition(0, GROUND_Y);
        window.draw(ground);

        window.draw(dino.shape);
        for (const auto& obstacle : obstacles) {
            window.draw(obstacle.shape);
        }

        // 绘制得分文本
        window.draw(scoreText);
    }
    else {
        // 绘制游戏结束文本和最终得分文本
        window.draw(gameOverText);
        window.draw(finalScoreText);
    }

    window.display();
}

return 0;

}

0 条评论

目前还没有评论...

信息

ID
1976
时间
1000ms
内存
256MiB
难度
5
标签
递交数
64
已通过
25
上传者