在蛇游戏中取代Thread.Sleep的东西

时间:2019-07-18 06:37:50

标签: c#

我正在做蛇游戏作为我的作业,我已经添加了一个代码,用于根据用户输入(左,右,下,上)移动蛇头。 但是我坚持使用它的时机,我使用Thread.Sleep来防止游戏崩溃并获得异常,但是我的教练告诉我Thread.Sleep在编程中是一个可怕的想法,因为它确实增加了延迟。您的游戏。 因此,我需要以某种方式确保避免Thread.Sleep的同时不会延迟我的游戏

class Program
    {
        static void Main(string[] args)
        {
            Direction direction = Direction.Down;

            Console.CursorVisible = false;
            int x=0 , y=0 ;
            int xprev = 2, yprev = 2;
            char shape = 'o';
            x = xprev;
            y = yprev;
            Console.SetCursorPosition(xprev, yprev);
            Console.Write(shape);

            UserInput input = new UserInput();
            ConsoleKeyInfo? info;

            while (true)
            {

                info = input.GetKey();
                if (info.HasValue)
                {

                    switch (info.Value.Key)
                    {
                        case ConsoleKey.RightArrow:
                            direction = Direction.Right;
                            break;

                        case ConsoleKey.LeftArrow:
                            direction = Direction.Left;
                            break;

                        case ConsoleKey.UpArrow:
                            direction = Direction.Up;
                            break;

                        case ConsoleKey.DownArrow:
                            direction = Direction.Down;
                            break;
                    }

                }
                Thread.Sleep(100);

                switch (direction)
                {
                    case Direction.Up:
                        y--;

                        break;
                    case Direction.Down:
                        y++;

                        break;
                    case Direction.Left:
                        x--;


                        break;
                    case Direction.Right:
                        x++;

                        break;

                }

                Console.MoveBufferArea(xprev, yprev, 1, 1, x, y);
                xprev = x;
                yprev = y;
            }

1 个答案:

答案 0 :(得分:1)

正如Sohaib Jundi所建议的那样,此处的某种timer是一个合理的解决方案。您的目标是:

  

每100毫秒,更新蛇所在的位置

与其说“ 使应用程序暂停100ms,然后更新蛇”,要么使用计时器将其更改为“ 具有每100ms触发一次更新蛇的东西”来解决此问题。 >。

例如:

using System;
using System.Threading;

namespace Snake
{
    class Program
    {
        static void Main(string[] args)
        {
            var snakeTimer = new Timer(updateTheSnake, null, 0, 100);
            while(true)
            {
                var keypress = Console.ReadKey();
                if (keypress.Key == ConsoleKey.Escape)
                {
                    break;
                }
            }
        }

        static void updateTheSnake(object state)
        {
            Console.Write("@");
        }
    }
}

这是一个非常简单的示例,仅在屏幕上画一条@,直到用户按下退出键。在您的代码中,您可能希望将Thread.Sleep以下的所有内容移至updateTheSnake方法中。 direction可能需要存储为共享状态,以便您可以从updateTheSnake方法中引用它。