跳到正文

休闲·娱乐

dino,谷歌小恐龙

Area_qu约 22 分钟
原文
文章信息与版本

文章信息

发布
2025年12月7日
收录
2026年1月10日
更新
2026年11月9日
篇幅
约 22 分钟

版本

  1. v2当前
  2. v1
C++
#include <windows.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <math.h>

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 300
#define GROUND_HEIGHT 250
#define DINO_WIDTH 40
#define DINO_HEIGHT 60
#define CACTUS_WIDTH 20
#define CACTUS_HEIGHT 40
#define BIRD_WIDTH 30
#define BIRD_HEIGHT 20

typedef enum {GAME_MENU, GAME_PLAYING, GAME_OVER} GameState;

HWND hwnd;
HDC hdc, hdcBuffer;
HBITMAP hbmBuffer;
PAINTSTRUCT ps;
GameState gameState = GAME_MENU;

int dinoX = 100;
int dinoY = GROUND_HEIGHT - DINO_HEIGHT;
int dinoVelocity = 0;
const int gravity = 1;
const int jumpForce = -15;
int dinoFrame = 0;
bool isDucking = false;

typedef struct {
    int x, y, width, height, type;
    bool active;
} Obstacle;

Obstacle obstacles[3];
int obstacleSpeed = 5;
int obstacleSpawnTimer = 0;
int obstacleSpawnInterval = 120; // Removed 'const' here

int score = 0;
int highScore = 0;
int gameTime = 0;
bool isDay = true;
char scoreText[50];
clock_t lastTime;

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void UpdateGame();
void DrawGame(HDC hdc);
void ResetGame();
void SpawnObstacle();
void DrawDino(HDC hdc, int x, int y, int frame, bool ducking);
void DrawObstacle(HDC hdc, Obstacle* obs);
void DrawClouds(HDC hdc);
void DrawGround(HDC hdc);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    WNDCLASS wc = {0};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = "DinoGame";
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    RegisterClass(&wc);

    hwnd = CreateWindowEx(0, "DinoGame", "谷歌小恐龙游戏", WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,
                         CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);
    if (!hwnd) return 0;

    ShowWindow(hwnd, nCmdShow);
    hdc = GetDC(hwnd);
    hdcBuffer = CreateCompatibleDC(hdc);
    hbmBuffer = CreateCompatibleBitmap(hdc, WINDOW_WIDTH, WINDOW_HEIGHT);
    SelectObject(hdcBuffer, hbmBuffer);
    srand((unsigned)time(NULL));

    for (int i = 0; i < 3; i++) obstacles[i].active = false;
    lastTime = clock();

    MSG msg = {0};
    while (true) {
        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
            if (msg.message == WM_QUIT) {
                DeleteObject(hbmBuffer);
                DeleteDC(hdcBuffer);
                ReleaseDC(hwnd, hdc);
                return (int)msg.wParam;
            }
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        if (gameState == GAME_PLAYING) UpdateGame();
        InvalidateRect(hwnd, NULL, FALSE);
        UpdateWindow(hwnd);
        while ((clock() - lastTime) < (1000 / 60)) {}
        lastTime = clock();
    }
    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            DrawGame(hdcBuffer);
            BitBlt(hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, hdcBuffer, 0, 0, SRCCOPY);
            EndPaint(hwnd, &ps);
            return 0;
        case WM_KEYDOWN:
            if (gameState == GAME_PLAYING) {
                if (wParam == VK_SPACE && dinoY >= GROUND_HEIGHT - DINO_HEIGHT) dinoVelocity = jumpForce;
                else if (wParam == VK_DOWN) isDucking = true;
            }
            else if (wParam == VK_RETURN) {
                if (gameState == GAME_MENU || gameState == GAME_OVER) {
                    gameState = GAME_PLAYING;
                    ResetGame();
                }
            }
            return 0;
        case WM_KEYUP:
            if (wParam == VK_DOWN) isDucking = false;
            return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

void UpdateGame() {
    gameTime++;
    if (gameTime % 1000 == 0) isDay = !isDay;

    dinoY += dinoVelocity;
    dinoVelocity += gravity;
    if (dinoY > GROUND_HEIGHT - (isDucking ? DINO_HEIGHT/2 : DINO_HEIGHT)) {
        dinoY = GROUND_HEIGHT - (isDucking ? DINO_HEIGHT/2 : DINO_HEIGHT);
        dinoVelocity = 0;
    }

    dinoFrame = (dinoY < GROUND_HEIGHT - (isDucking ? DINO_HEIGHT/2 : DINO_HEIGHT)) ? 0 : (gameTime / 10) % 2;

    obstacleSpawnTimer++;
    if (obstacleSpawnTimer >= obstacleSpawnInterval) {
        obstacleSpawnTimer = 0;
        SpawnObstacle();
    }

    for (int i = 0; i < 3; i++) {
        if (obstacles[i].active) {
            obstacles[i].x -= obstacleSpeed;
            if (obstacles[i].x + obstacles[i].width < 0) obstacles[i].active = false;

            if (obstacles[i].x < dinoX + DINO_WIDTH && obstacles[i].x + obstacles[i].width > dinoX) {
                if (obstacles[i].type == 0) {
                    if (dinoY + (isDucking ? DINO_HEIGHT/2 : DINO_HEIGHT) > obstacles[i].y) {
                        gameState = GAME_OVER;
                        if (score > highScore) highScore = score;
                    }
                } else {
                    if (!isDucking && dinoY + DINO_HEIGHT > obstacles[i].y && dinoY < obstacles[i].y + obstacles[i].height) {
                        gameState = GAME_OVER;
                        if (score > highScore) highScore = score;
                    }
                }
            }
        }
    }

    score = gameTime / 10;
    obstacleSpeed = 5 + score / 100;
    int newInterval = 120 - (score / 10);
    obstacleSpawnInterval = newInterval < 60 ? 60 : newInterval; // This line no longer causes an error
}

void DrawGame(HDC hdc) {
    RECT bgRect = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
    HBRUSH bgBrush = CreateSolidBrush(isDay ? RGB(247, 247, 247) : RGB(50, 50, 50));
    FillRect(hdc, &bgRect, bgBrush);
    DeleteObject(bgBrush);

    DrawClouds(hdc);
    DrawGround(hdc);
    DrawDino(hdc, dinoX, dinoY, dinoFrame, isDucking);

    for (int i = 0; i < 3; i++)
        if (obstacles[i].active) DrawObstacle(hdc, &obstacles[i]);

    SetBkMode(hdc, TRANSPARENT);
    SetTextColor(hdc, isDay ? RGB(0, 0, 0) : RGB(255, 255, 255));
    sprintf(scoreText, "分数: %d", score);
    TextOut(hdc, WINDOW_WIDTH - 150, 10, scoreText, strlen(scoreText));
    sprintf(scoreText, "最高分: %d", highScore);
    TextOut(hdc, WINDOW_WIDTH - 150, 30, scoreText, strlen(scoreText));

    if (gameState == GAME_MENU) {
        TextOut(hdc, WINDOW_WIDTH/2 - 100, WINDOW_HEIGHT/2 - 30, "谷歌小恐龙游戏", 14);
        TextOut(hdc, WINDOW_WIDTH/2 - 100, WINDOW_HEIGHT/2, "按Enter键开始游戏", 18);
        TextOut(hdc, WINDOW_WIDTH/2 - 150, WINDOW_HEIGHT/2 + 30, "空格键跳跃, 下键蹲下", 22);
    }
    else if (gameState == GAME_OVER) {
        TextOut(hdc, WINDOW_WIDTH/2 - 100, WINDOW_HEIGHT/2 - 10, "游戏结束!", 10);
        TextOut(hdc, WINDOW_WIDTH/2 - 100, WINDOW_HEIGHT/2 + 20, "按Enter键重新开始", 18);
    }
}

void ResetGame() {
    dinoY = GROUND_HEIGHT - DINO_HEIGHT;
    dinoVelocity = 0;
    isDucking = false;
    for (int i = 0; i < 3; i++) obstacles[i].active = false;
    obstacleSpawnTimer = 0;
    obstacleSpeed = 5;
    gameTime = 0;
    score = 0;
}

void SpawnObstacle() {
    int freeSlot = -1;
    for (int i = 0; i < 3; i++) if (!obstacles[i].active) { freeSlot = i; break; }
    if (freeSlot == -1) return;

    if (rand() % 100 < 30 && score > 20) {
        obstacles[freeSlot].type = 1;
        obstacles[freeSlot].width = BIRD_WIDTH;
        obstacles[freeSlot].height = BIRD_HEIGHT;
        obstacles[freeSlot].y = GROUND_HEIGHT - (rand() % 2 == 0 ? 70 : 100);
    } else {
        obstacles[freeSlot].type = 0;
        obstacles[freeSlot].width = CACTUS_WIDTH;
        obstacles[freeSlot].height = CACTUS_HEIGHT;
        obstacles[freeSlot].y = GROUND_HEIGHT - CACTUS_HEIGHT;
    }
    obstacles[freeSlot].x = WINDOW_WIDTH;
    obstacles[freeSlot].active = true;
}

void DrawDino(HDC hdc, int x, int y, int frame, bool ducking) {
    HBRUSH dinoBrush = CreateSolidBrush(isDay ? RGB(50, 50, 50) : RGB(255, 255, 255)); // 白天灰色,黑夜白色
    HPEN dinoPen = CreatePen(PS_SOLID, 1, isDay ? RGB(50, 50, 50) : RGB(255, 255, 255)); // 白天灰色,黑夜白色
    HGDIOBJ oldBrush = SelectObject(hdc, dinoBrush);
    HGDIOBJ oldPen = SelectObject(hdc, dinoPen);

    if (ducking) {
        RoundRect(hdc, x, y + DINO_HEIGHT / 2, x + DINO_WIDTH, y + DINO_HEIGHT, 10, 10);
        Ellipse(hdc, x + 10, y + DINO_HEIGHT / 2 - 15, x + 30, y + DINO_HEIGHT / 2 + 5);
    } else {
        RoundRect(hdc, x, y, x + DINO_WIDTH, y + DINO_HEIGHT, 10, 10);
        if (frame == 0) {
            MoveToEx(hdc, x + 10, y + DINO_HEIGHT, NULL);
            LineTo(hdc, x + 15, y + DINO_HEIGHT + 5);
            MoveToEx(hdc, x + 30, y + DINO_HEIGHT, NULL);
            LineTo(hdc, x + 25, y + DINO_HEIGHT + 10);
        } else {
            MoveToEx(hdc, x + 10, y + DINO_HEIGHT, NULL);
            LineTo(hdc, x + 15, y + DINO_HEIGHT + 10);
            MoveToEx(hdc, x + 30, y + DINO_HEIGHT, NULL);
            LineTo(hdc, x + 25, y + DINO_HEIGHT + 5);
        }
        Ellipse(hdc, x + 10, y - 15, x + 30, y + 5);
    }

    SelectObject(hdc, oldBrush);
    SelectObject(hdc, oldPen);
    DeleteObject(dinoBrush);
    DeleteObject(dinoPen);
}
void DrawObstacle(HDC hdc, Obstacle* obs) {
    HBRUSH obsBrush = CreateSolidBrush(RGB(50, 50, 50));
    HGDIOBJ oldBrush = SelectObject(hdc, obsBrush);

    if (obs->type == 0) {
        Rectangle(hdc, obs->x, obs->y, obs->x + obs->width, obs->y + obs->height);
        for (int i = 0; i < 3; i++) {
            MoveToEx(hdc, obs->x + obs->width/2, obs->y + i*10, NULL);
            LineTo(hdc, obs->x + obs->width/2 + 10, obs->y + i*10 + 5);
        }
    } else {
        POINT birdWing[3];
        if ((gameTime / 5) % 2 == 0) {
            birdWing[0].x = obs->x + 5; birdWing[0].y = obs->y + 5;
            birdWing[1].x = obs->x + 15; birdWing[1].y = obs->y + 15;
            birdWing[2].x = obs->x + 5; birdWing[2].y = obs->y + 15;
        } else {
            birdWing[0].x = obs->x + 5; birdWing[0].y = obs->y + 15;
            birdWing[1].x = obs->x + 15; birdWing[1].y = obs->y + 5;
            birdWing[2].x = obs->x + 15; birdWing[2].y = obs->y + 15;
        }
        Polygon(hdc, birdWing, 3);
    }

    SelectObject(hdc, oldBrush);
    DeleteObject(obsBrush);
}

void DrawClouds(HDC hdc) {
    HBRUSH cloudBrush = CreateSolidBrush(isDay ? RGB(220, 220, 220) : RGB(100, 100, 100));
    HGDIOBJ oldBrush = SelectObject(hdc, cloudBrush);

    for (int i = 0; i < 3; i++) {
        int cloudX = (100 + i * 200 - gameTime / 10) % (WINDOW_WIDTH + 200);
        int cloudY = 30 + i * 40;
        Ellipse(hdc, cloudX, cloudY, cloudX + 60, cloudY + 20);
        Ellipse(hdc, cloudX + 10, cloudY - 10, cloudX + 70, cloudY + 20);
        Ellipse(hdc, cloudX + 30, cloudY - 5, cloudX + 90, cloudY + 15);
    }

    SelectObject(hdc, oldBrush);
    DeleteObject(cloudBrush);
}

void DrawGround(HDC hdc) {
    HPEN groundPen = CreatePen(PS_SOLID, 1, RGB(50, 50, 50));
    HGDIOBJ oldPen = SelectObject(hdc, groundPen);

    MoveToEx(hdc, 0, GROUND_HEIGHT, NULL);
    LineTo(hdc, WINDOW_WIDTH, GROUND_HEIGHT);

    SelectObject(hdc, oldPen);
    DeleteObject(groundPen);
}
原文