版本比较
dino,谷歌小恐龙
正文
增加 42 行删除 42 行
2 行未改动3456778910111213141516171718191920212223242525262728293031323333343536373838394041424343444546474849505051525354555657585960606162636465666768686970717272737475767778797980818282838485864 行未改动9192939495959697989910010110210310410410510610710821 行未改动1301311321331341341351361371381381391401411421431441451451461471471481491501511521531531541551561571581581591601611627 行未改动1701711721731741741751761771781791801801811821831841851861861871881891901911901911921931931941951961971981992002002012022032042 行未改动2072082092102112112122132142152 行未改动2182192202212222222232242252262272272282292302316 行未改动23823924024124224224324424524624724824824925025125210 行未改动26326426526626726726826927027127227327427527627627727827928011 行未改动2922932942952962962972982993003003013023033043043053063073083093103113123123133143153163163173183193203203213223233233243253263272 行未改动#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) {4 行未改动 } 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);21 行未改动 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;7 行未改动 } } } }
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++)
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);2 行未改动 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;2 行未改动 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;6 行未改动 } 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 {10 行未改动 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);11 行未改动 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);}```