【C#】控制台推箱子

帮我看一下怎么改

public static void Sokoban() /*推箱子*/
        {
        restart:
            Console.ForegroundColor = ConsoleColor.White;
            Console.Clear();
            int[,] Map;
            int _4x, _4y;
            int XLength, YLength;
            int Isnumber;

            //选择关卡页面
            Console.WriteLine("\n\t\t\t\t   >> 选择 关卡 <<\n\n\t\t\t\t【Enter】默认套装[ 1 ]\n");
            Console.WriteLine("\t\t\t\t\t[ 1 ] \n");
            Console.WriteLine("\t\t\t\t\t[ 2 ] \n");
            Console.Write("\t\t\t\t          ");
            string xuanZe = Console.ReadLine();
            switch (xuanZe)
            {
                case "1":
                one://0空气,1墙壁,2箱位,3箱子,4玩家,5箱位上的箱子,6箱位上的玩家
                    Map = new int[7, 10] {
                    { 0,1,1,1,1,1,1,1,0,0},
                    { 0,1,0,0,0,0,0,1,1,1},
                    { 1,1,3,1,1,1,0,0,0,1},
                    { 1,0,4,0,3,0,0,3,0,1},
                    { 1,0,2,2,1,0,3,0,1,1},
                    { 1,1,2,2,1,0,0,0,1,0},
                    { 0,1,1,1,1,1,1,1,1,0}};

                    Isnumber = 4;
                    XLength = Map.GetLength(0);
                    YLength = Map.GetLength(1);
                    _4x = 3;
                    _4y = 2;
                    break;
                case "2":
                    Map = new int[8, 10] {
                    { 0,0,0,1,1,1,1,1,1,1},
                    { 0,0,1,1,0,0,1,0,4,1},
                    { 0,0,1,0,0,0,1,0,0,1},
                    { 0,0,1,3,0,3,0,3,0,1},
                    { 0,0,1,0,3,1,1,0,0,1},
                    { 1,1,1,0,3,0,1,0,1,1},
                    { 1,2,2,2,2,2,0,0,1,0},
                    { 1,1,1,1,1,1,1,1,1,0}};

                    Isnumber = 5;
                    XLength = Map.GetLength(0);
                    YLength = Map.GetLength(1);
                    _4x = 1;
                    _4y =
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 C# 推箱子游戏的代码示例,仅供参考: ```csharp using System; using System.Collections.Generic; namespace PushBoxGame { class Program { static void Main(string[] args) { Console.WriteLine("Welcome to Push Box Game!"); // 初始化地图和玩家位置 char[,] map = new char[,] { { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }, { '#', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', '#' }, { '#', ' ', ' ', ' ', ' ', '#', ' ', 'X', ' ', '#' }, { '#', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', '#' }, { '#', '#', '#', '#', ' ', '#', ' ', ' ', ' ', '#' }, { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' }, { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' }, { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' }, { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' }, { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } }; int playerX = 4; int playerY = 5; while (true) { // 显示地图 DisplayMap(map, playerX, playerY); // 获取玩家输入 Console.Write("Enter your move (WASD): "); ConsoleKeyInfo key = Console.ReadKey(); Console.WriteLine(); // 根据玩家输入移动玩家 int dx = 0; int dy = 0; switch (key.Key) { case ConsoleKey.W: dy = -1; break; case ConsoleKey.A: dx = -1; break; case ConsoleKey.S: dy = 1; break; case ConsoleKey.D: dx = 1; break; } int newPlayerX = playerX + dx; int newPlayerY = playerY + dy; // 检查新的玩家位置是否合法 if (IsPositionValid(map, newPlayerX, newPlayerY)) { // 如果新的玩家位置是空地,则直接移动玩家 if (map[newPlayerY, newPlayerX] == ' ') { playerX = newPlayerX; playerY = newPlayerY; } // 如果新的玩家位置是箱子,则需要检查箱子是否可以移动 else if (map[newPlayerY, newPlayerX] == 'X') { int newBoxX = newPlayerX + dx; int newBoxY = newPlayerY + dy; if (IsPositionValid(map, newBoxX, newBoxY) && map[newBoxY, newBoxX] == ' ') { map[newPlayerY, newPlayerX] = ' '; map[newBoxY, newBoxX] = 'X'; playerX = newPlayerX; playerY = newPlayerY; } } } // 检查是否胜利 if (IsGameWon(map)) { Console.WriteLine("Congratulations, you won!"); break; } } } // 显示地图和玩家位置 static void DisplayMap(char[,] map, int playerX, int playerY) { for (int y = 0; y < map.GetLength(0); y++) { for (int x = 0; x < map.GetLength(1); x++) { if (x == playerX && y == playerY) { Console.Write('@'); } else { Console.Write(map[y, x]); } } Console.WriteLine(); } } // 检查位置是否合法 static bool IsPositionValid(char[,] map, int x, int y) { if (x < 0 || x >= map.GetLength(1) || y < 0 || y >= map.GetLength(0)) { return false; } return map[y, x] != '#'; } // 检查是否胜利 static bool IsGameWon(char[,] map) { for (int y = 0; y < map.GetLength(0); y++) { for (int x = 0; x < map.GetLength(1); x++) { if (map[y, x] == 'X') { return false; } } } return true; } } } ``` 在这个示例中,我们使用一个二维字符数组来表示地图,其中 `' '` 表示空地,`'#'` 表示墙,`'X'` 表示箱子。玩家的位置用 `playerX` 和 `playerY` 来表示。每次循环中,我们首先显示地图和玩家位置,然后获取玩家输入,并根据输入移动玩家。如果新的玩家位置是空地,则直接移动玩家。如果新的玩家位置是箱子,则需要检查箱子是否可以移动。移动箱子后,我们需要更新地图和玩家位置,然后检查是否胜利。如果所有箱子都被推到目标位置,则游戏胜利。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值