-
Bio
#include #include <windows.h> #include #include
using namespace std;
const int WIDTH = 50; const int HEIGHT = 10;
// 打印游戏界面 void printGameBoard(bool characterJump, int obstaclePos) { for (int i = 0; i < HEIGHT; ++i) { for (int j = 0; j < WIDTH; ++j) { if (i == HEIGHT - 1 && j == WIDTH - 1) { if (characterJump) { cout << " o "; } else { cout << " o "; } } else if (i == HEIGHT - 1 && j == obstaclePos) { cout << " X "; } else { cout << " "; } } cout << endl; } }
// 游戏主逻辑 void runGame() { bool characterJump = false; int obstaclePos = WIDTH - 2; int score = 0; int jumpTimer = 0; int jumpDuration = 3;
srand(static_cast<unsigned int>(time(nullptr))); while (true) { if (GetAsyncKeyState(VK_SPACE) & 0x8000 &&!characterJump) { characterJump = true; jumpTimer = 0; } if (characterJump) { jumpTimer++; if (jumpTimer >= jumpDuration) { characterJump = false; } } system("cls"); printGameBoard(characterJump, obstaclePos); if (obstaclePos == WIDTH - 1) { if (!characterJump) { cout << "游戏结束!你的得分是: " << score << endl; break; } score++; obstaclePos = rand() % (WIDTH - 2); } else { obstaclePos++; } Sleep(100); }
}
int main() { cout << "跑酷小游戏开始!按空格键跳跃躲避障碍物。" << endl; runGame(); return 0; }
-
Accepted Problems
-
Recent Activities
This person is lazy and didn't join any contests or homework.