带计时器的小游戏c++
时间: 2023-11-27 12:46:09 浏览: 42
以下是一个带计时器的小游戏C++的例子:
```c++
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int time = 60; // 设定游戏时间为60秒
int score = 0; // 初始化得分为0分
cout << "Welcome to the game! You have 60 seconds to get as many points as possible." << endl;
cout << "Press any key to start the game." << endl;
system("pause"); // 暂停等待用户按下任意键开始游戏
while (time > 0) // 当游戏时间大于0时,继续游戏
{
system("cls"); // 清屏
cout << "Time left: " << time << "s" << endl;
cout << "Score: " << score << endl;
// 在屏幕上随机生成一个数字,让用户输入这个数字
int num = rand() % 10;
cout << "Please enter the number " << num << ": ";
int input;
cin >> input;
if (input == num) // 如果用户输入的数字和随机生成的数字相同,得分加1
{
score++;
cout << "Correct! You get 1 point." << endl;
}
else // 如果用户输入的数字和随机生成的数字不同,得分不变
{
cout << "Wrong! You get 0 point." << endl;
}
Sleep(1000); // 等待1秒钟
time--; // 游戏时间减1秒
}
system("cls"); // 清屏
cout << "Time's up! Your final score is " << score << "." << endl;
return 0;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)