0

所以我在 Visual Studio 中创建一个迷宫游戏,用 C# 编码。我走得很好,但我一直在弄清楚如何使迷宫的墙壁变得坚固。当前,当您到达迷宫中的墙壁时,位置标记会直接穿过它们并删除蓝色墙壁。我目前正试图弄清楚如何制作你无法穿过的墙壁。我尝试了各种陈述,还尝试弄乱 unicode,但似乎无处可去。如果有人有任何提示,那就太好了!谢谢。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Project4
{
class Program
{
    static void Main(string[] args)
    {
        Console.SetWindowSize(10, 10);
        Console.BackgroundColor = ConsoleColor.White;
        Console.ForegroundColor = ConsoleColor.DarkBlue;

        //Make maze
        Console.WriteLine("");
        Console.WriteLine(" \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588 ");
        Console.WriteLine(" \u2588\u0020\u0020\u0020\u0020\u0020\u0020\u2588 ");
        Console.WriteLine(" \u2588\u0020\u2588\u2588\u2588\u2588\u0020\u2588 ");
        Console.WriteLine(" \u2588\u0020\u2588\u0020\u0020\u0020\u0020\u2588 ");
        Console.WriteLine(" \u2588\u0020\u2588\u0020\u2588\u2588\u2588\u2588 ");
        Console.WriteLine(" \u2588\u0020\u2588\u0020\u0020\u0020\u0020\u2588 ");
        Console.WriteLine(" \u2588\u0020\u2588\u2588\u2588\u2588\u0020\u2588 ");
        Console.WriteLine(" \u2588\u0020\u0020\u0020\u0020\u0020\u0020\u2588 ");
        Console.WriteLine(" \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588 ");
        Console.Write("");

        int col = 7;
        int row = 6;
        Console.SetCursorPosition(col, row);
        Console.Write("*");
        Console.SetCursorPosition(col, row);
        while (true)
        {
            ConsoleKeyInfo info = Console.ReadKey(true);
            if (info.Key == ConsoleKey.W)
            {
                Console.Write(" ");
                Debug.Print("W");
                row--;
            }
            if (info.Key == ConsoleKey.Z)
            {
                Console.Write(" ");
                Debug.Print("Z");
                row++;
            }

            if (info.Key == ConsoleKey.A)
            {
                Console.Write(" ");
                Debug.Print("A");
                col--;
            }

            if (info.Key == ConsoleKey.S)
            {
                Console.Write(" ");
                Debug.Print("S");
                col++;
            }

            Console.SetCursorPosition(col, row);
            Console.Write("*");
            Console.SetCursorPosition(col, row);

            }
        }
    }
}
4

2 回答 2

0

使墙壁“坚固”基本上意味着禁止移动,这会使您的光标位置与墙壁重叠。考虑以下解决方案:

class Program
{
    private const char wallchar = '\u2588';
    private const char mazechar = '\u0020';

    static private bool[][] mazeLayout = 
    {
        new[] {true, true, true, true, true, true, true, true},
        new[] {true, true, true, true, true, true, true, true},
        new[] {true, false, false, false, false, false, false, true},
        new[] {true, false, true, true, true, true, false, true},
        new[] {true, false, true, false, false, false, false, true},
        new[] {true, false, true, false, true, true, true, true},
        new[] {true, false, true, false, false, false, false, true},
        new[] {true, false, true, true, true, true, false, true},
        new[] {true, false, false, false, false, false, false, true},
        new[] {true, true, true, true, true, true, true, true}
    };

    static void Main(string[] args)
    {
        Console.SetWindowSize(10, 10);
        Console.BackgroundColor = ConsoleColor.White;
        Console.ForegroundColor = ConsoleColor.DarkBlue;

        Console.WriteLine("");

        //Make maze
        for (int i = 0; i < mazeLayout.Length; i++)
        {
            for (int j = 0; j < mazeLayout[i].Length; j++)
            {
                Console.Write(mazeLayout[i][j] ? wallchar : mazechar);
            }
            Console.Write("\n");
        }

        Console.WriteLine("");
        int col = 3;
        int row = 6;

        Console.SetCursorPosition(col, row);
        Console.Write("*");
        Console.SetCursorPosition(col, row);
        while (true)
        {
            ConsoleKeyInfo info = Console.ReadKey(true);
            if (info.Key == ConsoleKey.W && !mazeLayout[row - 2][col])
            {
                Console.Write(" ");
                Debug.Print("W");
                row--;
            }
            if (info.Key == ConsoleKey.Z && !mazeLayout[row][col])
            {
                Console.Write(" ");
                Debug.Print("Z");
                row++;
            }

            if (info.Key == ConsoleKey.A && !mazeLayout[row-1][col-1])
            {
                Console.Write(" ");
                Debug.Print("A");
                col--;
            }

            if (info.Key == ConsoleKey.S && !mazeLayout[row-1][col+1])
            {
                Console.Write(" ");
                Debug.Print("S");
                col++;
            }

            Console.SetCursorPosition(col, row);
            Console.Write("*");
            Console.SetCursorPosition(col, row);
        }
    }
}

该行:

if (info.Key == ConsoleKey.S && !mazeLayout[row-1][col+1])
{ ... }

方法:

if (the user pressed S) AND (the move would not move us onto a wall)
then... 
    { go ahead and do the move. }
于 2013-10-30T02:22:22.043 回答
0

我建议制作一个数组来保存您的地图,并编写一个方法来渲染您的游戏:

void Main()
{
    var map=new[]
    {
        new[]{true, true, true, true, true, true, true, true}, 
        new[]{true, false, false, false, false, false, false, true}, 
        new[]{true, false, true, true, true, true, false, true}, 
        new[]{true, false, true, false, false, false, false, true}, 
        new[]{true, false, true, false, true, true, true, true}, 
        new[]{true, false, true, false, false, false, false, true}, 
        new[]{true, false, true, true, true, true, false, true}, 
        new[]{true, false, false, false, false, false, false, true}, 
        new[]{true, true, true, true, true, true, true, true}
    };
    var playerX = 1;
    var playerY = 1;
    RenderMap(map, playerX, playerY);

}

void RenderMap(bool[][] map, int playerX, int playerY)
{

    for(var y = 0; y < map.Length; y++)
    {
        var row = map[y];
        for(var x = 0; x < row.Length; x++)
        {
            var tile = row[x];
            if(x == playerX && y == playerY)
            {
                Console.Write("X");
            }
            else
            {
                Console.Write(tile? "\u2588": " ");
            }
        }
        Console.WriteLine();
    }
}

现在您可以执行以下操作:

if(map[playerY][playerX-1])
{
    //there is a wall to the left of the player's current position
}

...如果我更进一步,我已经为你编写了你的​​程序,但这应该会让你上路。

于 2013-10-30T02:06:17.907 回答