C语言 使用 `sleep()` 延迟时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22130122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:50:41  来源:igfitidea点击:

Using `sleep()` for a time delay

cdelaysleepflappy-bird-clone

提问by user3371097

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?

我试图将程序执行延迟 200 毫秒,然后测试在延迟期间是否按下了某个键。我该怎么做呢?

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.

我正在尝试使用 C 创建一个类似于飞扬的小鸟的简单电脑游戏。我希望用户有一点时间(~200 毫秒)按下一个键让小鸟跳跃,否则它会掉下来,但我是无法实现延迟。

I've read on some forums [where?]that sleep(100)should give a 100ms delay, but when I do it, I get 100 seconds.

我读过一些论坛[在哪里?]sleep(100)应该给一个延时100ms,但是当我这样做,我得到100秒。

I also tried using sleep(1/5), but the function only takes integers.

我也尝试使用sleep(1/5),但该函数只接受整数。

Additionally, I need to be able to test if a key was pressed during the 200ms; I read somewhere[where?]that the kbhitfunction can be used for that, but I have no idea how to use it.

此外,我需要能够测试在 200 毫秒内是否按下了某个键;我在某处[where?]读到 该kbhit函数可以用于此目的,但我不知道如何使用它。

while(!dead) {
    sleep(200); // what do I put here to get 200ms?

    if (keyWasPressedDuringWait()){ //what do I put here?
        notDeadAnimation():
    }else{
        dead=true;
        deadAimation()
    }
}

回答by AJMansfield

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

要执行所需的延迟,#include <unistd.h>并使用usleep(microseconds). (要休眠 200 毫秒,调用是usleep(200000))。

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

为了测试键盘的罢工,#include <conio.h>并使用_kbhit()在您的测试(简称ķEY bOARD命中)。_kbhit测试密钥缓冲区中是否有密钥,但不会删除它。您还需要使用_getch来检索密钥,将其从密钥缓冲区中删除。我建议在这里定义一个辅助函数:

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.

此方法将清除当前在密钥缓冲区中的所有密钥,并返回清除的密钥数量。然后,您可以在测试中使用它,if(clearKeyBuffer())以测试自上次测试以来是否按下了某个键。

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

然而,这有一个明显的问题,即有人可以

回答by Alexey Polonsky

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:

使用 usleep() 而不是 sleep()。前者在微秒内起作用。并使用 _kbhit()+getch() 来发现是否按下了某个键以及它是哪个键:

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();
    }
}