public void ChangeDirection(object state) { while (true) { while (Console.KeyAvailable) { Console.ReadKey(true); } ConsoleKeyInfo key = Console.ReadKey(true); lock (state) { switch (key.Key) { // } } } There is a change of direction in the snake.
It works like this: if a snake moves to the right, then it can change directions in any direction, but not to the left (that is, in reverse, the snake will not surrender). Everything works, except for one, but if you press quickly, for example, up and left, then in the same line it will change its direction to the opposite (forbidden to the left).
As it seemed to me a problem in cleaning the console buffer
while (Console.KeyAvailable) { Console.ReadKey(true); } In the open spaces I found such a way to clean it, but apparently it does not work.
Tell me, please, where is the mistake or what am I doing wrong? And how can I get a queue of keystrokes?
I have the only input in this method (the method refers to the class Snake), it is called in the stream
Snake snake = new Snake(); object obj = new object(); Thread keyboard = new Thread(snake.ChangeDirection); // Смена направления.метод, который рассматривается с самого начала keyboard.Start(obj); while (true) { lock (obj) { Thread.Sleep(100); snake.Movement(obj); // перемещение координат ,switch с увелечением X или Y } snake.Draw(); } PS On another PC, everything works fine ...