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?

4
  • 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 Commented Sep 23, 2018 at 2: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. Commented Sep 23, 2018 at 2:20
  • @Stewcooker you may be able to try this to clear the buffer before reading. Commented Sep 23, 2018 at 2: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! Commented Sep 23, 2018 at 2:56

1 Answer 1

1
while (true)
{
    YourMethods();
    while (Console.KeyAvailable)
    {
        Console.ReadKey(true);
    }
    var key = Console.ReadKey();
    Console.WriteLine(key);
}
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Ok I did a bit of research (Thanks @John!) and playing around and it does seem to fix my problem! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.