• C++测手速小游戏


            终于有空写点代码了(作业好多awa)

            今天带来的是一款非常有趣的测手速游戏,玩家可以测试自己的反应速度。

    创作背景:

            MCYH的反应速度及其迟钝。

            为了锻炼自己,MCYH制作了一款测手速的小游戏,以达到提高反应速度的目的。

    游戏玩法:

            cmd框一开始为绿色,4-9秒钟后会变成红色,这时玩家需要按下空格键,游戏会记录你的反应时间。反应时间反映着你的反应速度。

           

    低配版代码:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. #define INF 0x3f3f
    6. int main()
    7. {
    8. system("cls");
    9. srand(time(0));
    10. system("color 2F");
    11. printf("when it turns red, press 'space'\n\n\n");
    12. Sleep(rand()%5000+4000);
    13. system("color 4F");
    14. int j=clock(),k;
    15. char s=_getch();
    16. printf("%d ms",clock()-j);
    17. Sleep(1000);
    18. return 0;
    19. }

    _____________________________________________________________________________

            emmmmm...是不是感觉少点什么?

            对啦!我们可以多测几次,取平均值,这样也可以避免偶然性,减少误差。

          

    中配版代码:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. #define INF 0x3f3f
    6. int res=0,a[6]={INF,INF,INF,INF,INF,INF};
    7. int c_a=INF;
    8. int main()
    9. {
    10. system("cls");
    11. for(int i=1;i<=5;i++)
    12. {
    13. system("cls");
    14. srand(time(0));
    15. system("color 2F");
    16. printf("when it turns red, press 'space'\n\n\n");
    17. Sleep(rand()%5000+4000);
    18. bool cont=0;
    19. while(kbhit())//防止玩家“抢跑 ”
    20. {
    21. printf("You pressed too soon.");
    22. char s=getch();
    23. Sleep(1000);
    24. cont=1;
    25. }
    26. if (cont==0)
    27. {
    28. system("color 4F");
    29. int j=clock(),k;
    30. char s=_getch();
    31. printf("%d ms",clock()-j);
    32. a[i]=clock()-j;
    33. res+=a[i];
    34. Sleep(1000);
    35. }
    36. else
    37. --i;//本次作废,重新来过
    38. }
    39. system("cls");
    40. res/=5;
    41. printf("Your average:%dms\n",res);
    42. for(int i=1;i<=5;i++)
    43. {
    44. printf("%d : %d ms\n",i,a[i]);
    45. c_a=min(a[i],c_a);
    46. }
    47. printf("You can press any key to quit the Game after 2 seconds.\n");
    48. Sleep(2000);
    49. _getch();
    50. return 0;
    51. }

    _____________________________________________________________________________

            emmmmm...要是能存档就更好了。

    高配版代码:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. #define INF 0x3f3f
    6. int res=0,a[6]={INF,INF,INF,INF,INF,INF};
    7. int best_res=INF,best_a=INF;
    8. int c_a=INF;
    9. void Read_File();//读档
    10. void Write_File();//存档
    11. int main()
    12. {
    13. Read_File();
    14. for(int i=1;i<=5;i++)
    15. {
    16. system("cls");
    17. srand(time(0));
    18. system("color 2F");
    19. printf("when it turns red, press 'space'\n\n\n");
    20. Sleep(rand()%5000+4000);
    21. bool cont=0;
    22. while(kbhit())
    23. {
    24. printf("You pressed too soon.");
    25. char s=getch();
    26. Sleep(1000);
    27. cont=1;
    28. }
    29. if (cont==0)
    30. {
    31. system("color 4F");
    32. int j=clock(),k;
    33. char s=_getch();
    34. printf("%d ms",clock()-j);
    35. a[i]=clock()-j;
    36. res+=a[i];
    37. Sleep(1000);
    38. }
    39. else
    40. --i;
    41. }
    42. system("cls");
    43. res/=5;
    44. printf("Your average:%dms\n",res);
    45. for(int i=1;i<=5;i++)
    46. {
    47. printf("%d : %d ms\n",i,a[i]);
    48. c_a=min(a[i],c_a);
    49. }
    50. printf("Your best average: %dms\n",best_res);
    51. printf("Your best score: %dms\n",best_a);
    52. if (res
    53. printf("New average record!\n"),best_res=res;
    54. if (c_a
    55. printf("New score record!\n"),best_a=c_a;
    56. Write_File();
    57. printf("You can press any key to quit the Game after 2 seconds.\n");
    58. Sleep(2000);
    59. _getch();
    60. return 0;
    61. }
    62. void Read_File()
    63. {
    64. freopen("save.txt","r",stdin);
    65. scanf("%d%d",&best_res,&best_a);
    66. fclose(stdin);
    67. }
    68. void Write_File()
    69. {
    70. freopen("save.txt","w",stdout);
    71. printf("%d %d",best_res,best_a);
    72. fclose(stdout);
    73. }

    _____________________________________________________________________________

            最后再完善一下场景,把光标屏蔽掉,大功告成!

    上成品:

           

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. #define INF 0x3f3f
    6. int res=0,a[6]={INF,INF,INF,INF,INF,INF};
    7. int best_res=INF,best_a=INF;
    8. int c_a=INF;
    9. void Read_File();//读档
    10. void Write_File();//存档
    11. void hidden();//隐藏光标
    12. int main()
    13. {
    14. Read_File();
    15. printf("Measuring hand speed Game\n");
    16. printf("Author : MCYH \n");
    17. printf("Press any key to join the game.\n\n");
    18. printf("Game Rule:\n");
    19. printf("The screen will be green first. When it turns red, you should press 'space'.\n");
    20. printf("DO NOT press ahead!!!\n");
    21. _getch();
    22. system("cls");
    23. for(int i=1;i<=5;i++)
    24. {
    25. system("cls");
    26. srand(time(0));
    27. system("color 2F");
    28. printf("when it turns red, press 'space'\n\n\n");
    29. Sleep(rand()%5000+4000);
    30. bool cont=0;
    31. while(kbhit())
    32. {
    33. printf("You pressed too soon.");
    34. char s=getch();
    35. Sleep(1000);
    36. cont=1;
    37. }
    38. if (cont==0)
    39. {
    40. system("color 4F");
    41. int j=clock(),k;
    42. char s=_getch();
    43. printf("%d ms",clock()-j);
    44. a[i]=clock()-j;
    45. res+=a[i];
    46. Sleep(1000);
    47. }
    48. else
    49. --i;
    50. }
    51. system("cls");
    52. res/=5;
    53. printf("Your average:%dms\n",res);
    54. for(int i=1;i<=5;i++)
    55. {
    56. printf("%d : %d ms\n",i,a[i]);
    57. c_a=min(a[i],c_a);
    58. }
    59. printf("Your best average: %dms\n",best_res);
    60. printf("Your best score: %dms\n",best_a);
    61. if (res
    62. printf("New average record!\n"),best_res=res;
    63. if (c_a
    64. printf("New score record!\n"),best_a=c_a;
    65. Write_File();
    66. printf("You can press any key to quit the Game after 2 seconds.\n");
    67. Sleep(2000);
    68. _getch();
    69. return 0;
    70. }
    71. void Read_File()
    72. {
    73. freopen("save.txt","r",stdin);
    74. scanf("%d%d",&best_res,&best_a);
    75. fclose(stdin);
    76. }
    77. void Write_File()
    78. {
    79. freopen("save.txt","w",stdout);
    80. printf("%d %d",best_res,best_a);
    81. fclose(stdout);
    82. }
    83. void hidden()
    84. {
    85. HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    86. CONSOLE_CURSOR_INFO CursorInfo;GetConsoleCursorInfo(handle, &CursorInfo);
    87. CursorInfo.bVisible = false;
    88. SetConsoleCursorInfo(handle, &CursorInfo);
    89. }

            好啦,以上就是这篇文章的全部内容,你的手速有多快呢?可以把你的最好成绩打在评论区哟。觉得好玩的可以点一个赞,感谢您的支持。

  • 相关阅读:
    排序-算法
    如何将一个mongodb中集合的索引 添加到另一个mongodb中集合中
    【微信小程序】wxml、wxss、js、json文件介绍
    Android 9.0 如何自定义自己的build.prop
    一个命令就可启用的微信机器人WhoChat
    springcloud-Eureka
    【MySQL】insert和select单表查询详解(包含大量示例,看了必会)
    BGP——BGP基础概念
    测试开发【Mock平台】04实战:前后端项目初始化与登录鉴权实现
    面试被问Redis主从复制不会答?这13张图让你彻底弄明白
  • 原文地址:https://blog.csdn.net/MCYH0206/article/details/126185656