![](/img/trans.png)
[英]Why does my file sometimes disappear in the process of reading from it or writing to it?
[英]Why does the middle part of the snake in my game disappear sometimes?
只有當我向左移動時我試圖向上或向下移動我的蛇時它才會發生。 如果我在向右移動之后向上或向下移動它就不會發生。
我創建了一個Point Type列表
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;
這將攔截用戶輸入並且不會顯示它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.