一个测试反应力的小程序
代碼:
#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);
}
總結
以上是生活随笔為你收集整理的一个测试反应力的小程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年高考历史学业水平测试成绩查询,
- 下一篇: 求和 (1)