gpt4 book ai didi

c# - 为什么我的游戏中蛇的中间部分有时会消失?

转载 作者:太空宇宙 更新时间:2023-11-03 22:32:09 25 4
gpt4 key购买 nike

只有当我试图在蛇向左移动后向上或向下移动时,它才会发生。如果我在它向右移动后向上或向下移动它,则不会发生这种情况。

IMG1

我创建了一个点类型列表

List<Point> snakeBody = new List<Point>();
for(int i = 0 ; i<5 ; i++)
{
body.Add(new Point(40 , i +25));
}

然后我把它打印到网格上

foreach(Point point in snakeBody)
{
Console.SetCursorPosition(point.X , point.Y);
Console.Write((char)2);
}

这是蛇移动的代码

do
{
Point last = snakeBody[snakeBody.Count - 1];
Console.SetCursorPosition(last.X , last.Y);
snakeBody.RemoveAt(snakeBody.Count - 1);
Console.WriteLine(" ");
Point nextHead = body[0];
if(initialInput == ConsoleKey.UpArrow)
{
nextHead = new Point(nextHead.X , nextHead.Y-1);
Console.SetCursorPosition(nextHead.X , nextHead.Y);
Console.Write("0");
}
else if(initialInput == ConsoleKey.DownArrow)
{
nextHead = new Point(nextHead.X, nextHead.Y+1);
Console.SetCursorPosition(nextHead.X , nextHead.Y);
Console.Write("0");
}
else if(initialInput == ConsoleKey.LeftArrow)
{
nextHead = new Point(nextHead.X -1 , nextHead.Y);
Console.SetCursorPosition(nextHead.X , nextHead.Y);
Console.Write("0");
}
else if(initialInput == ConsoleKey.RightArrow)
{
nextHead = new Point(nextHead.X+1 , nextHead.Y);
Console.SetCursorPosition(nextHead.X , nextHead.Y);
Console.Write("0");
}

snakeBody.Insert(0,nextHead);

if(Console.KeyAvailable)
{
initialInput = Console.ReadKey().Key;
}

}while(gameIsStillRunning);

最佳答案

您正在控制台中看到用户输入。例如。您的击键(左箭头、右箭头)会导致插入符号移动,因此有时您会得到空白。

你可以改变

      initialInput = Console.ReadKey().Key;

      initialInput = Console.ReadKey(true).Key;

这会拦截用户输入并且不会显示它

关于c# - 为什么我的游戏中蛇的中间部分有时会消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56993322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com