C++ 谷歌小恐龙源码

· · 科技·工程

希望各位 OIer 可以早日把信竞变为电竞。

使用 TDM-GCC 4.9.2 32-bit Release Windows 11 x64 测试运行成功。若有 bug 请站内私信我或在该文章下评论。您必须使用 ISO C++ 11 或以上标准编译,因为该代码使用了 C++ 的新特性。或者您可以在下方直接下载 exe 文件。

直接下载:https://duowanmc.lanzouw.com/i5W2g26gpnwf 密码:d74p

笔者最高分是 27.

操作说明:

键盘按键 对应作用
任意键 开始游戏
W 跳跃
Ctrl+C 快速退出
#include <iostream>
#include <conio.h>  // For _kbhit() and _getch()
#include <vector>
#include <chrono>
#include <thread>
#include <cstdlib>  // For rand() and srand()
#include <ctime>    // For time()

const int MAP_LENGTH = 50; // 地图长度
const int MAP_HEIGHT = 10; // 地图高度
const int INITIAL_SPEED = 100; // 初始速度(毫秒)
const int SPEED_INCREMENT_INTERVAL = 5000; // 每隔5秒加速一次
const int MAX_SPEED = 50; // 最大速度(毫秒)
float hard;

class Dinosaur {
public:
    int x, y;       // 位置
    bool jumping;   // 跳跃状态
    int jumpHeight; // 跳跃高度

    Dinosaur() : x(5), y(0), jumping(false), jumpHeight(0) {}

    void jump() {
        if (!jumping) {
            jumping = true;
            jumpHeight = 5; // 跳跃高度设为5
        }
    }

    void update() {
        if (jumping) {
            if (jumpHeight > 0) {
                y++;
                jumpHeight--;
            } else {
                y--;
                if (y == 0) {
                    jumping = false;
                }
            }
        }
    }
};

class Obstacle {
public:
    int x, y; // 障碍物位置

    Obstacle(int posX, int posY) : x(posX), y(posY) {}

    void update() {
        x--;
    }
};

class Game {
public:
    Dinosaur dino;
    std::vector<Obstacle> obstacles;
    int score;
    bool gameOver;
    int speed;
    std::chrono::time_point<std::chrono::steady_clock> lastSpeedIncrease;

    Game() : score(0), gameOver(false), speed(INITIAL_SPEED) {
        srand(static_cast<unsigned>(time(0))); // 初始化随机数种子
        obstacles.push_back(Obstacle(MAP_LENGTH, 0)); // 在地图末端初始化一个障碍物
        lastSpeedIncrease = std::chrono::steady_clock::now();
    }

    void processInput() {
        if (_kbhit()) {
            char ch = _getch();
            if (ch == 'W') {
                dino.jump();
            }
        }
    }

    void update() {
        dino.update();
        for (auto& obstacle : obstacles) {
            obstacle.update();
        }

        // 检查碰撞
        for (const auto& obstacle : obstacles) {
            if (obstacle.x == 5 && obstacle.y == dino.y) {
                gameOver = true;
            }
        }

        // 移除离开屏幕的障碍物并随机添加新障碍物
        if (!obstacles.empty() && obstacles.front().x < 0) {
            obstacles.erase(obstacles.begin());
            score++;
            if (rand() % 2 == 0) { // 50% 几率添加新障碍物
                obstacles.push_back(Obstacle(MAP_LENGTH + rand() % 10, 0)); // 随机生成障碍物位置
            }
        }

        // 在地图最后四分之一位置没有障碍物时添加新的障碍物
        if (obstacles.empty() || obstacles.back().x < MAP_LENGTH * 3 / 4) {
            obstacles.push_back(Obstacle(MAP_LENGTH + rand() % 10, 0));
        }

        // 每隔一段时间增加速度
        auto now = std::chrono::steady_clock::now();
        auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - lastSpeedIncrease).count();
        if (duration >= SPEED_INCREMENT_INTERVAL && speed > MAX_SPEED) {
            speed -= 10; // 减少等待时间(增加速度)
            lastSpeedIncrease = now;
        }
    }

    void render() {
        system("cls");
        for (int y = MAP_HEIGHT - 1; y >= 0; y--) { // 反向渲染纵向地图
            for (int x = 0; x < MAP_LENGTH; x++) {
                if (x == 5 && y == dino.y) {
                    std::cout << "D";
                } else {
                    bool isObstacle = false;
                    for (const auto& obstacle : obstacles) {
                        if (obstacle.x == x && obstacle.y == y) {
                            std::cout << "#";
                            isObstacle = true;
                            break;
                        }
                    }
                    if (!isObstacle) {
                        std::cout << ".";
                    }
                }
            }
            std::cout << "\n";
        }

        std::cout << "得分: " << score << "\n";
        std::cout << "难度: 1/" << speed << " \n";
    }

    void run() {
        while (!gameOver) {
            processInput();
            update();
            render();
            std::this_thread::sleep_for(std::chrono::milliseconds(speed));
        }
        std::cout << "游戏结束!你的得分: " << score << "\n";
    }
};

int main() {
    Game game;
    std::cout << "谷歌浏览器小恐龙(C++版),作者:Yuka(https://www.luogu.com.cn/user/1414372)" << std::endl << "按任意键开始游戏。"<< std::endl;;
    system("pause>nul");
    game.run();
    return 0;
}