地狱闯关游戏
2025-05-28 16:49:12
发布于:浙江
我本人做了一个游戏,叫“地狱闯关游戏”。
代码如下:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <conio.h> // 用于_getch()函数(Windows系统)
using namespace std;
// 游戏常量定义
const int WIDTH = 10;
const int HEIGHT = 10;
const char WALL = '#';
const char FLOOR = '.';
const char PLAYER = 'P';
const char KEY = 'K';
const char TRAP = 'T';
const char EXIT = 'E';
const char VISITED = ' ';
// 玩家结构体
struct Player {
int x, y;
int health;
int keys;
bool hasWon;
};
// 游戏地图
vector<vector<char>> createMap() {
vector<vector<char>> map(HEIGHT, vector<char>(WIDTH, FLOOR));
// 添加墙壁
for (int i = 0; i < HEIGHT; i++) {
map[i][0] = WALL;
map[i][WIDTH-1] = WALL;
}
for (int j = 0; j < WIDTH; j++) {
map[0][j] = WALL;
map[HEIGHT-1][j] = WALL;
}
// 随机添加墙壁
srand(time(0));
for (int i = 2; i < HEIGHT-1; i++) {
for (int j = 2; j < WIDTH-1; j++) {
if (rand() % 10 == 0) {
map[i][j] = WALL;
}
}
}
// 添加钥匙(1-3个)
int keyCount = rand() % 3 + 1;
for (int k = 0; k < keyCount; k++) {
int x, y;
do {
x = rand() % (WIDTH-4) + 2;
y = rand() % (HEIGHT-4) + 2;
} while (map[y][x] != FLOOR);
map[y][x] = KEY;
}
// 添加陷阱(3-5个)
int trapCount = rand() % 3 + 3;
for (int k = 0; k < trapCount; k++) {
int x, y;
do {
x = rand() % (WIDTH-4) + 2;
y = rand() % (HEIGHT-4) + 2;
} while (map[y][x] != FLOOR);
map[y][x] = TRAP;
}
// 添加出口(确保不在边缘)
int ex, ey;
do {
ex = rand() % (WIDTH-4) + 2;
ey = rand() % (HEIGHT-4) + 2;
} while (map[ey][ex] != FLOOR);
map[ey][ex] = EXIT;
// 确保玩家起始位置
map[1][1] = PLAYER;
return map;
}
// 显示地图
void displayMap(const vector<vector<char>>& map, const Player& player) {
system("cls"); // 清屏(Windows)
// system("clear"); // Linux/Mac系统使用这行
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (i == player.y && j == player.x) {
cout << PLAYER;
} else {
// 显示已访问区域
bool visited = false;
for (int y = max(0, i-2); y <= min(HEIGHT-1, i+2); y++) {
for (int x = max(0, j-2); x <= min(WIDTH-1, j+2); x++) {
if (y == player.y && x == player.x) continue;
if (abs(y - player.y) + abs(x - player.x) <= 3) {
if (map[y][x] != WALL && map[y][x] != PLAYER &&
map[y][x] != KEY && map[y][x] != TRAP && map[y][x] != EXIT) {
visited = true;
}
}
}
}
if (visited && map[i][j] == FLOOR) {
cout << VISITED;
} else {
cout << map[i][j];
}
}
}
cout << endl;
}
// 显示玩家状态
cout << "\n生命值: " << player.health
<< " | 钥匙: " << player.keys
<< " | 使用方向键移动, 按ESC退出" << endl;
}
// 检查游戏状态
bool checkGameState(const vector<vector<char>>& map, Player& player) {
if (map[player.y][player.x] == EXIT && player.keys > 0) {
player.hasWon = true;
return true;
}
if (map[player.y][player.x] == TRAP) {
player.health -= 20;
map[player.y][player.x] == FLOOR; // 陷阱触发后消失
if (player.health <= 0) {
return true; // 游戏结束
}
}
if (map[player.y][player.x] == KEY) {
player.keys++;
map[player.y][player.x] == FLOOR; // 钥匙拾取后消失
}
return false;
}
int main() {
cout << "欢迎来到地狱闯关游戏!" << endl;
cout << "目标: 收集钥匙并到达出口(E)" << endl;
cout << "注意: 地图上有陷阱(T),踩到会减少生命值" << endl;
cout << "按任意键开始游戏..." << endl;
_getch();
Player player = {1, 1, 100, 0, false};
vector<vector<char>> map = createMap();
while (true) {
displayMap(map, player);
if (checkGameState(map, player)) {
break;
}
// 获取玩家输入
char input = _getch();
int newX = player.x;
int newY = player.y;
switch (input) {
case 'w': case 'W': case 72: // 上(箭头键上是72)
newY--;
break;
case 's': case 'S': case 80: // 下
newY++;
break;
case 'a': case 'A': case 75: // 左
newX--;
break;
case 'd': case 'D': case 77: // 右
newX++;
break;
case 27: // ESC键退出
cout << "游戏已退出。" << endl;
return 0;
default:
continue;
}
// 检查移动是否有效
if (newX >= 0 && newX < WIDTH && newY >= 0 && newY < HEIGHT) {
if (map[newY][newX] != WALL) {
player.x = newX;
player.y = newY;
}
}
}
displayMap(map, player);
if (player.hasWon) {
cout << "恭喜你成功逃出地狱!" << endl;
cout << "你收集了 " << player.keys << " 把钥匙,剩余生命值: " << player.health << endl;
} else {
cout << "游戏结束!你的生命值耗尽了。" << endl;
}
return 0;
}
游戏说明
1.游戏目标:收集钥匙并到达出口(E)
2.控制方式:
· 使用WASD或方向键移动
· ESC键退出游戏
3.游戏元素:
· P: 玩家
· K: 钥匙(需要收集)
· T: 陷阱(踩到会减少生命值)
· E: 出口(需要钥匙才能通过)
· #: 墙壁(不可通过)
· .: 地板(可通行)
注意事项
1.这个游戏使用了conio.h库中的_getch()函数来获取键盘输入,这在Windows系统上可用。如果你使用Linux/Mac,需要替换为其他输入方法(如ncurses库)。
游戏地图是随机生成的,每次运行都会不同。
2.你可以根据需要调整游戏难度,如修改玩家初始生命值、陷阱伤害等参数。
全部评论 2
连夜赶得
哭2025-05-27 来自 浙江
1我是第一个吗??
2025-05-27 来自 浙江
1是的
2025-05-27 来自 浙江
1
有帮助,赞一个