【问题标题】:C# Reading and placing a character at a specific locationC#读取字符并将其放置在特定位置
【发布时间】:2023-03-22 20:38:01
【问题描述】:

首先,提前感谢您的帮助。 我是一名学生,我刚刚开始使用 C# 编程,所以请原谅我让我的代码如此混乱。

出于练习和获得乐趣的预期目的,我正在尝试制作一个 ascii 游戏(自上而下,就像旧的 zelda 游戏,但非常简单)。我已经设法制作了一个移动系统('w' 在控制台中向上移动光标,'a' 向左移动等等)地图也将在控制台中完全绘制。

现在我遇到的问题和接下来的问题:每当我移动光标时,它实际上会将与我按下的按钮相对应的字符放置在地图的平铺位置(例如:我按下“w”。光标首先放置一个“w”,它会替换地图图块,然后向上移动)。我的解决方案是首先复制最初位于磁贴中的字符。之后,我移动光标。最后,我将复制的字符放回它所属的位置,而不是程序之前放在那里的“w”、“a”、“s”或“d”)。另一种解决方案是首先确保光标永远不会替换地图的 ascii 字符。问题是:我将如何实施这些解决方案中的任何一个?

我包含的代码显示了我的地图构建例程(当前仅填充了“█”)和移动系统。

class Program
{   
    static void Main(string[] args)
    {
        Console.WindowWidth = 128; //The map will be 128x32
        Console.WindowHeight = 32;
        LoadMap();
        Console.SetCursorPosition(10, 10); //the cursor will be set at x = 10 and y = 10

        while (true) //a simple loop to check for user input
        {
            ConsoleKeyInfo input = Console.ReadKey();
            Console.Write("\b");
            PosX = Console.CursorLeft;
            PosY = Console.CursorTop;
            switch (input.KeyChar)
            {
                case 'w':
                Console.SetCursorPosition(PosX + 0, PosY - 1);
                    break;
                case 'a':
                Console.SetCursorPosition(PosX - 1, PosY + 0);
                    break;
                case 's':
                Console.SetCursorPosition(PosX + 0, PosY + 1);
                    break;
                case 'd':
                Console.SetCursorPosition(PosX + 1, PosY + 0);
                    break;
            }
        }
    }

    public static void PathWay(int PathSize) //pathsize = amount of █ placed in a row.
    {
        int n = 0;
        Console.ForegroundColor = ConsoleColor.Gray;
        while (n < PathSize)
        {
            n = n + 1;
            Console.Write("█");
        }
    }

    public static void LoadMap() //This will eventually call to many subroutines to create a map (a subroutine for creating a tree for example)
    {
        PathSize = 128;
        int n;
        n = 0;
        while(n < 32)
        {
            n = n + 1;
            PathWay(PathSize);
        }
    }

    public static int PathSize;
    public static int PosX;
    public static int PosY;
    public static string test;
}   

当然我可以让你只能在一种类型的角色上行走,但如果你能在不止一种类型的角色上行走,游戏会更有趣。

再次感谢您!

【问题讨论】:

  • 使用ConsoleKeyInfo input = Console.ReadKey(true); 防止正在输入的字符显示在控制台窗口中。
  • 通常在这些场景中,您有一组字符组成地图,并且您绘制的字符(通常是@)来代替其中一个地图字符..
  • @Tau 这立即解决了问题,非常感谢!
  • @BugFinder 将光标设置为“@”会很棒。我该怎么做呢?
  • 你不会,你只是把@画成一个字符

标签: c# console ascii console-application


【解决方案1】:

你应该只使用这句话来读取带有 true 参数的控制台输入键

// Start a console read operation. Do not display the input.
ConsoleKeyInfo input = Console.ReadKey(true);

【讨论】:

    【解决方案2】:

    使用ReadKey(true) 代替ReadKey()

    ConsoleKeyInfo input = Console.ReadKey(true);
    //Console.Write ("\b");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      相关资源
      最近更新 更多