0

试图制作蛇游戏。我正在使用 Console.SetCursorPosition(int x, int y) 来绘制身体部位(“O”),使用 2D 数组来存储“O”以前的位置,并使用 Console.KeyAvailable 来检测方向变化。当按下不在 switch 语句中的键时,绘图将停止一次迭代。当按下 switch 语句中的键时,控制台会删除最旧的正文部分。

调试器没有显示原因。谁能帮我理解为什么会这样?

static void Main(string[] args)
{
    Console.CursorVisible = false;

    while (true)
    {
        ConsoleKey key = Console.ReadKey().Key;
        switch (key)
        {
            case ConsoleKey.RightArrow:
                direction = "RIGHT";
                break;
            case ConsoleKey.LeftArrow:
                direction = "LEFT";
                break;
            case ConsoleKey.DownArrow:
                direction = "DOWN";
                break;
            case ConsoleKey.UpArrow:
                direction = "UP";
                break;
        }

        breakOut = false;

        switch (direction)
        {
            case "RIGHT":
                for (int i = x; i <= 100; i++)
                {
                    Iterator(i, y);
                    x = i;
                    if (breakOut)
                        break;
                };
                break;
            case "LEFT":
                break;
            case "DOWN":
                for (int i = y; i <= 100; i++)
                {
                    Iterator(x, i);
                    y = i;
                    if (breakOut)
                        break;
                }
                break;
            case "UP":
                break;
        }
    }
}

static void Iterator(int x, int y)
{
    Draw(x, y);

    xSlot = x;
    ySlot = y;

    histPoints[slotCounter, (int)place.x] = xSlot;
    histPoints[slotCounter, (int)place.y] = ySlot;

    if (slotCounter >= length)
    {
        Erase(histPoints[slotCounter - length, (int)place.x], histPoints[slotCounter - length, (int)place.y]);
    }

    slotCounter += 1;

    if (Console.KeyAvailable)
    {
        breakOut = true;
        return;
    }

    Sleep();
}
4

0 回答 0