好玩走迷宫(不要错过)

我做了一个游戏,本来不想发的结果~~~话不多说,上代码:
 

亮亮 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;
}

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值