Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When I run the example code you provided, the application will run. That is to say, the ball (*) will move around the screen. Are you expecting the ball to move on the screen, and the paddle to be drawn and move when an arrow is pressed? If so, you should look at how you are looping. The issue I see is that you have a loop that draws the ball and your loop will never ends, and therefore your code will not draw the paddle and/or reflect your arrow key changes. Is your expectation also for the Thread.Sleep to pause the loop and allow you to draw/move the paddle?</p> <p>UPDATE:</p> <pre><code>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; } } /// &lt;summary&gt; /// Move the ball on screen /// &lt;/summary&gt; /// &lt;param name="speed"&gt;The refresh/draw speed of the screen&lt;/param&gt; public void Move(int speed) { if (_timer &gt;= _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 &gt; 77) _dx = -_dx; if (_a &lt; 2) _dx = -_dx; if (_b &gt; 22) _dy = -_dy; if (_b &lt; 2) _dy = -_dy; } else { _timer = _timer + speed; } } } class Paddle { private int _x = 2, _y = 23, _da = 1; public int x { get { if (_x &gt; (Console.BufferWidth - "~~~~~~~~~~".Length)) x = -_da; if (_x &lt; 2) x = -_da; if (_x &lt; 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; } } /// &lt;summary&gt; /// Move the Paddle on screen /// &lt;/summary&gt; /// &lt;param name="direction"&gt;Direction to move the paddle (Left = -1, Right = 1, Do Not Move = 0)&lt;/param&gt; 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); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload