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);
}
}
}
Try this
Instead of this