如何在C#中运行两个Thread.sleep

时间:2013-12-23 16:45:04

标签: c# loops

我正在编写一个简单的基于控制台的游戏。在游戏中,我有两个功能应该移动两件事 - 一个球拍和一个球。 但是当我使用两个具有Thread.sleep的循环时,游戏不起作用。如何使用Thread.sleep的两个循环并使其正常运行?

我的代码:

class Program
{
    static void Ball()
    {

    }

    static void Main(string[] args)
    {
        int x = 10; int y = 10;
        int dx = 1; int dy = 1;
        ConsoleKeyInfo Exit = new ConsoleKeyInfo();
        do
        {
            Console.SetCursorPosition(x, y);
            Console.WriteLine(" ");
            x += dx;
            y += dy;
            Console.SetCursorPosition(x, y);
            Console.Write("*");
            Thread.sleep(95);
            if (x > 77) dx = -dx;
            if (x < 2) dx = -dx;
            if (y > 22) dy = -dy;
            if (y < 2) dy = -dy;
        } while (true);

        int a = 2;
        int b = 23;
        int da = 1;

        Console.SetCursorPosition(a, b);
        Console.Write("          ");
        if (Console.KeyAvailable)
        {
            ConsoleKeyInfo k = Console.ReadKey(true);
            if (k.Key == ConsoleKey.LeftArrow) a = a - da;
            else if (k.Key == ConsoleKey.RightArrow) a = a + da;
            if (a > 78) a = -da;
            if (a < 2) a = -da;
            Thread.Sleep(200);
        }
        Console.SetCursorPosition(a, b);
        Console.Write("~~~~~~~~~~");
    }
}

1 个答案:

答案 0 :(得分:0)

当我运行您提供的示例代码时,应用程序将运行。也就是说,球(*)将在屏幕周围移动。您是否期望球在屏幕上移动,并且在按下箭头时要划动并移动球拍?如果是这样,你应该看看你是如何循环的。我看到的问题是你有一个绘制球的循环,你的循环永远不会结束,因此你的代码不会绘制球拍和/或反映你的箭头键变化。您是否期望Thread.Sleep暂停循环并允许您绘制/移动球拍?

更新:

namespace ConsoleGame
{    
class Ball {

    private static Ball _instance;

    private int _a = 10, _b = 10;
    private int _dx = 1, _dy = 1;

    private int _timer = 0;
    private int _milliseconds = 1000;

    private Ball() { }

    public static Ball Instance {
        get {
            if(_instance == null) {
                    _instance = new Ball();
            }
            return _instance;
        }
    }

    /// <summary>
    /// Move the ball on screen
    /// </summary>
    /// <param name="speed">The refresh/draw speed of the screen</param>
    public void Move(int speed) {

        if (_timer >= _milliseconds) {
            // Set the cursor position to the current location of the ball
            Console.SetCursorPosition(_a, _b);
            // Clear the ball from the screen
            Console.WriteLine(" ");
            _a += _dx;
            _b += _dy;
            // Set a new locatio for the ball
            Console.SetCursorPosition(_a, _b);
            // Draw the new ball location on screen
            Console.Write("*");
            if (_a > 77) _dx = -_dx;
            if (_a < 2) _dx = -_dx;
            if (_b > 22) _dy = -_dy;
            if (_b < 2) _dy = -_dy;
        } else {
            _timer = _timer + speed;
        }
    }
}

class Paddle {

    private int _x = 2, _y = 23, _da = 1;

    public int x {
        get {

            if (_x > (Console.BufferWidth - "~~~~~~~~~~".Length)) x = -_da;
            if (_x < 2) x = -_da;
            if (_x < 0) x = 2;
            return _x; 
        }
        set { _x = value; }
    }

    private static Paddle _instance;

    private Paddle() { }

    public static Paddle Instance {
        get {
            if (_instance == null) {
                _instance = new Paddle();
            }
            return _instance;
        }
    }

    /// <summary>
    /// Move the Paddle on screen
    /// </summary>
    /// <param name="direction">Direction to move the paddle (Left = -1, Right = 1, Do Not Move = 0)</param>
    public void Move(int direction) {
        Console.SetCursorPosition(x, _y);
        Console.Write("          ");
        x = x - direction;
        Console.SetCursorPosition(x, _y);
        Console.Write("~~~~~~~~~~");
    }
}

class Program {

    private static int PaddleDirection = 0;

    static void Main(string[] args) {

        Thread ConsoleKeyListener = new Thread(new ThreadStart(ListerKeyBoardEvent));
        ConsoleKeyListener.Name = "KeyListener";
        ConsoleKeyListener.Start();
        //ConsoleKeyListener.IsBackground = true;

        int speed = 50;

        do {

            Console.Clear();

            Ball.Instance.Move(speed);
            Paddle.Instance.Move(PaddleDirection);

            PaddleDirection = 0; // You can remove this line to make the paddle loop left/right on the screen after key press.

            Thread.Sleep(speed);
        } while (true);
    }

    private static void ListerKeyBoardEvent() {
        do {
            ConsoleKeyInfo k = Console.ReadKey(true);
            if (k.Key == ConsoleKey.LeftArrow) PaddleDirection = 1;
            else if (k.Key == ConsoleKey.RightArrow) PaddleDirection = -1;
            else PaddleDirection = 0;
            Console.ReadKey(false);
        } while (true);           

    }

}
}