I an trying to delay program execution for 200ms and then test if a key was pressed during the delay. How do I do this?
I am tryint to create a simple computer game similar to flappy birds, using C. I want the user to have tiny bit of time (~200ms) to press a key for the bird to jump, or it will fall down, but I am having trouble with implementing the delay.
I've read on some forums [where?] that sleep(100)
should give a 100ms delay, but when I do it, I get 100 seconds.
I also tried using sleep(1/5)
, but the function only takes integers.
Additionally, I need to be able to test if a key was pressed during the 200ms; I read somewhere[where?] that the kbhit
function can be used for that, but I have no idea how to use it.
while(!dead) {
sleep(200); // what do I put here to get 200ms?
if (keyWasPressedDuringWait()){ //what do I put here?
notDeadAnimation():
}else{
dead=true;
deadAimation()
}
}