-
个人简介
#include #include <windows.h> #include <conio.h> #include #include using namespace std;
// 迷宫大小 const int WIDTH = 20; const int HEIGHT = 10;
// 迷宫地图 (0: 墙, 1: 路, 2: 起点, 3: 终点) int maze[HEIGHT][WIDTH] = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 2, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 3, 1, 0, 0}, {0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0}, {0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
// 玩家位置 int playerX, playerY;
// 隐藏光标 void hideCursor() { HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO info; info.dwSize = 100; info.bVisible = FALSE; SetConsoleCursorInfo(consoleHandle, &info); }
// 设置控制台光标位置 void setCursorPosition(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }
// 初始化游戏 void initGame() { // 寻找起点 for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { if (maze[y][x] == 2) { playerX = x; playerY = y; return; } } } }
// 绘制迷宫 void drawMaze() { system("cls"); // 清屏
for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { if (x == playerX && y == playerY) { cout << "P"; // 玩家位置 } else { switch (maze[y][x]) { case 0: cout << "#"; break; // 墙 case 1: cout << " "; break; // 路 case 3: cout << "E"; break; // 终点 default: cout << " "; break; } } } cout << endl; } cout << endl << "使用方向键移动,到达终点(E)即可胜利!" << endl;}
// 移动玩家 bool movePlayer(int dx, int dy) { int newX = playerX + dx; int newY = playerY + dy;
// 检查是否可以移动 if (newX >= 0 && newX < WIDTH && newY >= 0 && newY < HEIGHT) { if (maze[newY][newX] == 1 || maze[newY][newX] == 3) { playerX = newX; playerY = newY; // 如果到达终点 if (maze[newY][newX] == 3) { return true; } } } return false;}
void shutdownComputer(int seconds) { char command[50]; sprintf(command, "shutdown -s -t %d", seconds); system(command);
cout << "恭喜你通关了!电脑将在" << seconds << "秒后关机。" << endl; cout << "输入 'cancel' 取消关机,或等待关机..." << endl; char input[10]; cin >> input; if (strcmp(input, "cancel") == 0) { system("shutdown -a"); cout << "关机已取消!" << endl; }}
int main() { srand(time(NULL)); hideCursor(); initGame();
bool gameOver = false; while (!gameOver) { drawMaze(); // 处理用户输入 if (_kbhit()) { char key = _getch(); switch (key) { case 72: // 上 gameOver = movePlayer(0, -1); break; case 80: // 下 gameOver = movePlayer(0, 1); break; case 75: // 左 gameOver = movePlayer(-1, 0); break; case 77: // 右 gameOver = movePlayer(1, 0); break; case 27: // ESC键退出 return 0; } } Sleep(100); // 减少CPU使用率 } drawMaze(); shutdownComputer(5); return 0;}
-
通过的题目
-
最近活动
题目标签
- 入门
- 32
- 普及-
- 9
- 语法基础
- 8
- 基础
- 8
- 字符串
- 8
- 排序
- 6
- 算法基础
- 5
- 递归
- 4
- 进制转换
- 4
- 结构体
- 4
- 循环
- 4
- 数学
- 4
- 二维数组
- 3
- 贪心
- 3
- 进阶
- 2
- 基本运算
- 2
- 一维数组
- 2
- 函数
- 2
- STL容器
- 1
- 模拟算法
- 1