Stop!
在生活中經常看到那種秒數暫停的遊戲,決定敲個代碼出來
遊戲將提示一個隨機數字範圍,開始計時後,玩家必須快速按下鍵盤停止計時使數字正好處在要求範圍裏。
源代碼:
#include<iostream>
#include<windows.h>
#include<conio.h>
#include<ctime>
using namespace std;
int main(){
char answer = 'a';
while(answer!='0'){
srand(unsigned(time(NULL)));
int des = rand() % 100;
cout<<"您的目標範圍:"<<des<<"~"<<des+5<<endl;
cout<<"是否開始?(按任意鍵開始,開始後按任意鍵停止計時):";
if(getch()){
int count = 0;
while(!kbhit()){
system("cls");
cout<<"您的目標範圍:"<<des<<"~"<<des+5<<endl;
cout<<count;
count++;
}
answer = getch();
count--;
cout<<endl;
if(count>=des && count<=des+5){
cout<<"你贏了!"<<endl;
}
else{
cout<<"你輸了!"<<endl;
}
}
cout<<endl;
cout<<"是否重新開始?(輸入0結束遊戲,其他任意鍵重新開始):";
answer = getch();
cout<<endl;
if(answer != '0')
system("cls");
}
system("pause");
return 0;
}
遊戲界面: