代码:
直接上代码:
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
int main() {
const int ESCKEY = 27; //esc键的ASCII值
int pos, posmax = 72;
int win = 0, loss = 0; //正确次数和错误次数
cout << "===================欢迎来到打字游戏!====================\n 作者:刘峻鹏" << endl;
Sleep(3000);//系统休眠3秒
system("cls"); //清屏
cout<<"游戏规则: \n"<<"1、游戏开始系统随机生成一个字符,请你键入同样的字符来进入下一字母\n"<<"2、游戏时按esc键结束游戏,游戏结束时会输出你的正确个数和练习个数,加油吧!\n"<<"(5秒后开始游戏)\n";
Sleep(5000);//休眠5秒
system("cls");
int ch = 0, key = 0;
cout<<"开始了!" <<endl;
srand(time(0));
system("cls");//再次清屏
Sleep(1000);
while (key != ESCKEY) {
ch = rand() % ('z' + 1 - 'a') + 'a';
for (pos = 0; pos < posmax && key != 27; pos++) {
cout << "\b--" << (char)ch;
pos++;
Sleep(200);
if (kbhit() && (key = getch()) == ch){
win++;
cout << "*\a";//觉得响铃不行的把\a删掉
break;
}
}
if (key == ESCKEY) {
cout << "\n结束游戏吗?(t/f)";
while ((key = tolower(getchar())) != 't' && ch != 'f')
;
key = (key == 't' ? ESCKEY : 0);
}
if (pos >= posmax)
loss ++;
cout << endl;
}
system("cls");
cout << "字符练习个数:" << win + loss << " 正确键入个数:" << win << endl;
cout << "游戏结束,谢谢使用!" << endl;
Sleep(1000);
return 0;
}