You're probably confused because Console.ReadKey()
is blocking...so your game loop would be stuck waiting for the user to press a key right? What you do is only grab the keystroke with ReadKey() if there is already one in the queue. This can be checked with Console.KeyAvailable
which returns true if a key is already there. This way the game loop just keeps looping around if no key has been pressed...but you can still trap it and do something with it. Try this quick example out and see what happens:
class Program
{
static void Main(string[] args)
{
while (true)
{
System.Threading.Thread.Sleep(250);
Console.Write(".");
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey(true); // read key without displaying it
Console.WriteLine("");
Console.WriteLine("Key Pressed: " + key.KeyChar.ToString());
}
}
}
}