C#迷宫游戏查询

时间:2013-10-30 01:37:07

标签: c# maze

所以我在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);

            }
        }
    }
}

2 个答案:

答案 0 :(得分: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
}

...如果我继续前进,我已经为你编写了你的​​程序,但是这应该可以帮助你。

答案 1 :(得分: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. }