0

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?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Stewcooker
  • 103
  • 1
  • 3
  • Just a guess, but the keyboard likely _buffering_ your input. A quick glance at the System.Console class does show any way of turning this off. You might try digging around the docs for the Windows console to see if there's a function you could P/Invoke to do this. The other thing I'd do would be to write a test program (say that does a ReadKey, sleeps for 5 sec, and then loops 5 or 10 times on ReadKey calls and see what happens. Good luck – Flydog57 Sep 23 '18 at 02:17
  • Thanks for the suggestion! It doesn't exactly break the game, its just an annoyance that I don't understand. None of my research turned up anything either, so I figured I would ask here and see what I learned. – Stewcooker Sep 23 '18 at 02:20
  • @Stewcooker you may be able to try [this](https://stackoverflow.com/questions/3769770/clear-console-buffer) to clear the buffer before reading. – ProgrammingLlama Sep 23 '18 at 02:38
  • @John that helped! It still reads input during a couple of "cinematic" sequences I have, where I slowly print lines one after another for effect. But that's probably because I'm using Thread.Sleep(), and that's another question for another day! – Stewcooker Sep 23 '18 at 02:56

1 Answers1

1
while (true)
{
    YourMethods();
    while (Console.KeyAvailable)
    {
        Console.ReadKey(true);
    }
    var key = Console.ReadKey();
    Console.WriteLine(key);
}
Circle Hsiao
  • 1,497
  • 4
  • 22
  • 37
  • This seems to do the opposite of what I am looking for. If no key is being pressed, then the program loops continuously until one is pressed. I would like the program to stop reading input until all code has finished executing. – Stewcooker Sep 23 '18 at 02:40
  • Ok I did a bit of research (Thanks @John!) and playing around and it does seem to fix my problem! Thanks! – Stewcooker Sep 23 '18 at 02:53