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 ...

  • the error is more likely not in clearing the console, but in the way user input is processed. Show the main cycle of the program without unnecessary details of course - rdorn
  • @rdorn added a question - Mikhail Znak
  • then I would do that. Two variable directions. One for the snake, the second for the keyboard. Before the next movement of the snake, we check whether the new value of the direction is permissible, if yes - change it, if not - ignore - rdorn
  • @rdorn such a check exists inside the direction reversal method. And it works, but the problem is a bit different. suppose it is our direction to the right. if I press to the left, everything will work as expected and nothing will happen, but if I press up / down and immediately to the left, without changing the lines, it will go to the left. I hope explained - Mikhail Znak
  • correctly, or rather not correctly =), the test should not be in the method of changing direction, where the user wants what he wants to do, but in the method of motion of the snake, which is critical of the new direction - rdorn

1 answer 1

Following the dialogue in the comments:

The main active element in this case is the "snake", it is she who knows which direction is permissible for her and which is not. Therefore, the test for admissibility of the new direction, you need to do in the method of movement "snake", and the user can enter anything, we just need to know what was introduced before the next movement. Accordingly, the user’s actions are added to the buffer variable without checks, and before the next movement we check: a new direction is permissible - we change direction and move, it is unacceptable - we ignore and move in the previous direction.


This scheme allows you to implement all possible modifications of the "snake" behavior when trying to change the direction to the opposite, which occurred in the history of this game:

  • reverse, head and tail are interchanged without pause.
  • stop, found in a lightweight version of the original.
  • death, found in hardcore versions, is seen as a collision with a tail.