这是一款基于控制台的双模式迷宫冒险游戏。在极限逃脱模式中,玩家需操控角色"A"在三个精心设计的关卡中躲避追踪者"B",通过WASD键在100步限制内抵达终点"@",关卡包含特殊地形和动态敌人机制。无尽挑战模式则采用随机生成的渐进式迷宫,每关迷宫尺寸随等级扩大,玩家需在无限扩展的迷宫中不断挑战。游戏提供可视化操作界面,通过方向键控制移动,支持中途退出功能(o)。两种模式分别提供3个固定关卡和无限递增关卡,通过启动菜单选择模式后即可开始冒险,考验玩家的路径规划能力和反应速度。代码如下:
#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <windows.h> #include <time.h> #define MAX_W 50000 #define MAX_H 30000 char map[MAX_H][MAX_W]; char map1[100][100] = { "##########", "#A #", "# ## #", "# ## #", "# ## #", "# B@#", "##########" }; char map2[100][100] = { "####################", "#A B#", "### ############ #", " # # # #", " # # ###### # #", " # # # # #", " # ###### # #", " # # # # #", " # # ###### # #", " # # # #", " # #### #### #", " # #################", " # B@#", " ####################" }; char map3[100][100] = { "###############################################", "# #B# #", "# # # ################ ###", "# # # # # #", "# ################### # # ############## ###", "# ||| # #", "# ################### ##################### #", "# # # # # # # ##A# # #", "# # # # # ################", "# ~~~ ~~~ # # # # # # # # # # #", "# 0 0 # # #### # # # # # # #", "# . # . # # # # # # # # # #", "# . . # #### ##################", "# . . # # #", "# #B# ################# #", "# #@# #", "###############################################" }; int t = 1; int W, H; // 迷宫的宽度和高度 int player_x, player_y; // 玩家的位置 int exit_x, exit_y; // 终点的位置 // 显示游戏界面 void start() { printf("*********************\n"); printf("** ** ** ** **\n"); printf("*** * * *** * * ** *\n"); printf("欢迎来到走迷宫游戏!\n"); printf("** * * * *** * * ** *\n"); printf("*** * * *** * * ** *\n"); printf("*********************\n"); printf("请按任意键\n"); _getch(); system("cls"); printf("本游戏分为两个模式:\n"); printf("1.极限逃脱模式\n"); printf("2.无尽挑战模式\n"); printf("请先选择游戏模式(1/2):"); } // 初始化迷宫,全部设置为墙壁 void initialize_maze() { for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { map[y][x] = '#'; // 内部初始化为墙壁 } } } // 检查坐标是否在迷宫范围内(不包括边界) int is_in_bounds(int x, int y) { return x > 0 && x < W - 1 && y > 0 && y < H - 1; } // 递归生成迷宫 void generate_maze(int x, int y) { map[y][x] = ' '; // 打通当前格子 // 四个方向 int directions[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} }; // 打乱方向顺序 for (int i = 0; i < 4; i++) { int r = rand() % 4; int temp_x = directions[i][0]; int temp_y = directions[i][1]; directions[i][0] = directions[r][0]; directions[i][1] = directions[r][1]; directions[r][0] = temp_x; directions[r][1] = temp_y; } // 尝试向四个方向扩展 for (int i = 0; i < 4; i++) { int dx = directions[i][0]; int dy = directions[i][1]; int nx = x + dx * 2; // 前进两个格子 int ny = y + dy * 2; if (is_in_bounds(nx, ny) && map[ny][nx] == '#') { map[y + dy][x + dx] = ' '; // 打通中间的格子 generate_maze(nx, ny); // 递归生成 } } } // 打印迷宫 void print_maze() { system("cls"); printf("欢迎来到第%d关\n\n", t); for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { if (x == player_x && y == player_y) { printf("A"); // 玩家位置 } else if (x == exit_x && y == exit_y) { printf("@"); // 终点位置 } else { printf("%c", map[y][x]); } } printf("\n"); } } // 初始化关卡 void initialize_level(int level) { W = 20 + level * 2 + 1; // 宽度随关卡增加 H = 10 + level; // 高度随关卡增加 initialize_maze(); generate_maze(1, 1); // 设置玩家起点 player_x = 1; player_y = 1; // 设置终点 exit_x = W - 2; exit_y = H - 2; map[exit_y][exit_x] = '@'; } // 处理玩家移动 int move_player(char direction) { int x = player_x; int y = player_y; switch (direction) { case 'w': y--; break; case 's': y++; break; case 'a': x--; break; case 'd': x++; break; case'o':return 0; } if (map[y][x] == ' ' || map[y][x] == '@') { player_x = x; player_y = y; } return 0; } // 控制B的移动 void controlB(char z[][100], int aX, int aY, int* bX, int* bY) { int dx = aX - *bX; int dy = aY - *bY; // 优先水平移动 if (dx > 0 && z[*bY][*bX + 1] != '#') { z[*bY][*bX] = ' '; z[*bY][*bX + 1] = 'B'; (*bX)++; } else if (dx < 0 && z[*bY][*bX - 1] != '#') { z[*bY][*bX] = ' '; z[*bY][*bX - 1] = 'B'; (*bX)--; } // 然后垂直移动 else if (dy > 0 && z[*bY + 1][*bX] != '#') { z[*bY][*bX] = ' '; z[*bY + 1][*bX] = 'B'; (*bY)++; } else if (dy < 0 && z[*bY - 1][*bX] != '#') { z[*bY][*bX] = ' '; z[*bY - 1][*bX] = 'B'; (*bY)--; } } // 控制A的移动 int controlA(char z[][100], int x, int y, int endy, int endx, int By, int Bx, int By2, int Bx2) { int time = 100; while (1) { printf("剩余步数:%d\n", time); // 计数器 Sleep(10); time--; if (time == -1) { printf("没有在规定步数内到达终点,游戏失败!!!\n"); return 0; } printf("\n"); for (int i = 0; i < endx + 5; i++) { puts(z[i]); } char ch = _getch(); { if (ch == 'w' && z[y - 1][x] != '#') { z[y][x] = ' '; z[y - 1][x] = 'A'; y--; } if (ch == 's' && z[y + 1][x] != '#') { z[y][x] = ' '; z[y + 1][x] = 'A'; y++; } if (ch == 'a' && z[y][x - 1] != '#') { z[y][x] = ' '; z[y][x - 1] = 'A'; x--; } if (ch == 'd' && z[y][x + 1] != '#') { z[y][x] = ' '; z[y][x + 1] = 'A'; x++; } if (ch == 'o') return 0; } controlB(z, x, y, &Bx, &By); controlB(z, x, y, &Bx2, &By2); system("cls"); if (Bx == x && By == y || Bx2 == x && By2 == y) { system("cls"); printf("您被追上了,游戏失败!!!\n"); return 0; } if (x == endx && y == endy) { return 1; } } } int main() { srand(time(NULL)); // 初始化随机数种子 start(); do { char st = _getch(); system("cls"); if (st == '1') { printf("欢迎来到极限逃脱模式\n"); printf("游戏规则:\n"); printf("w为向上走\n"); printf("s为向下走\n"); printf("a为向左走\n"); printf("d为向右走\n"); printf("在规定步数内,控制A到达@点,同时躲避B的抓捕\n"); printf("请按任意键开始游戏...\n\n<按o可以退出游戏>"); _getch(); system("cls"); int end1 = controlA(map1, 1, 1, 5, 8, 5, 7, 5, 7); int end2, end3; if (end1 == 1) { printf("恭喜通过第一关\n"); printf("是否进入第二关\n"); printf("请输入y/n\n"); char ch; do { ch = _getch(); if (ch == 'Y' || ch == 'y') { system("cls"); end2 = controlA(map2, 1, 1, 12, 20, 1, 18, 12, 20); break; } else if (ch == 'N' || ch == 'n') { printf("已退出游戏"); return 0; } else { printf("\n字符错误\n"); printf("请重新输入:\n"); } } while (1); } else { return 0; } if (end2 == 1) { printf("恭喜通过第二关\n"); printf("是否进入第三关\n"); printf("请输入y/n\n"); char ch; do { ch = _getch(); if (ch == 'Y' || ch == 'y') { system("cls"); end3 = controlA(map3, 23, 7, 15, 23, 1, 23, 14, 23); if (end3 == 1) { printf("恭喜您已经通关全部关卡\n快去体验无尽挑战模式吧\n"); break; } else return 0; } else if (ch == 'N' || ch == 'n') { printf("已退出游戏"); return 0; } else { printf("字符错误\n"); printf("请重新输入:\n"); } } while (1); } else return 0; } else if (st == '2') { printf("欢迎来到无尽挑战模式\n"); printf("游戏规则:\n"); printf("w为向上走\n"); printf("s为向下走\n"); printf("a为向左走\n"); printf("d为向右走\n"); printf("控制A到达@点\n"); printf("请按任意键开始游戏...\n\n<按o可以退出游戏>"); _getch(); system("cls"); int level = 1; while (1) { initialize_level(level); print_maze(); while (1) { char input = _getch(); // 获取键盘输入 if (input == 'o')return 0; move_player(input); print_maze(); if (player_x == exit_x && player_y == exit_y) { t++; level += 2; break; } } } } else { printf("字符错误\n"); printf("请重新输入(1/2):\n"); } } while (1); return 0; }