我正在尝试创建一个控制台应用程序,让用户控制屏幕中单词的动画。基本上我会显示这个词,然后它会根据用户按下的键开始移动。它几乎可以工作,但出于某种原因,我无法弄清楚,用户必须按左箭头键 2 或 3 次,以便单词向左移动,其他键上箭头、右箭头和下箭头也是如此。用户应该只按一次键,单词就会朝那个方向移动。
我知道我仍然必须处理屏幕结束的异常(indexoutofrange),但这将是字母。首先,我想让控件正常工作。
谢谢您的帮助
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);
}
}
}