900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > C语言打字练习小游戏

C语言打字练习小游戏

时间:2022-04-09 03:09:50

相关推荐

C语言打字练习小游戏

C语言打字练习小游戏

#include <stdio.h>#include <conio.h>#include <time.h>#include <stdlib.h>#include <windows.h>int getTimeDetail(int index){time_t timeNow;struct tm *timeType;int year, month, day, hour, min, sec;/*Get time from local system.*/time(&timeNow);//get time return secondstimeType = localtime(&timeNow); //into structyear = timeType->tm_year + 1900;month = timeType->tm_mon;day = timeType->tm_mday;hour = timeType->tm_hour;min = timeType->tm_min;sec = timeType->tm_sec;//add time to second (ignored date)int time[6] = {year, month, day, hour, min, sec}; //store in arrayreturn time[index];}int getTime(){return (int)time(NULL);}void goto_xy(int x, int y){HANDLE hOut;hOut = GetStdHandle(STD_OUTPUT_HANDLE);COORD pos = {x, y};SetConsoleCursorPosition(hOut, pos);}int main(void){char press;int timeNow = getTime();int timeInitial = timeNow - 1;double rate = 0;int correct = 0;int randomNum;int pressed = 1;system("COLOR F3"); //set color: background-white text-bluewhile (press != 27)//quit when press is the ESC{press = 0; //nullSetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0xF3);// update rate: correct times / game timerate = (double)correct / (double)(timeNow - timeInitial);if (pressed)//update key after pressed{srand((unsigned)time(NULL));//use time as the random seed//randomNum = rand() % 93 + 33; //get a random number from 33~126 (! to ~)randomNum = rand() % 26 + 65;pressed = 0;}if (timeNow != getTime())//update board each second{timeNow = getTime();goto_xy(0, 0);printf("*******KEYBOARD ONLY********\n");printf("** Heal typing disability **\n");printf("****************************\n");printf("************** Author:LMC **\n");printf("******* Created:/10/10 *\n");printf("****************************\n\n");printf("----------------------------\n");printf("\tTime:%d\tRate:%.3f\t\n", timeNow - timeInitial, rate);printf("\tPlease press %c\n", randomNum);printf("----------------------------\n");}if (kbhit())//check press{goto_xy(0, 15);press = getch();if (press == randomNum){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x3A);correct++;}else{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x3C);correct--;}printf("\t┏━┓\n");printf("\t┃%c┃\n", press);printf("\t┗━┛\n");pressed = 1;}}system("COLOR F3");goto_xy(0, 20);printf("Your accuracy rate in %d seconds is %.2f%%!", timeNow - timeInitial, rate * 100.0);system("pause");return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。