用户计时器在第一次使用后出错,无论我等待多长时间都会返回零
User timer bugs out after first use and returns zero no matter how long I wait
我试图创建一个测试用户反应速度的游戏,在游戏的第一部分,我使用计时器来随机选择用户何时按下。在那之后,我想计算用户按下所需的时间,但无论我等待按下多长时间,它总是返回"0秒后你按下了1",1是我按下的键。如何使它的时间用户也可取毫秒而不是秒。我对C++真的很陌生,并试图在书籍的帮助下自学,但我真的陷入了困境。有什么帮助吗?
#include <iostream
#include <time.h>
using namespace std;
class timer {
private:
unsigned long startTime;
public:
void start() {
startTime = clock();
}
unsigned long elapsedTime() {
unsigned long t = (clock() - startTime) / CLOCKS_PER_SEC;
return t;
}
bool isTimeout(unsigned long seconds) {
return seconds >= elapsedTime();
}
int main() {
srand(time(NULL));
int keyPressed = (0);
unsigned long secondsBeforeStart = (rand() % 11);
timer tBefore;
timer tAfter;
bool waitingInput = false;
bool gameIsFinished = false;
tBefore.start();
cout << "timer started . . ." << endl;
while(true)
{
if(tBefore.elapsedTime() >= secondsBeforeStart && !waitingInput && !gameIsFinished) //waiting the game to draw "Tryck nu!"
{
waitingInput = true;
cout << "Tryck nu!" << endl;
tAfter.start();
}
if(waitingInput && !gameIsFinished) // if "Tryck nu" has appeared => waiting the player to press a key
{
// cout << "DEBUG : tAfter == " << tAfter.elapsedTime(); << endl;
cin >> keyPressed;
if( keyPressed != 0) // if player press the key
{
cout << "you have pressed " << keyPressed << " after " << tAfter.elapsedTime() << " seconds" << endl;
gameIsFinished = true;
}
}
if(gameIsFinished)
{
cout << "Well done, you can push a button to restart the game" << endl;
cin >> keyPressed;
if( keyPressed != 0) // if player press the key
{
cout << "Restarting" << endl;
waitingInput = false;
gameIsFinished = false;
secondsBeforeStart = (rand() % 11);
tBefore.start();
cout << "timer started . . ." << endl;
}
}
}
}
问题可能是您的elapsedTime((函数:
unsigned long elapsedTime() {
unsigned long t = (clock() - startTime) / CLOCKS_PER_SEC;
return t;
}
如果计数少于CLOCKS_PER_SEC
刻度(这是非常可能的(,除法将返回0,因为操作数是整数类型。
试着先显示时钟滴答声,然后再担心将时间转换为秒。
clock()
告诉您进程使用的CPU节拍数,但您的进程在输入时被阻止时不会占用任何CPU时间!所以这对这类问题没用。
您应该使用一个不同的函数,它可以为您提供墙时间(即真实世界时间的绝对流逝(,特别是如果您想要毫秒分辨率的话。考虑gettimeofday()
。
#include <sys/time.h>
class timer
{
private:
double startTime;
/**
* This function wraps the call to `gettimeofday()`,
* so that you may simply use it instead of `clock()`.
*
* You will still have to remove your `CLOCKS_PER_SEC`
* logic and switch from `unsigned long` to `double`,
* though.
*/
double getTime()
{
timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec + tv.tv_usec/1e9;
}
public:
void start()
{
startTime = getTime();
}
double elapsedTime()
{
return getTime() - startTime;
}
bool isTimeout(double seconds) {
return seconds >= elapsedTime();
}
};
(课堂现场演示(
在实际应用程序中,我实际上会将clock_gettime
与CLOCK_MONOTONIC
一起使用,以避免本地时间跳跃的影响。不过,在这个简单的学习案例中,这可能太多了。
相关文章:
- 使用Boost Interprocess创建托管共享内存需要很长时间
- SFML RenderWindow打开窗口需要很长时间
- Kafka C++客户端需要很长时间才能收到消息
- 给定使用 C++ 或 C,我如何测量在 linux 下进行线程切换需要多长时间?可能吗?
- asio::read() 需要很长时间,使用 asio::write 没有问题
- 将线程锁定很长时间
- 正在等待在非阻塞文件描述符上长时间运行ioctl
- 即使长时间等待,C++线程也不会加入
- 连接() 在连接被拒绝时长时间挂起
- 为什么这段代码需要这么长时间才能用 g++ 编译?
- 如何在长时间运行的方法中等待信号?
- mbed 套接字连接需要很长时间
- Lambda 捕获此函数和长时间运行的函数
- std::fstream 需要很长时间才能将大数据写入.csv文件中
- 为什么与Java和Python相比,使用Cmake运行C++程序每次都需要这么长时间?
- 需要很长时间
- 在关闭应用程序期间正确关闭线程,该线程可能会运行很长时间的循环
- 如何在长时间计算中进行C 的一次进度更新
- 如何在英特尔TBB中拥有长时间等待的线程
- 用户计时器在第一次使用后出错,无论我等待多长时间都会返回零