Sign in to follow this  
Elof Valantor

Thread.Sleep and reading inputs

Recommended Posts

I've started a quick little project just for fun on the side of my studies and so far I've been able to overcome any issues I've faced. But this one is starting to bug me. I'm making a kind of space invaders type game in the console window (crazy I know). It's going okay so far. I've got the scrolling across the screen working, got the inputs working, got sound working, am almost ready to start work on the bullets and collisions and things. Now I'm using Thread.Sleep to control frame rates. In my main program loop I check for user inputs. If any keys are pressed whilst the program is "sleeping" they do NOT get discarded. Instead they all get processed once the program wakes up. This is causing the players ship to go the wrong way or to appear stuck or whatever, making the game annoying and un-responsive unless you're lighting fast and only tap the keys for a milli-second. I would like to know how I can discard any inputs that occur during the sleep period, or how to only allow the first of those inputs to run and discard the rest (probably preferable as it gives more time to listening for inputs). I tried a do-while loop using Console.KeyAvailable == true to try and catch all the excess inputs in a fast loop that would negate them all but it just broke the program (not to mention it wouldn't work if the player just held a key down). Also I've encountered a delay of a second or so in the program registering inputs and acting on them. Is KeyAvailable slow in picking up key presses? Might there be a better way to tell if the keyboard is in use? Thanks.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this