跳到正文

个人记录

谷歌小恐龙

wooood约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月26日
篇幅
约 11 分钟

版本

  1. v1当前
C++
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cmath>

using namespace std;

// 游戏常量
const int WIDTH = 80;
const int HEIGHT = 20;
const int GROUND = 18;
const int DINO_START_X = 5;
const int CACTUS_SPEED = 1;
const float GRAVITY = 3.0f;
const float JUMP_FORCE = -4.5f;
const int ANIMATION_SPEED = 10;
const float LANDING_SPEED = 0.6f;
const int MIN_CACTUS_GAP = 10;

// 游戏状态
bool gameOver = false;
int score = 0;
int highScore = 0;

// 恐龙状态
float dinoY = GROUND;
float dinoVelocity = 0;
bool isJumping = false;
bool isLanding = false;
int lastDinoY = GROUND;
float jumpProgress = 0.0f;
char dinoChar = '@';

// 障碍物
vector<int> cacti;

void gotoxy(int x, int y) {
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void hideCursor() {
    CONSOLE_CURSOR_INFO cursorInfo;
    cursorInfo.dwSize = 100;
    cursorInfo.bVisible = FALSE;
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
}

void clearDino() {
    for (int y = GROUND-4; y <= GROUND; y++) {
        gotoxy(DINO_START_X, y);
        cout << " ";
    }
    gotoxy(DINO_START_X, GROUND);
    cout << " ";
}

void updateDinoAnimation() {
    if (isJumping) {
        float maxHeight = GROUND - (JUMP_FORCE*JUMP_FORCE)/(2*GRAVITY);
        jumpProgress = (GROUND - dinoY) / (GROUND - maxHeight);

        if (jumpProgress < 0.3f) {
            dinoChar = '^';
        }
        else if (jumpProgress > 0.7f) {
            dinoChar = 'v';
            if (jumpProgress > 0.9f) dinoChar = 'V';
        }
        else {
            dinoChar = '@';
        }
    } else {
        dinoChar = '@';
        jumpProgress = 0.0f;
    }
}

void drawDino() {
    int currentY = static_cast<int>(round(dinoY));
    currentY = max(GROUND-4, min(GROUND, currentY));

    updateDinoAnimation();

    if (lastDinoY != currentY) {
        clearDino();
    }

    gotoxy(DINO_START_X, currentY);
    cout << dinoChar;

    if (currentY < GROUND) {
        gotoxy(DINO_START_X, GROUND);
        char shadowChar = (currentY > GROUND-2) ? ' ' : ' ';
        cout << shadowChar;
    }

    lastDinoY = currentY;
}

void draw() {
    system("cls");

    gotoxy(WIDTH - 20, 2);
    cout << "Score: " << score;
    gotoxy(WIDTH - 20, 3);
    cout << "High Score: " << highScore;

    for (int i = 0; i < WIDTH; i++) {
        gotoxy(i, GROUND + 1);
        cout << "_";
    }

    drawDino();

    // 只绘制屏幕内的障碍物
    for (int cactus : cacti) {
        if (cactus - 2 >= 0 && cactus - 2 < WIDTH) {  // 只绘制在屏幕内的障碍物
            gotoxy(cactus - 2, GROUND);
            cout << "###";
        }
    }
}

bool canSpawnCactus() {
    if (cacti.empty()) return true;

    int lastCactus = cacti.back();
    return (WIDTH - 5 - lastCactus) >= MIN_CACTUS_GAP;
}

void update() {
    if (isJumping) {
        if (!isLanding) {
            dinoY += dinoVelocity;
            dinoVelocity += GRAVITY;

            if (dinoVelocity > 0 && !isLanding) {
                isLanding = true;
            }
        } else {
            if (dinoY < GROUND - 1) {
                dinoY += LANDING_SPEED;
            } else {
                dinoY = GROUND;
                isJumping = false;
                isLanding = false;
                dinoVelocity = 0;
            }
        }
    }

    if (rand() % 20 == 0 && canSpawnCactus()) {
        cacti.push_back(WIDTH - 5);
    }

    for (size_t i = 0; i < cacti.size(); i++) {
        cacti[i] -= CACTUS_SPEED;

        if (cacti[i] >= DINO_START_X && cacti[i] <= DINO_START_X + 2) {
            int dinoPosY = static_cast<int>(round(dinoY));
            if (dinoPosY >= GROUND) {
                gameOver = true;
            }
        }

        if (cacti[i] < -3) {  // 完全移出屏幕后再移除
            cacti.erase(cacti.begin() + i);
            i--;
            score++;
        }
    }
}

void input() {
    if (_kbhit()) {
        char key = _getch();
        if ((key == ' ' || key == 'w' || key == 'W') && !isJumping && dinoY >= GROUND) {
            isJumping = true;
            isLanding = false;
            dinoVelocity = JUMP_FORCE;
        }
    }
}

void init() {
    srand(time(NULL));
    hideCursor();
    cacti.clear();
    dinoY = GROUND;
    lastDinoY = GROUND;
    isJumping = false;
    isLanding = false;
    score = 0;
    gameOver = false;
    dinoChar = '@';
    jumpProgress = 0.0f;
}

void gameLoop() {
    init();

    while (!gameOver) {
        draw();
        input();
        update();
        Sleep(ANIMATION_SPEED);
    }

    if (score > highScore) {
        highScore = score;
    }

    gotoxy(WIDTH / 2 - 5, HEIGHT / 2);
    cout << "GAME OVER";
    gotoxy(WIDTH / 2 - 5, HEIGHT / 2 + 1);
    cout << "Press R to restart";

    while (true) {
        if (_kbhit() && (_getch() == 'r' || _getch() == 'R')) {
            gameLoop();
            break;
        }
    }
}

int main() {
    system("mode con cols=80 lines=25");

    cout << "Dino Game with Fixed Cactus Display" << endl;
    cout << "Controls:" << endl;
    cout << "Space/W - Jump" << endl;
    cout << "R - Restart after game over" << endl;
    cout << "\nPress any key to start...";
    _getch();

    gameLoop();

    return 0;
}
原文