c++有记时的小游戏
时间: 2023-11-27 14:46:09 浏览: 49
以下是一个用C++编写的倒计时小游戏的示例代码:
```c++
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
puts("I am Timer. Please tell me your time (seconds): ");
int n;
scanf("%d", &n);
int rsth = n / 3600, rstm = n / 60 % 60, rsts = n % 60;
cout << "The timer will start in 3 seconds." << endl;
Sleep(3000);
while (rsth >= 0)
{
cout << rsth << ":" << rstm << ":" << rsts << endl;
Sleep(1000);
rsts--;
if (rsts < 0)
{
rsts = 59;
rstm--;
if (rstm < 0)
{
rstm = 59;
rsth--;
}
}
}
Beep(1000, 1000);
cout << "Time's up!" << endl;
return 0;
}
```
这个小游戏会提示用户输入倒计时的时间(单位为秒),然后将其转换为时分秒的形式。接着会在3秒后开始倒计时,并且每秒钟输出当前的剩余时间。当时间到达0时,会发出一声响铃并输出“Time's up!”。