gpt4 book ai didi

c# - C#控制台蛇在长按时卡住

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

我几个月前才刚刚开始学习编程,然后决定做一个主机蛇游戏。一切都对一件事情充满期待。

如果我的蛇在向上移动,而我按下箭头并保持按下状态,那么我的蛇会停止并且即使在我停止按下按钮后仍保持停止状态。

如果我的蛇向右走,而我按向右箭头的时间太长,也会失去控制一段时间(但蛇不会停止),也会发生同样的事情。它发生在每个方向(左,右,上,下)。

我试图将cki与另一个ConsoleKeyInfo进行比较,但两者之间略有延迟,但这并不重要。如果我按住键,我的程序将停留在该位置并更新键。 (至少我认为这就是问题所在)

这是Console.ReadKey的“功能”,还是有某种方法可以解决此问题?

请记住,我刚刚开始,所以我还不了解高级知识。

只要我按住按键的时间不超过1秒钟,一切都会正常进行。

   public void LiikutaMato() //movesnake
{

if (Console.KeyAvailable)
{
ConsoleKeyInfo cki;
cki = Console.ReadKey(true); // <-- I believe this is where it gets stuck

}

最佳答案

玩这个...它使用紧密循环来消耗按键,直到设置的延迟时间到期为止。您也不必按住键来保持蛇的运动。它对我的系统非常敏感:


class Program
{

public enum Direction
{
Up,
Down,
Right,
Left
}

static void Main(string[] args)
{
const int delay = 75;
string snake = "O";
char border = 'X';

int x, y;
int length;
bool crashed = false;
Direction curDirection = Direction.Up;
Dictionary<string, bool> eaten = new Dictionary<string, bool>();
Console.CursorVisible = false;

ConsoleKeyInfo cki;
bool quit = false;
while (!quit)
{
Console.Clear();
Console.Title = "Use 'a', 's', 'd' and 'w' to steer. Hit 'q' to quit.";

// draw border around the console:
string row = new String(border, Console.WindowWidth);
Console.SetCursorPosition(0, 0);
Console.Write(row);
Console.SetCursorPosition(0, Console.WindowHeight - 2);
Console.Write(row);
for (int borderY = 0; borderY < Console.WindowHeight - 2; borderY++)
{
Console.SetCursorPosition(0, borderY);
Console.Write(border.ToString());
Console.SetCursorPosition(Console.WindowWidth - 1, borderY);
Console.Write(border.ToString());
}

// reset all game variables:
length = 1;
crashed = false;
curDirection = Direction.Up;
eaten.Clear();
x = Console.WindowWidth / 2;
y = Console.WindowHeight / 2;
eaten.Add(x.ToString("00") + y.ToString("00"), true);

// draw new snake:
Console.SetCursorPosition(x, y);
Console.Write(snake);

// wait for initial keypress:
while (!Console.KeyAvailable)
{
System.Threading.Thread.Sleep(10);
}

// see if intitial direction should be changed:
cki = Console.ReadKey(true);
switch (cki.KeyChar)
{
case 'w':
curDirection = Direction.Up;
break;

case 's':
curDirection = Direction.Down;
break;

case 'a':
curDirection = Direction.Left;
break;

case 'd':
curDirection = Direction.Right;
break;

case 'q':
quit = true;
break;
}

// main game loop:
DateTime nextCheck = DateTime.Now.AddMilliseconds(delay);
while (!quit && !crashed)
{
Console.Title = "Length: " + length.ToString();

// consume keystrokes and change current direction until the delay has expired:
// *The snake won't actually move until after the delay!
while (nextCheck > DateTime.Now)
{
if (Console.KeyAvailable)
{
cki = Console.ReadKey(true);
switch (cki.KeyChar)
{
case 'w':
curDirection = Direction.Up;
break;

case 's':
curDirection = Direction.Down;
break;

case 'a':
curDirection = Direction.Left;
break;

case 'd':
curDirection = Direction.Right;
break;

case 'q':
quit = true;
break;
}
}
}

// if the user didn't quit, attempt to move without hitting the border:
if (!quit)
{
string key = "";
switch (curDirection)
{
case Direction.Up:
if (y > 1)
{
y--;
}
else
{
crashed = true;
}
break;

case Direction.Down:
if (y < Console.WindowHeight - 3)
{
y++;
}
else
{
crashed = true;
}
break;

case Direction.Left:
if (x > 1)
{
x--;
}
else
{
crashed = true;
}

break;

case Direction.Right:
if (x < Console.WindowWidth - 2)
{
x++;
}
else
{
crashed = true;
}
break;
}

// if the user didn't hit the border, see if they hit the snake
if (!crashed)
{
key = x.ToString("00") + y.ToString("00");
if (!eaten.ContainsKey(key))
{
length++;
eaten.Add(key, true);
Console.SetCursorPosition(x, y);
Console.Write(snake);
}
else
{
crashed = true;
}
}

// set the next delay:
nextCheck = DateTime.Now.AddMilliseconds(delay);
}
} // end main game loop

if (crashed)
{
Console.Title = "*** Crashed! *** Length: " + length.ToString() + " Hit 'q' to quit, or 'r' to retry!";

// wait for quit or retry:
bool retry = false;
while (!quit && !retry)
{
if (Console.KeyAvailable)
{
cki = Console.ReadKey(true);
switch (cki.KeyChar)
{
case 'q':
quit = true;
break;

case 'r':
retry = true;
break;
}
}
}
}

} // end main program loop

} // end Main()

}

关于c# - C#控制台蛇在长按时卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16905124/

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