I am writing a 2d, turn-based adventure game in C# that runs in windows console. Here is a rough idea of what my game loop looks like:
while(condition) {
MethodCall1();
MethodCall2();
MethodCall3();
GetPlayerInput(Console.ReadKey());
}
The game runs in a while loop, with the end of the loop being Console.ReadKey(). The idea is for the game to print out all relevant info, run enemy AI and other calculations, and then wait for user input before doing it all over again. I am running into a problem, however. It takes a fair amount of time for all of the code to run (printing the map to the console is the main culprit, taking around 150 ms to print with colors), and during this time the console is still reading user input, even though it seems like it should wait to read the input until all code and printing is done. If any key is held down, it loops through the code for as many keypresses as it detected, even if I release the key.
So I suppose I have two questions: Why is the console reading the input even though it is still executing code, and what is a good way to stop this from occuring?
while (true)
{
YourMethods();
while (Console.KeyAvailable)
{
Console.ReadKey(true);
}
var key = Console.ReadKey();
Console.WriteLine(key);
}