c语言打字游戏代码

举报
C语言与CPP编程 发表于 2021/06/04 01:02:57 2021/06/04
【摘要】 #include<stdio.h>#include<time.h> char *kw[]={"Q W E R T Y U I O P [ ]","A S D F G H J K L ; '","Z X C V B N M , . / "}; long AllCounter=0,RightCounter=0,WrongCounter=0; // 来自公...

  
  1. #include<stdio.h>
  2. #include<time.h>
  3. char *kw[]={"Q W E R T Y U I O P [ ]","A S D F G H J K L ; '","Z X C V B N M , . / "};
  4. long AllCounter=0,RightCounter=0,WrongCounter=0;
  5. // 来自公众号:c语言与cpp编程
  6. main()
  7. {
  8. int i,j;
  9. int fun_Esc();
  10. clrscr();
  11. gotoxy(18,1);
  12. printf("%s\n",kw[0]);
  13. gotoxy(20,3);
  14. printf("%s\n",kw[1]);
  15. gotoxy(22,5);
  16. printf("%s\n",kw[2]);
  17. gotoxy(11,25);
  18. for(i=0;i<60;i++)
  19. {
  20. printf("=");
  21. }
  22. gotoxy(1,1);
  23. printf("AllCh: %ld\nRight: %ld\nWrong: %ld",AllCounter,RightCounter,WrongCounter);
  24. gotoxy(50,1);
  25. printf("Press Esc to exit");
  26. gotoxy(50,2);
  27. printf("Enter to pause");
  28. gotoxy(26,12);
  29. printf("* * * * * * * * * * * * * * ");
  30. gotoxy(26,13);
  31. printf("* Press any key to start! *");
  32. gotoxy(26,14);
  33. printf("* * * * * * * * * * * * * * ");
  34. gotoxy(51,13);
  35. if(getch()==27)
  36. {
  37. if(fun_Esc()==1)
  38. {
  39. clrscr();
  40. exit(0);
  41. }
  42. }
  43. gotoxy(23,12);
  44. printf(" ");
  45. gotoxy(23,13);
  46. printf(" ");
  47. gotoxy(23,14);
  48. printf(" ");
  49. while(1)
  50. fun_Play();
  51. }
  52. int fun_Play()
  53. {
  54. int x,y,i,j;
  55. unsigned int Timer;
  56. char ch;
  57. char cur;
  58. time_t t;
  59. srand((unsigned)time(&t));
  60. gotoxy(26,12);
  61. printf(" ");
  62. gotoxy(26,13);
  63. printf(" ");
  64. gotoxy(26,14);
  65. printf(" ");
  66. y = 6;
  67. Timer = 100000;
  68. i = rand()%3;
  69. j = rand()%(9-i);
  70. ch = kw[i][j*4];
  71. x = 18+i*2+j*4;
  72. while(y<=24)
  73. {
  74. if(kbhit())
  75. {
  76. cur = getch();
  77. if(cur==ch || cur==ch+32)
  78. {
  79. ch = '*'; Timer = 1000;
  80. }
  81. else if(cur==27)
  82. {
  83. if(fun_Esc()==1)
  84. {
  85. clrscr();
  86. exit(0);
  87. }
  88. }
  89. else if(cur=='\r')
  90. {
  91. gotoxy(x,y-1);
  92. printf(" ");
  93. gotoxy(26,12);
  94. printf("* * * * * * * * * * * * * * *");
  95. gotoxy(26,13);
  96. printf("* Press any key to continue *");
  97. gotoxy(26,14);
  98. printf("* * * * * * * * * * * * * * * ");
  99. getch();
  100. gotoxy(28,13);
  101. printf(" ");
  102. }
  103. else
  104. {
  105. WrongCounter++;
  106. }
  107. }
  108. if(y>6)
  109. {
  110. gotoxy(x,y-1);
  111. printf(" ");
  112. }
  113. gotoxy(x,y);
  114. printf("%c",ch);
  115. gotoxy(1,1);
  116. printf("AllCh: %ld\nRight: %ld\nWrong: %ld",AllCounter,RightCounter,WrongCounter);
  117. delay(Timer);
  118. y++;
  119. }
  120. AllCounter++;
  121. if(ch == '*')
  122. {
  123. RightCounter++;
  124. }
  125. }
  126. int fun_Esc()
  127. {
  128. int key = '#';
  129. gotoxy(26,12);
  130. printf("* * * * * * * * * * * * * * * * ");
  131. gotoxy(26,13);
  132. printf("* Are you sure to exit? (Y/N) *");
  133. gotoxy(26,14);
  134. printf("* * * * * * * * * * * * * * * * ");
  135. gotoxy(51,13);
  136. while(key!='Y' && key!='y' && key!='N' && key!='n')
  137. {
  138. key = getch();
  139. if(key=='Y' || key=='y')
  140. {
  141. return 1;
  142. }
  143. if(key=='N' || key=='n')
  144. {
  145. gotoxy(24,12);
  146. printf(" ");
  147. gotoxy(24,13);
  148. printf(" ");
  149. gotoxy(24,14);
  150. printf(" ");
  151. return 0;
  152. }
  153. }
  154. }

文章来源: blog.csdn.net,作者:C语言与CPP编程,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_41055260/article/details/112107249

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。