Daniel
Daniel

Reputation: 197

C# - Problems with the animations, using readkey() method

I'm trying to creating an Console app that let the user control the animation of a word in the screen. Basically I show the word and then it starts moving according with the keys pressed by the user. Its almost working but for some reasoning i cant figure out, the user would have to press 2 or 3 times the key leftArrow so the word goes to the left, and the same is happening for the other keys upArrow, RightArrow and downArrow. The user should be pressing the key only one time and word would move in that direction.

I know I still have to deal with the exceptions for the end of the screen(indexoutofrange) but that will be done letter. First I want to make the controls work.

Thanks for the help

using System;
using System.Threading;

namespace Annimation
{
    class Program
    {
        static void Main(string[] args)
        {
            Boolean endOfCanvas = false;
            int x = 20, y = 25;
            ConsoleKeyInfo dir = new ConsoleKeyInfo();
            String word = "@@@@@@@@@@@";

            Console.WriteLine(word);

            do
            {
                do
                {
                    dir = Console.ReadKey(true);
                    while (Console.KeyAvailable == false)
                    {
                        if (dir.Key == ConsoleKey.DownArrow)
                        {
                            System.Console.Clear();
                            Console.SetCursorPosition(x, y);
                            Console.WriteLine(word);
                            Thread.Sleep(100);
                            Console.WriteLine("down");
                            y++;
                        }
                        else if (dir.Key == ConsoleKey.UpArrow)
                        {
                            System.Console.Clear();
                            Console.SetCursorPosition(x, y);
                            Console.WriteLine(word);
                            Thread.Sleep(100);
                            Console.WriteLine("up");
                            y--;
                        }
                        else if (dir.Key == ConsoleKey.LeftArrow)
                        {
                            System.Console.Clear();
                            Console.SetCursorPosition(x, y);
                            Console.WriteLine(word);
                            Thread.Sleep(100);
                            Console.WriteLine("Left");
                            x--;
                        }
                        else if (dir.Key == ConsoleKey.RightArrow)
                        {
                            System.Console.Clear();
                            Console.SetCursorPosition(x, y);
                            Console.WriteLine(word);
                            Thread.Sleep(100);
                            Console.WriteLine("Right");
                            x++;
                        }

                    }
                } while (Console.ReadKey(true).Key == ConsoleKey.DownArrow ||
                                Console.ReadKey(true).Key == ConsoleKey.UpArrow ||
                                Console.ReadKey(true).Key == ConsoleKey.RightArrow ||
                                Console.ReadKey(true).Key == ConsoleKey.LeftArrow);
            } while (!endOfCanvas);
        } 
    }
}

Upvotes: 0

Views: 64

Answers (2)

giokoguashvili
giokoguashvili

Reputation: 2103

Try this

...
do {
    ...
} while (dir.Key == ConsoleKey.DownArrow ||
         dir.Key == ConsoleKey.UpArrow ||
         dir.Key == ConsoleKey.RightArrow ||
         dir.Key == ConsoleKey.LeftArrow);

Instead of this

...
do {
    ...
} while (Console.ReadKey(true).Key == ConsoleKey.DownArrow ||
         Console.ReadKey(true).Key == ConsoleKey.UpArrow ||
         Console.ReadKey(true).Key == ConsoleKey.RightArrow ||
         Console.ReadKey(true).Key == ConsoleKey.LeftArrow);

Upvotes: 0

MKR
MKR

Reputation: 20095

Seems problem is in your below while-loop.

while ((Console.ReadKey(true).Key == ConsoleKey.DownArrow ||
                                Console.ReadKey(true).Key == ConsoleKey.UpArrow ||
                                Console.ReadKey(true).Key == ConsoleKey.RightArrow ||
                                Console.ReadKey(true).Key == ConsoleKey.LeftArrow);

Let the loop continue and take decision to break based on dir = Console.ReadKey(true);. If entered key is not one expected then you can break.

You can get feel of your error by replacing above while condition like while(true) then run your program.

Upvotes: 0

Related Questions