我做了一个游戏,本来不想发的结果~~~,话不多说,上代码:
亮亮 2024/7/1 9:52:14
#include <stdio.h>
#include <stdlib.h>
#include <conio.h> // 包含 getch() 函数
#include <unistd.h> // 包含 sleep() 函数
int main() {
char a[50][50] = {
"######",
"#o # %",
"# ## #",
"# # #",
"## #",
"######"
};
char b[50][50] = {
"######",
"#o %",
"# # #",
"# ###",
"## #",
"######"
};
char c[50][50] = {
"####################",
"#o # ## ### #####",
"# ## # #####",
"# # ######### #####",
"## ####### %##",
"####################"
}; // 第三张地图
int i, x, y, p;
char ch;
int gameRunning = 1; // 添加一个标志变量来控制游戏循环
// 初始化玩家位置
x = 1, y = 1;
// 让玩家选择地图
printf("选择地图:1为a地图,2为b地图,3为c地图\n");
scanf("%d", &p);
// 打印初始迷宫并开始游戏
if(p == 1) {
// 打印初始迷宫
for (i = 0; i <= 5; i++) {
puts(a[i]);
}
// 游戏主循环
while (gameRunning) {
ch = getch(); // 获取用户输入
// 根据用户输入移动玩家
switch (ch) {
case 's':
case 'S':
if (a[x + 1][y] != '#') {
if (a[x + 1][y] == '%') {
gameRunning = 0; // 设置标志变量为 0,退出循环
}
a[x][y] = ' ';
x++;
a[x][y] = 'o';
}
break;
case 'w':
case 'W':
if (a[x - 1][y] != '#') {
if (a[x - 1][y] == '%') {
gameRunning = 0; // 设置标志变量为 0,退出循环
}
a[x][y] = ' ';
x--;
a[x][y] = 'o';
}
break;
case 'a':
case 'A':
if (a[x][y - 1] != '#') {
if (a[x][y - 1] == '%') {
gameRunning = 0; // 设置标志变量为 0,退出循环
}
a[x][y] = ' ';
y--;
a[x][y] = 'o';
}
break;
case 'd':
case 'D':
if (a[x][y + 1] != '#') {
if (a[x][y + 1] == '%') {
gameRunning = 0; // 设置标志变量为 0,退出循环
}
a[x][y] = ' ';
y++;
a[x][y] = 'o';
}
break;
}
system("cls"); // 清屏
// 重新打印迷宫
for (i = 0; i <= 5; i++) {
puts(a[i]);
}
}
} else if(p == 2) {
// 打印初始迷宫
// 打印初始迷宫
for (i = 0; i <= 5; i++) {
puts(b[i]);
}
// 游戏主循环
while (gameRunning) {
ch = getch(); // 获取用户输入
// 根据用户输入移动玩家
switch (ch) {
case 's':
case 'S':
if (b[x + 1][y] != '#') {
if (b[x + 1][y] == '%') {
gameRunning = 0; // 设置标志变量为 0,退出循环
}
b[x][y] = ' ';
x++;
b[x][y] = 'o';
}
break;
case 'w':
case 'W':
if (b[x - 1][y] != '#') {
if (b[x - 1][y] == '%') {
gameRunning = 0; // 设置标志变量为 0,退出循环
}
b[x][y] = ' ';
x--;
b[x][y] = 'o';
}
break;
case 'a':
case 'A':
if (b[x][y - 1] != '#') {
亮亮 2024/7/1 9:52:20
if (b[x][y - 1] == '%') {
gameRunning = 0; // 设置标志变量为 0,退出循环
}
b[x][y] = ' ';
y--;
b[x][y] = 'o';
}
break;
case 'd':
case 'D':
if (b[x][y + 1] != '#') {
if (b[x][y + 1] == '%') {
gameRunning = 0; // 设置标志变量为 0,退出循环
}
b[x][y] = ' ';
y++;
b[x][y] = 'o';
}
break;
}
system("cls"); // 清屏
// 重新打印迷宫
for (i = 0; i <= 5; i++) {
puts(b[i]);
}
}
} else if(p == 3) {
// 打印初始迷宫
for (i = 0; i <= 5; i++) {
puts(c[i]);
}
// 游戏主循环
while (gameRunning) {
ch = getch(); // 获取用户输入
// 根据用户输入移动玩家
switch (ch) {
case 's':
case 'S':
if (c[x + 1][y] != '#') {
if (c[x + 1][y] == '%') {
gameRunning = 0; // 设置标志变量为 0,退出循环
}
c[x][y] = ' ';
x++;
c[x][y] = 'o';
}
break;
case 'w':
case 'W':
if (c[x - 1][y] != '#') {
if (c[x - 1][y] == '%') {
gameRunning = 0; // 设置标志变量为 0,退出循环
}
c[x][y] = ' ';
x--;
c[x][y] = 'o';
}
break;
case 'a':
case 'A':
if (c[x][y - 1] != '#') {
if (c[x][y - 1] == '%') {
gameRunning = 0; // 设置标志变量为 0,退出循环
}
c[x][y] = ' ';
y--;
c[x][y] = 'o';
}
break;
case 'd':
case 'D':
if (c[x][y + 1] != '#') {
if (c[x][y + 1] == '%') {
gameRunning = 0; // 设置标志变量为 0,退出循环
}
c[x][y] = ' ';
y++;
c[x][y] = 'o';
}
break;
}
system("cls"); // 清屏
// 重新打印迷宫
for (i = 0; i <= 5; i++) {
puts(c[i]);
}
}
}
// 清屏
system("cls");
printf("You win!\n");
sleep(5); // 等待 5 秒后退出
return 0;
}