c++小游戏——谷歌小恐龙

这是一个使用C++编写的简单游戏程序,包含游戏画面绘制、障碍物生成、恐龙跳跃、游戏难度随得分增加而提高等功能。玩家可以通过键盘控制恐龙跳跃避开障碍物,碰到障碍物则游戏结束。程序使用了标准库如iostream、conio.h和chrono进行控制和时间处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

请用Dev-cpp6.5或专业C++程序,否则不可运行。

#include <iostream>
#include <conio.h>
#include <ctime>
#include <chrono>
#include <thread>
#include <Windows.h>

using namespace std;

// 游戏画面宽度和高度
const int WIDTH = 80;
const int HEIGHT = 20;

// 障碍物速度和间隔时间
int obstacle_speed = 10; // 单位是毫秒
int obstacle_interval = 50; // 单位是毫秒

// 初始跳跃高度和最大跳跃高度
int jump_height = 3;
int max_jump_height = 6;

// 游戏是否结束
bool game_over = false;

// 当前得分
int score = 0;

// 跳跃状态
bool jumping = false;
int jump_time = 0;

// 恐龙位置
int dino_pos = HEIGHT - 2;

// 障碍物位置和类型
int obstacle_pos = -1;
char obstacle_type;

// 随机生成障碍物类型
char random_obstacle_type() {
    char obstacle_types[] = { '#', '=', '*', '^' };
    return obstacle_types[rand() % 4];
}

// 根据得分调整游戏难度
void adjust_game_difficulty() {
    if (score >= 100 && score < 300) {
        obstacle_speed = 8;
        obstacle_interval = 40;
    }
    else if (score >= 300 && score < 500) {
        obstacle_speed = 6;
        obstacle_interval = 35;
    }
    else if (score >= 500 && score < 1000) {
        obstacle_speed = 4;
        obstacle_interval = 30;
    }
    else if (score >= 1000) {
        obstacle_speed = 2;
        obstacle_interval = 25;
    }
}

// 绘制游戏画面
void draw()
{
    system("cls");
    cout << "Score: " << score << endl;
    cout << "+";
    for (int i = 0; i < WIDTH - 2; i++) {
        cout << "-";
    }
    cout << "+" << endl;
    for (int i = 0; i < HEIGHT - 1; i++) {
        cout << "|";
        if (i == dino_pos && !jumping) {
            cout << "@";
            for (int j = 0; j < WIDTH - 2; j++) {
                cout << " ";
            }
        }
        else if (i == dino_pos - jump_height && jumping) {
            cout << "/";
            for (int j = 1; j < WIDTH - 3; j++) {
                cout << " ";
            }
            cout << "\\";
        }
        else if (obstacle_pos != -1 && i == HEIGHT - 2) {
            if (obstacle_pos == 0) {
                cout << obstacle_type;
                for (int j = 1; j < WIDTH - 2; j++) {
                    cout << " ";
                }
            }
            else {
                for (int j = 0; j < obstacle_pos - 1; j++) {
                    cout << " ";
                }
                cout << obstacle_type;
                for (int j = obstacle_pos; j < WIDTH - 2; j++) {
                    cout << " ";
                }
            }
        }
        else {
            for (int j = 0; j < WIDTH - 2; j++) {
                cout << " ";
            }
        }
        cout << "|" << endl;
    }
    cout << "+";
    for (int i = 0; i < WIDTH - 2; i++) {
        cout << "-";
    }
    cout << "+" << endl;
}

// 控制恐龙跳跃
void jump()
{
    if (!jumping) {
        jumping = true;
        jump_time = 0;
    }
}

// 更新游戏状态
void update_game_state() {
    // 恐龙跳跃时位置变化
    if (jumping) {
        jump_time++;
        if (jump_time <= max_jump_height) {
            dino_pos--;
        }
        else if (jump_time <= max_jump_height * 2) {
            dino_pos++;
        }
        else {
            jumping = false;
            jump_time = 0;
        }
    }
    // 障碍物移动并重新生成
    obstacle_pos -= obstacle_speed;
    if (obstacle_pos < -1) {
        score += 10;
        obstacle_type = random_obstacle_type();
        obstacle_pos = WIDTH - 2;
        adjust_game_difficulty();
        // 生成随机事件
        int event_type = rand() % 10;
        if (event_type == 0) { // 减速障碍物
            obstacle_speed = 5;
            obstacle_interval = 75;
            draw();
            Sleep(500);
        }
        else if (event_type == 1) { // 加速障碍物
            obstacle_speed = 15;
            obstacle_interval = 25;
            draw();
            Sleep(500);
        }
        else {
            obstacle_speed = 10;
            obstacle_interval = 50;
        }
    }
    // 检测是否碰到障碍物
    if (obstacle_pos >= 0 && obstacle_pos < WIDTH - 2 && obstacle_pos == dino_pos && obstacle_type != '^') {
        game_over = true;
    }
}

int main()
{
    srand((unsigned)time(nullptr));
    while (!game_over) {
        if (_kbhit()) {
            char c = _getch();
            if (c == ' ') {
                jump();
            }
        }
        update_game_state();
        draw();
        std::this_thread::sleep_for(std::chrono::milliseconds(obstacle_interval));
    }
    dino_pos = HEIGHT / 2;
    draw();
    cout << endl << "Game over! Final score: " << score << endl;
    return 0;
}
 

以下是谷歌小恐龙的简单实现代码(使用C++和SDL库): ```c++ #include <iostream> #include <SDL.h> using namespace std; const int SCREEN_WIDTH = 600; const int SCREEN_HEIGHT = 200; const int GROUND_HEIGHT = 180; int main(int argc, char* args[]) { SDL_Init(SDL_INIT_VIDEO); SDL_Window* window = SDL_CreateWindow("Google Dino", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); SDL_Texture* dinoTexture = SDL_CreateTextureFromSurface(renderer, SDL_LoadBMP("dino.bmp")); SDL_Texture* cactusTexture = SDL_CreateTextureFromSurface(renderer, SDL_LoadBMP("cactus.bmp")); SDL_Texture* groundTexture = SDL_CreateTextureFromSurface(renderer, SDL_LoadBMP("ground.bmp")); bool quit = false; SDL_Event e; int dinoX = 0; int dinoY = GROUND_HEIGHT; int cactusX = SCREEN_WIDTH; int cactusY = GROUND_HEIGHT - 20; int groundX = 0; int groundY = GROUND_HEIGHT + 20; int dinoVelY = 0; while (!quit) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_QUIT) { quit = true; } else if (e.type == SDL_KEYDOWN) { if (e.key.keysym.sym == SDLK_SPACE) { if (dinoY == GROUND_HEIGHT) { dinoVelY = -10; } } } } dinoY += dinoVelY; if (dinoY > GROUND_HEIGHT) { dinoY = GROUND_HEIGHT; dinoVelY = 0; } else if (dinoY < 0) { dinoY = 0; dinoVelY = 0; } cactusX -= 5; if (cactusX < -20) { cactusX = SCREEN_WIDTH; } groundX -= 5; if (groundX < -SCREEN_WIDTH) { groundX = 0; } SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_RenderClear(renderer); SDL_Rect dinoRect = { dinoX, dinoY, 48, 48 }; SDL_RenderCopy(renderer, dinoTexture, NULL, &dinoRect); SDL_Rect cactusRect = { cactusX, cactusY, 20, 40 }; SDL_RenderCopy(renderer, cactusTexture, NULL, &cactusRect); SDL_Rect groundRect1 = { groundX, groundY, SCREEN_WIDTH, 20 }; SDL_RenderCopy(renderer, groundTexture, NULL, &groundRect1); SDL_Rect groundRect2 = { groundX + SCREEN_WIDTH, groundY, SCREEN_WIDTH, 20 }; SDL_RenderCopy(renderer, groundTexture, NULL, &groundRect2); SDL_RenderPresent(renderer); SDL_Delay(17); } SDL_DestroyTexture(dinoTexture); SDL_DestroyTexture(cactusTexture); SDL_DestroyTexture(groundTexture); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; } ``` 这只是一个简单的实现,你可以根据需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值