终于有空写点代码了(作业好多awa)
今天带来的是一款非常有趣的测手速游戏,玩家可以测试自己的反应速度。
创作背景:
MCYH的反应速度及其迟钝。
为了锻炼自己,MCYH制作了一款测手速的小游戏,以达到提高反应速度的目的。
游戏玩法:
cmd框一开始为绿色,4-9秒钟后会变成红色,这时玩家需要按下空格键,游戏会记录你的反应时间。反应时间反映着你的反应速度。
低配版代码:
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
#define INF 0x3f3f
int main()
{system("cls");srand(time(0));system("color 2F");printf("when it turns red, press 'space'\n\n\n");Sleep(rand()%5000+4000);system("color 4F");int j=clock(),k;char s=_getch();printf("%d ms",clock()-j);Sleep(1000);return 0;
}
_____________________________________________________________________________
emmmmm...是不是感觉少点什么?
对啦!我们可以多测几次,取平均值,这样也可以避免偶然性,减少误差。
中配版代码:
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
#define INF 0x3f3f
int res=0,a[6]={INF,INF,INF,INF,INF,INF};
int c_a=INF;
int main()
{system("cls");for(int i=1;i<=5;i++){system("cls");srand(time(0));system("color 2F");printf("when it turns red, press 'space'\n\n\n");Sleep(rand()%5000+4000);bool cont=0;while(kbhit())//防止玩家“抢跑 ” {printf("You pressed too soon.");char s=getch();Sleep(1000);cont=1;}if (cont==0){system("color 4F");int j=clock(),k;char s=_getch();printf("%d ms",clock()-j);a[i]=clock()-j;res+=a[i];Sleep(1000);}else--i;//本次作废,重新来过 }system("cls");res/=5;printf("Your average:%dms\n",res);for(int i=1;i<=5;i++){printf("%d : %d ms\n",i,a[i]);c_a=min(a[i],c_a);}printf("You can press any key to quit the Game after 2 seconds.\n");Sleep(2000);_getch();return 0;
}
_____________________________________________________________________________
emmmmm...要是能存档就更好了。
高配版代码:
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
#define INF 0x3f3f
int res=0,a[6]={INF,INF,INF,INF,INF,INF};
int best_res=INF,best_a=INF;
int c_a=INF;
void Read_File();//读档
void Write_File();//存档
int main()
{Read_File();for(int i=1;i<=5;i++){system("cls");srand(time(0));system("color 2F");printf("when it turns red, press 'space'\n\n\n");Sleep(rand()%5000+4000);bool cont=0;while(kbhit()){printf("You pressed too soon.");char s=getch();Sleep(1000);cont=1;}if (cont==0){system("color 4F");int j=clock(),k;char s=_getch();printf("%d ms",clock()-j);a[i]=clock()-j;res+=a[i];Sleep(1000);}else--i;}system("cls");res/=5;printf("Your average:%dms\n",res);for(int i=1;i<=5;i++){printf("%d : %d ms\n",i,a[i]);c_a=min(a[i],c_a);}printf("Your best average: %dms\n",best_res);printf("Your best score: %dms\n",best_a);if (res<best_res)printf("New average record!\n"),best_res=res;if (c_a<best_a)printf("New score record!\n"),best_a=c_a;Write_File();printf("You can press any key to quit the Game after 2 seconds.\n");Sleep(2000);_getch();return 0;
}
void Read_File()
{freopen("save.txt","r",stdin);scanf("%d%d",&best_res,&best_a);fclose(stdin);
}
void Write_File()
{freopen("save.txt","w",stdout);printf("%d %d",best_res,best_a);fclose(stdout);
}
_____________________________________________________________________________
最后再完善一下场景,把光标屏蔽掉,大功告成!
上成品:
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
#define INF 0x3f3f
int res=0,a[6]={INF,INF,INF,INF,INF,INF};
int best_res=INF,best_a=INF;
int c_a=INF;void Read_File();//读档
void Write_File();//存档
void hidden();//隐藏光标
int main()
{Read_File();printf("Measuring hand speed Game\n");printf("Author : MCYH \n");printf("Press any key to join the game.\n\n");printf("Game Rule:\n");printf("The screen will be green first. When it turns red, you should press 'space'.\n");printf("DO NOT press ahead!!!\n");_getch();system("cls");for(int i=1;i<=5;i++){system("cls");srand(time(0));system("color 2F");printf("when it turns red, press 'space'\n\n\n");Sleep(rand()%5000+4000);bool cont=0;while(kbhit()){printf("You pressed too soon.");char s=getch();Sleep(1000);cont=1;}if (cont==0){system("color 4F");int j=clock(),k;char s=_getch();printf("%d ms",clock()-j);a[i]=clock()-j;res+=a[i];Sleep(1000);}else--i;}system("cls");res/=5;printf("Your average:%dms\n",res);for(int i=1;i<=5;i++){printf("%d : %d ms\n",i,a[i]);c_a=min(a[i],c_a);}printf("Your best average: %dms\n",best_res);printf("Your best score: %dms\n",best_a);if (res<best_res)printf("New average record!\n"),best_res=res;if (c_a<best_a)printf("New score record!\n"),best_a=c_a;Write_File();printf("You can press any key to quit the Game after 2 seconds.\n");Sleep(2000);_getch();return 0;
}
void Read_File()
{freopen("save.txt","r",stdin);scanf("%d%d",&best_res,&best_a);fclose(stdin);
}
void Write_File()
{freopen("save.txt","w",stdout);printf("%d %d",best_res,best_a);fclose(stdout);
}
void hidden()
{HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO CursorInfo;GetConsoleCursorInfo(handle, &CursorInfo);CursorInfo.bVisible = false;SetConsoleCursorInfo(handle, &CursorInfo);
}
好啦,以上就是这篇文章的全部内容,你的手速有多快呢?可以把你的最好成绩打在评论区哟。觉得好玩的可以点一个赞,感谢您的支持。