Ognjen
Ognjen

Reputation: 3

I want to make a snake game in a C# console, movement problem

To change snakes direction I first have to press upArrow and then the new direction of the snake, and the snake is going in that direction until I hit upArrow again. So, to change direction to the left you need to press upArrow and then leftArrow. I want that to disappear, when I press left to make snake go left. Basically up arrow is pausing the game and I dont know why.

            if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.UpArrow)
            {
                keyinfo = Console.ReadKey();
                Console.WriteLine(ConsoleKey.UpArrow);
            }
            if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.DownArrow)
            {
                keyinfo = Console.ReadKey();
                Console.WriteLine(ConsoleKey.DownArrow);
            }
            if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.LeftArrow)
            {
                keyinfo = Console.ReadKey();
                Console.WriteLine(ConsoleKey.LeftArrow);
            }
            if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.RightArrow)
            {
                keyinfo = Console.ReadKey();
                Console.WriteLine(ConsoleKey.RightArrow);
            }
            if (keyinfo.Key == ConsoleKey.UpArrow)
            {
                j--;
            }
            if (keyinfo.Key == ConsoleKey.DownArrow)
            {
                j++;
            }
            if (keyinfo.Key == ConsoleKey.LeftArrow)
            {
                k--;
            }
            if (keyinfo.Key == ConsoleKey.RightArrow)
            {
                k++;
            }

Upvotes: -2

Views: 201

Answers (2)

Ognjen
Ognjen

Reputation: 3

            if (Console.KeyAvailable)
            {
                keyinfo = Console.ReadKey(true);
                Console.WriteLine(keyinfo);

            }
            if (keyinfo.Key == ConsoleKey.UpArrow)
            {
                j--;
            }
            if (keyinfo.Key == ConsoleKey.DownArrow)
            {
                j++;
            }
            if (keyinfo.Key == ConsoleKey.LeftArrow)
            {
                k--;
            }
            if (keyinfo.Key == ConsoleKey.RightArrow)
            {
                k++;
            }

Upvotes: 0

Chris Dunaway
Chris Dunaway

Reputation: 11216

You're calling ReadKey too many times. You should call it once and then store the result in a variable:

     if (Console.KeyAvailable)
     {
          keyinfo = Console.ReadKey(true);
          Console.WriteLine(keyinfo);

          if (keyinfo.Key == ConsoleKey.UpArrow)
          {
              j--;
          }
          if (keyinfo.Key == ConsoleKey.DownArrow)
          {
              j++;
          }
          if (keyinfo.Key == ConsoleKey.LeftArrow)
          {
              k--;
          }
          if (keyinfo.Key == ConsoleKey.RightArrow)
          {
              k++;
          }
     }

Upvotes: 1

Related Questions