赛车游戏(会更新)【1】
2026-08-12 18:49:58
发布于:广东
耗时:1年(改了3稿,在附件)
代码意思自己看注释
给个点赞~
#include <bit/stdc++.h>
using namespace std;
// 常量定义
const int MAP_HEIGHT = 15;
const int MAP_WIDTH = 75;
const int MAX_SPEED = 5;
const int MIN_SPEED = 1;
const int TRACK_LEFT = 2;
const int TRACK_RIGHT = MAP_HEIGHT - 3;
const int CAR_INIT_X = 0; // 赛车初始X位置
const int CAR_MAX_X = 70; // 赛车最右侧边界
const int RESET_OFFSET = 70; // 每次重置终点线的递减值
// 全局变量
int game_hard = 1;
int carY = MAP_HEIGHT / 2;
int carX = CAR_INIT_X;
int speed = 1;
bool isFinish = false;
int baseEndLineX; // 基础终点线坐标(按公式计算)
int currentEndLineX; // 当前实际终点线坐标(含递减偏移)
int resetCount = 0; // 赛道重置次数
vector<vector<int> > map(MAP_HEIGHT, vector<int>(MAP_WIDTH, 0));
// 高效清屏
void clearScreen() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {0, 0};
DWORD written;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);
DWORD size = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, ' ', size, coord, &written);
SetConsoleCursorPosition(hConsole, coord);
}
// 光标绘制
void drawChar(int x, int y, char c) {
COORD coord = {(SHORT)x, (SHORT)y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
cout << c;
}
// 计算基础终点线坐标(原始公式)
int calculateBaseEndLineX() {
return CAR_INIT_X + MAP_HEIGHT * game_hard * 2;
}
// 计算当前实际终点线坐标(基础值 - 重置次数×70)
int calculateCurrentEndLineX() {
int endX = baseEndLineX - resetCount * RESET_OFFSET;
// 确保终点线在有效范围内(最小X=10,最大X=CAR_MAX_X-5)
return max(min(endX, CAR_MAX_X - 5), 10);
}
// 初始化/重绘地图(支持终点线递减)
void initMap() {
// 1. 清空地图
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
map[y][x] = 0;
}
}
// 2. 绘制跑道边框(/ 和 -)
for (int x = 0; x < MAP_WIDTH; x++) {
map[TRACK_LEFT][x] = 1;
map[TRACK_RIGHT][x] = 1;
if (x % 3 == 0) map[MAP_HEIGHT-2][x] = 1;
}
// 3. 计算并绘制当前终点线
currentEndLineX = calculateCurrentEndLineX();
map[MAP_HEIGHT/2][currentEndLineX] = 3;
// 4. 生成障碍物(仅在终点线左侧生成)
srand((unsigned int)time(NULL));
int obstacleCount = 5 + game_hard;
for (int i = 0; i < obstacleCount; i++) {
int obsX = rand() % (currentEndLineX - 5) + 5;
int obsY = rand() % (TRACK_RIGHT - TRACK_LEFT - 1) + TRACK_LEFT + 1;
if (!(obsX == carX && obsY == carY)) {
map[obsY][obsX] = 2;
}
}
}
// 绘制完整地图
void drawMap() {
clearScreen();
// 第一步:绘制所有固定物体
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
if (map[y][x] == 1) {
drawChar(x, y, (y == MAP_HEIGHT-2) ? '-' : '/');
}
else if (map[y][x] == 2) {
drawChar(x, y, '#');
}
else if (map[y][x] == 3) {
drawChar(x, y, '|');
}
}
}
// 第二步:绘制赛车
drawChar(carX, carY, '*');
// 绘制游戏信息(显示终点线递减逻辑)
COORD infoCoord = {0, TRACK_RIGHT + 1};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), infoCoord);
cout << "========== 循环竞速赛车(终点线递减) ==========\n"
<< "当前速度:" << speed << " (最大速度:" << MAX_SPEED << ")\n"
<< "控制键:W(上) S(下) A(减速) D(加速) Q(退出)\n"
<< "基础终点线:" << baseEndLineX << " | 重置次数:" << resetCount << " | 递减偏移:" << resetCount * RESET_OFFSET << "\n"
<< "当前终点线:X=" << currentEndLineX << " | 赛车位置:X=" << carX << ", Y=" << carY << "\n"
<< "赛道边框(//-) 赛车(*) 障碍物(#) 终点线:|" << endl;
}
// 赛车移动逻辑(核心:修复to_string错误 + 终点线递减+循环赛道)
void moveCar() {
// 1. 检测是否到达最右侧(X=70)→ 循环重置
if (carX >= CAR_MAX_X) {
resetCount++; // 重置次数+1
carX = CAR_INIT_X; // 赛车移回最左侧(X=0)
initMap(); // 重绘地图(使用新的终点线)
// 修复:用sprintf替代to_string拼接弹窗信息
char resetMsg[256];
sprintf(resetMsg,
"已到达赛道终点!\n重置次数:%d\n新终点线:%d - %d×70 = %d",
resetCount, baseEndLineX, resetCount, currentEndLineX);
MessageBox(NULL, resetMsg, "循环赛道", MB_OK);
return; // 重置后跳过本次移动
}
// 2. 赛车自动向右前进
int newCarX = carX + speed;
if (newCarX <= CAR_MAX_X) {
carX = newCarX;
}
// 3. 检测终点:到达当前终点线即胜利
if (carX >= currentEndLineX) {
isFinish = true;
// 修复:用sprintf替代to_string拼接弹窗信息
char winMsg[256];
sprintf(winMsg,
"恭喜!到达终点!\n最终终点线:X=%d\n总重置次数:%d",
currentEndLineX, resetCount);
MessageBox(NULL, winMsg, "游戏胜利", MB_OK);
exit(0);
}
// 4. 检测碰撞
if (map[carY][carX] == 2) {
speed = 0;
MessageBox(NULL, "撞到障碍物了!速度清零!(按D加速继续)", "提示", MB_OK);
if (carX - 1 >= CAR_INIT_X) {
carX--;
}
}
}
// 手动控制逻辑
void controlCar() {
if (!_kbhit() || isFinish) return;
char key = _getch();
switch (key) {
case 'w': case 'W':
if (carY - 1 > TRACK_LEFT && map[carY - 1][carX] != 2 && map[carY - 1][carX] != 1) {
carY--;
}
break;
case 's': case 'S':
if (carY + 1 < TRACK_RIGHT && map[carY + 1][carX] != 2 && map[carY + 1][carX] != 1) {
carY++;
}
break;
case 'a': case 'A':
speed = max(speed - 1, MIN_SPEED);
break;
case 'd': case 'D':
speed = min(speed + 1, MAX_SPEED);
break;
case 'q': case 'Q':
exit(0);
break;
}
}
int main() {
// 关闭控制台快速编辑模式
HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hInput, &mode);
mode &= ~ENABLE_QUICK_EDIT_MODE;
SetConsoleMode(hInput, mode);
// 输入难度
while (true) {
cout << "输入游戏难度(1~15):";
if (cin >> game_hard && game_hard >= 1 && game_hard <= 15) {
break;
}
cin.clear();
cin.ignore(1000, '\n');
cout << "请输入1~15之间的有效数字!" << endl;
}
// 初始化基础终点线坐标(仅计算一次)
baseEndLineX = calculateBaseEndLineX();
// 初始化当前终点线坐标
currentEndLineX = calculateCurrentEndLineX();
// 初始化地图
initMap();
// 游戏主循环
while (true) {
drawMap();
controlCar();
moveCar();
Sleep(16);
}
return 0;
}
点完赞在拿代码~~~
再给一个关注吧~~~
附件:
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <windows.h>
#include <ctime>
#include <vector>
int game_hard;
const int MAP_HEIGHT = 15;
const int MAP_WIDTH = 75;
const int MAX_SPEED = 5;
const int MIN_SPEED = 1;
int carY = MAP_HEIGHT / 2;
int carX = 5;
int speed = 1;
bool isFinish = false;
std::vector<std::vector<int> > map(MAP_HEIGHT, std::vector<int>(MAP_WIDTH, 0));
void initMap() {
for (int x = 0; x < MAP_WIDTH; x++) {
map[0][x] = 1;
map[MAP_HEIGHT - 1][x] = 1;
}
for (int y = 0; y < MAP_HEIGHT; y++) {
map[y][0] = 1;
map[y][MAP_WIDTH - 1] = 1;
}
srand((unsigned int)time(NULL));
for (int i = 0; i < 20; i++) {
int obsX = rand() % (MAP_WIDTH - 2) + 1;
int obsY = rand() % (MAP_HEIGHT - 2) + 1;
if (!(obsX == carX && obsY == carY)) {
map[obsY][obsX] = 2;
}
}
}
void scrollMap() {
if (speed <= 0 || isFinish) return;
for (int s = 0; s < speed; s++) {
for (int y = 1; y < MAP_HEIGHT - game_hard*2; y++) {
for (int x = 1; x < MAP_WIDTH - 1; x++) {
map[y][x] = map[y][x + 1];
}
}
for (int y = 1; y < MAP_HEIGHT - 5; y++) {
map[y][MAP_WIDTH - 2] = (rand() % 100 < 15) ? 2 : 0;
}
static int distance = 0;
distance += 1;
if (distance >= MAP_WIDTH * game_hard) {
isFinish = true;
MessageBox(NULL, "恭喜!到达终点!", "游戏胜利", MB_OK);
exit(0);
}
}
if (map[carY][carX] == 1 || map[carY][carX] == 2) {
speed = 0;
MessageBox(NULL, "撞到边框/障碍物了!速度清零!(躲开障碍物(W或S)后按D)", "提示", MB_OK);
}
}//撞墙提示
void drawMap() {
system("cls");// 画边框 空白 障碍 车
for (int y = 0; y < MAP_HEIGHT; y++) {//画图
for (int x = 0; x < MAP_WIDTH; x++) {// 画车
if (x == carX && y == carY) {
std::cout << "*";
}
else if (map[y][x] == 1) {
std::cout << "--";
x++; // --占2列,跳过下一列
}
else if (map[y][x] == 2) {
std::cout << "#";
}
else {
std::cout << " ";
}
}
std::cout << std::endl;
}
std::cout << "========== 边框式竞速赛车 ==========" << std::endl;
std::cout << "当前速度:" << speed << " (最大速度:" << MAX_SPEED << ")" << std::endl;
std::cout << "控制键:W(上) S(下) A(减速) D(加速) Q(退出)" << std::endl;
std::cout << "赛道边框(--) 赛车(*) 障碍物(#) 终点:向右行驶到底" << std::endl; //面板
}
void controlCar() {
if (_kbhit() && !isFinish) {
switch (_getch()) {
case 'w': case 'W': // 上移
if (carY - 1 > 0 && map[carY - 1][carX] != 1 && map[carY - 1][carX] != 2) {
carY--;
}
break;
case 's': case 'S': // 下移
if (carY + 1 < MAP_HEIGHT - 1 && map[carY + 1][carX] != 1 && map[carY + 1][carX] != 2) {
carY++;
}
break;
case 'a': case 'A': // 减速
speed = (speed - 1 < MIN_SPEED) ? MIN_SPEED : speed - 1;
break;
case 'd': case 'D':
speed = (speed + 1 > MAX_SPEED) ? MAX_SPEED : speed + 1;
break;
case 'q': case 'Q':
exit(0);
break;
}
}
}
int main() {
do {
std::cout << "输入游戏难度(1~15)";
if (!(std::cin >> game_hard)) {
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "请输入有效数字!" << std::endl;
} else if (game_hard < 1 || game_hard > 15) {
std::cout << "请输入1~15之间的数字!" << std::endl;
}
} while (game_hard < 1 || game_hard > 15 || !std::cin);
initMap();
while (true) {//游戏循环
drawMap();
controlCar();
scrollMap();
Sleep(200);
}
return 0;
}//第一稿
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>
#include <vector>
using namespace std;
// 游戏常量定义
const int MAP_HEIGHT = 15;
const int MAP_WIDTH = 75;
const int CAR_INIT_X = MAP_WIDTH / 2;
const int CAR_INIT_Y = MAP_HEIGHT - 2;
// 全局变量(兼容旧编译器的vector写法)
vector<vector<int> > map(MAP_HEIGHT, vector<int>(MAP_WIDTH, 0));
int carX = CAR_INIT_X, carY = CAR_INIT_Y;
int speed = 1;
int game_hard = 1;
bool isFinish = false;
// 设置控制台光标位置
void setCursorPosition(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// 隐藏控制台光标
void hideCursor() {
CONSOLE_CURSOR_INFO cci;
cci.bVisible = FALSE;
cci.dwSize = 1;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
}
// 初始化地图(核心修改:终点线位置随难度动态调整)
void initMap() {
// 清空地图
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
map[y][x] = 0;
}
}
// 绘制边框
for (int x = 0; x < MAP_WIDTH; x++) {
map[0][x] = 1;
map[MAP_HEIGHT-1][x] = 1;
}
for (int y = 0; y < MAP_HEIGHT; y++) {
map[y][0] = 1;
map[y][MAP_WIDTH-1] = 1;
}
// 生成障碍物
int obstacleCount = 10 + game_hard * 2;
for (int i = 0; i < obstacleCount; i++) {
int obsX = rand() % (MAP_WIDTH - 2) + 1;
int obsY = rand() % (MAP_HEIGHT - 2) + 1;
if (!(obsX == carX && obsY == carY)) {
map[obsY][obsX] = 2;
}
}
// 核心修改:计算终点线位置(离车辆初始位置 MAP_WIDTH*game_hard*2)
// 限制终点线最大位置不超过地图宽度(避免越界)
int endLineStartX = min(CAR_INIT_X + MAP_WIDTH * game_hard * 2, MAP_WIDTH - 10);
int endLineEndX = min(endLineStartX + 20, MAP_WIDTH - 2); // 终点线长度固定20个字符
// 绘制终点线(y固定为1行,x为计算出的位置)
for (int x = endLineStartX; x < endLineEndX; x++) {
map[1][x] = 3;
}
// 放置车辆
map[carY][carX] = 4;
}
// 地图滚动逻辑(同步修改终点线重绘位置)
void scrollMap() {
if (isFinish) return;
// 先清空赛车当前位置
map[carY][carX] = 0;
// 整体向右滚动
for (int y = 1; y < MAP_HEIGHT - 1; y++) {
for (int x = MAP_WIDTH - 2; x > 0; x--) {
map[y][x] = map[y][x - 1];
}
}
// 左侧补充新内容
for (int y = 1; y < MAP_HEIGHT - 1; y++) {
int randVal = rand() % (20 - game_hard);
if (randVal == 0 && !(y == carY && 1 == carX)) {
map[y][1] = 2;
} else {
map[y][1] = 0;
}
}
// 重新绘制终点线(同步动态位置)
int endLineStartX = min(CAR_INIT_X + MAP_WIDTH * game_hard * 2, MAP_WIDTH - 10);
int endLineEndX = min(endLineStartX + 20, MAP_WIDTH - 2);
for (int x = endLineStartX; x < endLineEndX; x++) {
map[1][x] = 3;
}
// 重新放置赛车
map[carY][carX] = 4;
}
// 车辆控制
void controlCar() {
if (_kbhit() && !isFinish) {
char key = _getch();
// 清空原车辆位置
map[carY][carX] = 0;
// 按键逻辑
switch (key) {
case 'w': case 'W':
if (carY > 1) carY--;
break;
case 's': case 'S':
if (carY < MAP_HEIGHT - 2) carY++;
break;
case 'a': case 'A':
if (carX > 1) carX--;
break;
case 'd': case 'D':
if (carX < MAP_WIDTH - 2) carX++;
break;
default:
break;
}
// 碰撞检测
if (map[carY][carX] == 1 || map[carY][carX] == 2) {
speed = 0;
MessageBox(NULL, "撞到边框/障碍物了!速度清零!(躲开障碍物(W或S)后按D)", "提示", MB_OK);
}
// 终点检测
if (map[carY][carX] == 3) {
isFinish = true;
MessageBox(NULL, "恭喜!到达终点!", "游戏胜利", MB_OK);
}
// 重置车辆位置
map[carY][carX] = 4;
}
}
// 绘制地图(显示符号保持不变)
void drawMap() {
setCursorPosition(0, 0);
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
switch (map[y][x]) {
case 1: cout << "#"; break; // 边框
case 2: cout << "#"; break; // 障碍物
case 3: cout << "|"; break; // 终点线
case 4: cout << "*"; break; // 赛车
default: cout << " "; break; // 空白
}
}
cout << endl;
}
// 显示游戏信息(新增终点位置提示)
cout << "难度:" << game_hard << " | 速度:" << speed
<< " | 终点位置:" << CAR_INIT_X + MAP_WIDTH * game_hard * 2
<< " | 控制:W(上) S(下) A(左) D(右)" << endl;
}
int main() {
// 随机数播种
srand((unsigned int)time(NULL));
hideCursor();
// 难度输入验证
do {
cout << "请输入游戏难度(1-10):";
cin >> game_hard;
if (cin.fail() || game_hard < 1 || game_hard > 10) {
cin.clear();
cin.ignore(1024, '\n');
cout << "输入无效!请输入1-10之间的整数。" << endl;
} else {
break;
}
} while (true);
// 初始速度随难度提升
speed = 1 + game_hard / 3;
// 初始化游戏
initMap();
// 游戏主循环
while (!isFinish) {
drawMap();
controlCar();
scrollMap();
Sleep(1000 / (speed * 2));
}
// 游戏结束
cout << "\n游戏结束!感谢游玩!" << endl;
return 0;
}//第二稿
简单稳定版:
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <windows.h>
#include <ctime>
#include <vector>
using namespace std;
// 核心常量(极简定义)
const int MAP_HEIGHT = 15;
const int MAP_WIDTH = 75;
const int MAX_SPEED = 5;
const int MIN_SPEED = 1;
const int TRACK_LEFT = 2; // 跑道上边界
const int TRACK_RIGHT = 12; // 跑道下边界(MAP_HEIGHT-3=12)
const int CAR_START_X = 5; // 赛车初始X位置
// 全局变量(仅保留必要的)
int game_hard = 1;
int carY = MAP_HEIGHT / 2; // 赛车Y位置(居中)
int carX = CAR_START_X; // 赛车X位置
int speed = 1; // 赛车速度
int endLineX; // 终点线X坐标
vector<vector<int> > map(MAP_HEIGHT, vector<int>(MAP_WIDTH, 0));
// 高效清屏(保留优化)
void clearScreen() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {0, 0};
DWORD written;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);
DWORD size = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, ' ', size, coord, &written);
SetConsoleCursorPosition(hConsole, coord);
}
// 初始化地图(极简逻辑)
void initMap() {
// 1. 清空地图
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
map[y][x] = 0;
}
}
// 2. 绘制跑道边框(/ 表示左右边界,- 表示底部)
for (int x = 0; x < MAP_WIDTH; x++) {
map[TRACK_LEFT][x] = 1; // 上跑道线(/)
map[TRACK_RIGHT][x] = 1; // 下跑道线(/)
if (x % 3 == 0) map[MAP_HEIGHT-2][x] = 1; // 底部线(-)
}
// 3. 计算终点线(严格按公式:初始X + 地图高度 × 难度 × 2)
endLineX = CAR_START_X + MAP_HEIGHT * game_hard * 2;
// 安全边界:终点线不超出地图(最大X=70)
endLineX = min(endLineX, MAP_WIDTH - 5);
map[MAP_HEIGHT/2][endLineX] = 3; // 绘制终点线(|)
// 4. 生成障碍物(数量随难度,仅在跑道内)
srand((unsigned int)time(NULL));
int obstacleCount = 3 + game_hard; // 难度越高,障碍物越多(3~18个)
for (int i = 0; i < obstacleCount; i++) {
int obsX = rand() % (endLineX - 10) + 10; // 障碍物在终点线左侧
int obsY = rand() % (TRACK_RIGHT - TRACK_LEFT - 1) + TRACK_LEFT + 1;
map[obsY][obsX] = 2; // 障碍物(#)
}
}
// 绘制游戏界面(核心:物体永不消失)
void drawGame() {
clearScreen();
// 第一步:绘制所有固定物体
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
if (map[y][x] == 1) {
cout << (y == MAP_HEIGHT-2 ? '-' : '/');
} else if (map[y][x] == 2) {
cout << '#';
} else if (map[y][x] == 3) {
cout << '|';
} else {
cout << ' ';
}
}
cout << endl;
}
// 第二步:绘制赛车
COORD carPos = {(SHORT)carX, (SHORT)carY};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), carPos);
cout << '*';
// --- 核心修改:把信息移到屏幕最下方 ---
COORD infoPos = {0, (SHORT)(MAP_HEIGHT + 2)}; // 定位到地图下方2行
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), infoPos);
// 输出游戏信息
cout << "========== 极简稳定版赛车游戏 ==========\n";
cout << "难度:" << game_hard << " | 当前速度:" << speed << " (最大" << MAX_SPEED << ")\n";
cout << "赛车位置:X=" << carX << ", Y=" << carY << " | 终点线位置:X=" << endLineX << "\n";
cout << "操作:W(上) S(下) A(减速) D(加速) Q(退出)\n";
}
// 玩家控制(极简逻辑,无BUG)
void playerControl() {
if (_kbhit()) {
char key = _getch();
switch (key) {
case 'w': case 'W':
// 向上移动:不超出跑道上边界,不碰障碍物
if (carY - 1 > TRACK_LEFT && map[carY - 1][carX] != 2) {
carY--;
}
break;
case 's': case 'S':
// 向下移动:不超出跑道下边界,不碰障碍物
if (carY + 1 < TRACK_RIGHT && map[carY + 1][carX] != 2) {
carY++;
}
break;
case 'a': case 'A':
// 减速:不低于最小速度
speed = max(speed - 1, MIN_SPEED);
break;
case 'd': case 'D':
// 加速:不高于最大速度
speed = min(speed + 1, MAX_SPEED);
break;
case 'q': case 'Q':
// 退出游戏
exit(0);
break;
}
}
}
// 赛车自动前进(核心逻辑)
void carMove() {
// 1. 自动向右前进(速度决定移动距离)
carX += speed;
// 边界保护:不超出地图
carX = min(carX, MAP_WIDTH - 2);
// 2. 碰撞检测:碰到障碍物速度清零
if (map[carY][carX] == 2) {
speed = 0;
// 回退一格,避免卡在障碍物里
if (carX - 1 >= CAR_START_X) {
carX--;
}
}
// 3. 终点检测:到达终点线胜利
if (carX >= endLineX) {
clearScreen();
cout << "\n\n========================" << endl;
cout << " 游戏胜利! " << endl;
cout << "========================" << endl;
cout << "难度:" << game_hard << endl;
cout << "最终速度:" << speed << endl;
cout << "到达终点线:X=" << endLineX << endl;
cout << "\n按任意键退出..." << endl;
_getch();
exit(0);
}
}
// 主函数(极简流程)
int main() {
// 关闭控制台快速编辑模式(避免卡顿)
HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hInput, &mode);
mode &= ~ENABLE_QUICK_EDIT_MODE;
SetConsoleMode(hInput, mode);
// 输入难度(极简验证)
cout << "请输入游戏难度(1~10):";
while (!(cin >> game_hard) || game_hard < 1 || game_hard > 10) {
cin.clear();
cin.ignore(1000, '\n');
cout << "输入错误!请输入1~10之间的数字:";
}
// 初始化地图
initMap();
// 游戏主循环(极简,零BUG)
while (true) {
drawGame(); // 绘制界面(物体永不消失)
playerControl();// 玩家操作
carMove(); // 自动前进+碰撞+终点检测
Sleep(50); // 适中的帧率,流畅不卡顿
}
return 0;
}
全部评论 3
赞一个吧~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1周前 来自 广东
3关注一下啊啊啊
6天前 来自 广东
1
6天前 来自 广东
0


























有帮助,赞一个