用c++帮我写一个闯关小游戏
时间: 2023-08-13 15:40:02 浏览: 32
好的,我可以帮你写一个简单的控制台闯关小游戏。以下是代码:
```c++
#include <iostream>
#include <conio.h> // 用于控制台输入
#include <windows.h> // 用于控制台界面
using namespace std;
const int MAP_HEIGHT = 10; // 地图高度
const int MAP_WIDTH = 20; // 地图宽度
const int MAX_STEP = 30; // 最大步数
int map[MAP_HEIGHT][MAP_WIDTH] = {0}; // 地图数组
int playerX = 0; // 玩家X坐标
int playerY = 0; // 玩家Y坐标
int stepCount = 0; // 步数计数器
// 初始化地图
void initMap() {
// 设置边界
for (int i = 0; i < MAP_WIDTH; i++) {
map[0][i] = 1;
map[MAP_HEIGHT - 1][i] = 1;
}
for (int i = 0; i < MAP_HEIGHT; i++) {
map[i][0] = 1;
map[i][MAP_WIDTH - 1] = 1;
}
// 设置障碍物
map[2][2] = 1;
map[2][3] = 1;
map[2][4] = 1;
map[3][4] = 1;
map[4][4] = 1;
map[5][4] = 1;
map[6][4] = 1;
map[6][3] = 1;
map[6][2] = 1;
// 设置起点和终点
playerX = 1;
playerY = 1;
map[playerY][playerX] = 2;
map[MAP_HEIGHT - 2][MAP_WIDTH - 2] = 3;
}
// 绘制地图
void drawMap() {
system("cls"); // 清空屏幕
for (int i = 0; i < MAP_HEIGHT; i++) {
for (int j = 0; j < MAP_WIDTH; j++) {
if (map[i][j] == 0) {
cout << " "; // 空地
}
else if (map[i][j] == 1) {
cout << "#"; // 障碍物
}
else if (map[i][j] == 2) {
cout << "S"; // 起点
}
else if (map[i][j] == 3) {
cout << "E"; // 终点
}
}
cout << endl;
}
cout << "步数:" << stepCount << "/" << MAX_STEP << endl;
}
// 移动玩家
void movePlayer(int dirX, int dirY) {
int newX = playerX + dirX;
int newY = playerY + dirY;
if (map[newY][newX] == 0 || map[newY][newX] == 3) {
// 如果目标位置是空地或终点,可以移动到该位置
map[playerY][playerX] = 0;
playerX = newX;
playerY = newY;
map[playerY][playerX] = 2;
stepCount++; // 步数加一
}
}
int main() {
initMap(); // 初始化地图
drawMap(); // 绘制地图
while (true) {
int key = getch(); // 获取键盘输入
if (key == 224) { // 方向键
key = getch();
switch (key) {
case 72: // 上
movePlayer(0, -1);
break;
case 80: // 下
movePlayer(0, 1);
break;
case 75: // 左
movePlayer(-1, 0);
break;
case 77: // 右
movePlayer(1, 0);
break;
}
drawMap(); // 绘制地图
if (playerX == MAP_WIDTH - 2 && playerY == MAP_HEIGHT - 2) {
// 如果到达终点,游戏结束
cout << "你赢了!" << endl;
break;
}
if (stepCount >= MAX_STEP) {
// 如果步数用完,游戏结束
cout << "你输了!" << endl;
break;
}
}
}
return 0;
}
```
这个游戏的玩法很简单,你需要控制一个人物从起点走到终点,期间需要躲避障碍物。你可以用方向键来控制人物的移动,每移动一步步数计数器就会加一,当步数超过最大步数或者到达终点时游戏结束。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)