C# 为什么行ConsoleKey=Console.ReadKey().key删除最早的;O";在控制台屏幕上?
试着做蛇的游戏。我使用Console.SetCursorPosition(intx,inty)绘制身体部位(“O”),使用2D数组存储“O”以前的位置,使用Console.key检测方向变化。当按下开关语句中不包含的键时,绘图将停止一次迭代。当按下switch语句中的一个键时,Console将删除最旧的车身部位 调试器未显示原因。有人能帮我理解为什么会这样吗C# 为什么行ConsoleKey=Console.ReadKey().key删除最早的;O";在控制台屏幕上?,c#,console,console.readkey,C#,Console,Console.readkey,试着做蛇的游戏。我使用Console.SetCursorPosition(intx,inty)绘制身体部位(“O”),使用2D数组存储“O”以前的位置,使用Console.key检测方向变化。当按下开关语句中不包含的键时,绘图将停止一次迭代。当按下switch语句中的一个键时,Console将删除最旧的车身部位 调试器未显示原因。有人能帮我理解为什么会这样吗 static void Main(string[] args) { Console.CursorVisible = false;
static void Main(string[] args)
{
Console.CursorVisible = false;
while (true)
{
ConsoleKey key = Console.ReadKey().Key;
switch (key)
{
case ConsoleKey.RightArrow:
direction = "RIGHT";
break;
case ConsoleKey.LeftArrow:
direction = "LEFT";
break;
case ConsoleKey.DownArrow:
direction = "DOWN";
break;
case ConsoleKey.UpArrow:
direction = "UP";
break;
}
breakOut = false;
switch (direction)
{
case "RIGHT":
for (int i = x; i <= 100; i++)
{
Iterator(i, y);
x = i;
if (breakOut)
break;
};
break;
case "LEFT":
break;
case "DOWN":
for (int i = y; i <= 100; i++)
{
Iterator(x, i);
y = i;
if (breakOut)
break;
}
break;
case "UP":
break;
}
}
}
static void Iterator(int x, int y)
{
Draw(x, y);
xSlot = x;
ySlot = y;
histPoints[slotCounter, (int)place.x] = xSlot;
histPoints[slotCounter, (int)place.y] = ySlot;
if (slotCounter >= length)
{
Erase(histPoints[slotCounter - length, (int)place.x], histPoints[slotCounter - length, (int)place.y]);
}
slotCounter += 1;
if (Console.KeyAvailable)
{
breakOut = true;
return;
}
Sleep();
}
static void Main(字符串[]args)
{
Console.CursorVisible=false;
while(true)
{
ConsoleKey=Console.ReadKey().key;
开关(钥匙)
{
case ConsoleKey.RightArrow:
direction=“RIGHT”;
打破
case ConsoleKey.LeftArrow:
方向=“左”;
打破
case ConsoleKey.DownArrow:
方向=“向下”;
打破
case ConsoleKey.UpArrow:
方向=“向上”;
打破
}
突破=错误;
开关(方向)
{
案例“权利”:
对于(int i=x;如果看不到完整的代码,我很难判断。顺便说一下,double switch语句是多余的,您可以将它放在一个语句中。@Innat3缺少的部分只是由Console.SetCursorPosition()和Console.Write()组成。噢,Sleep()是Thread.Sleep(500)。为方向设置默认值