-1

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()
    }
}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
user3371097
  • 1
  • 1
  • 1
  • 1
  • 2
    http://stackoverflow.com/questions/4184468/sleep-for-milliseconds – 001 Mar 02 '14 at 16:23
  • Search for [`usleep()`](http://linux.die.net/man/3/usleep) or [`nanosleep()`](http://linux.die.net/man/2/nanosleep) (the second one is preferred). – MBlanc Mar 02 '14 at 16:24
  • Try usleep. see `man usleep` for more details. – elyashiv Mar 02 '14 at 16:24
  • 2
    If goolging for "khbit" is unsusseccful, try googlign for "kbhit". – n. m. could be an AI Mar 02 '14 at 16:25
  • This could have been answered for yourself by reading the documentation provided for your implementation of the standard library or by using google [`sleep function c`](https://www.google.com/search?q=sleep+function+c). It is a classic RTFM. If your documentation is even remotely good it would include a see-also entry for the finer grained functions. – dmckee --- ex-moderator kitten Mar 02 '14 at 17:08

2 Answers2

3

To perform the desired delay, #include <unistd.h> and use usleep(microseconds). (To sleep for 200ms, the call is usleep(200000)).

To test the keyboard strike, #include <conio.h> and use _kbhit() in your test (short for keyboard hit). _kbhit tests if there is a key in the key buffer, but does not get rid of it. You also need to use _getch to retrieve the key, removing it from the key buffer. I'd recommend defining a helper function here:

int clearKeyBuffer(){
    int count = 0;
    while(_kbhit()){
        _getch();
        count++;
    }
    return count;
}

This method will clear all keys currently in the key buffer, and return the number of keys cleared. You can then use this in your test, as if(clearKeyBuffer()) to test if a key has been presses since the last time you tested it.

As for your program flow, you have a lot of extra stuff there. You can get rid of most of it and still be functionally identical:

do {
    notDeadAnimation();
    usleep(200000);
} while(clearKeyBuffer());

deadAnimation();

However, this has the obvious issue that someone could just

AJMansfield
  • 4,039
  • 3
  • 29
  • 50
2

Use usleep() instead of sleep(). The former works in micro seconds. And use _kbhit()+getch() to discover if a key was pressed and which key was it:

while (!dead) {
    usleep(200*1000); // 200 msec

    if (_kbhit()) { // if key was pressed during sleep
        int key = getch();
        // you can check key value here
        notDeadAnimation();
    } else {
        dead = true;
        deadAnimation();
    }
}
Alexey Polonsky
  • 1,141
  • 11
  • 12