用c++帮我写一个闯关小游戏
时间: 2023-08-13 07:39:55 浏览: 72
好的,闯关小游戏可以采用控制台界面来实现,代码如下:
```c++
#include <iostream>
#include <conio.h>
#include <Windows.h>
using namespace std;
// 游戏地图
char map[10][20] = {
"###################",
"#@ #",
"# ####### #",
"# # #",
"# # ##### #",
"# ####### # # #",
"# # # # # #",
"# # # # #",
"# # # # #",
"###################"
};
// 角色坐标
int x = 1;
int y = 1;
// 游戏是否结束
bool isGameOver = false;
void drawMap()
{
system("cls"); // 清屏
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 20; j++)
{
cout << map[i][j];
}
cout << endl;
}
}
void move(int dx, int dy)
{
if (map[x + dx][y + dy] == ' ') // 判断目标位置是否为空地
{
map[x][y] = ' '; // 当前位置变为空地
x += dx;
y += dy;
map[x][y] = '@'; // 目标位置变为角色
}
else if (map[x + dx][y + dy] == '#') // 判断目标位置是否为墙
{
return;
}
else if (map[x + dx][y + dy] == '$') // 判断目标位置是否为宝藏
{
map[x][y] = ' '; // 当前位置变为空地
x += dx;
y += dy;
map[x][y] = '@'; // 目标位置变为角色
isGameOver = true; // 游戏胜利
}
}
int main()
{
drawMap();
while (!isGameOver)
{
int ch = _getch(); // 获取键盘输入
switch (ch)
{
case 'w': // 上
move(-1, 0);
break;
case 's': // 下
move(1, 0);
break;
case 'a': // 左
move(0, -1);
break;
case 'd': // 右
move(0, 1);
break;
}
drawMap();
}
cout << "恭喜你通关啦!" << endl;
system("pause");
return 0;
}
```
这是一个简单的地图移动游戏,玩家需要控制角色移动到宝藏位置,遇到墙就无法通过,移动时可以使用 w、s、a、d 这四个方向键,移动成功就会刷新地图,直到到达宝藏位置,游戏结束。
相关推荐
![-](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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)