1.设计过程
之前在设计打字游戏项目设计之前曾在CSDN和百度上找了许多,但是最后运行都有问题,最后通过小组分配函数功能设计了一个简单的打字游戏程序
2.核心算法
①读取字符函数getch()
②退出游戏函数fun_Esc()
③实现游戏开始函数fun-Play()
④程序暂停函数delay()
⑤检查键盘输入函数kbhit()
⑥随机产生数字函数srand()
⑦随机产生数函数rand()
具体不多做赘述,源代码里面都有对应的注释。
3.运行过程
4.话不多说上代码
#include<stdio.h> //标明头文件
#include<time.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>
void delay(int sec)
{
time_t start_time, cur_time;//声明变量
time(&start_time);
do
{
time(&cur_time);
}
while ((cur_time - start_time) < sec);
}
void gotoxy(int x, int y)
{
COORD c;
c.X = x - 1;
c.Y = y - 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
const 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;
void main()
{
int i = 0, j = 0; //声明函数
int fun_Esc(); //声明退出游戏函数
int getch(); //读取字符函数
int fun_Play(); //声明调用游戏函数
void delay(); //运行的程序暂停函数
int kbhit(); //检查是否有键盘输入函数
void srand(); //随机产生数字函数
int rand(); //随机产生数函数
system("cls"); //清屏功能
gotoxy(18, 1); //定位光标
printf("%s\n", kw[0]); //输出字母
gotoxy(20, 3); //输出游戏窗口下界
printf("%s\n", kw[1]);
gotoxy(22, 5);
printf("%s\n", kw[2]);
gotoxy(11, 25);
for (i = 0; i < 60; i++)
{
printf("=");
} //输出游戏窗口下界
gotoxy(1, 1);
printf("AllCh: %ld\nRight: %ld\nWrong: %ld", AllCounter, RightCounter, WrongCounter); //输出游戏数据
gotoxy(50, 1);
printf("Press Esc to exit"); //输出游戏提示信息
gotoxy(50, 2);
printf("Enter to pause");
gotoxy(26, 12);
printf("* * * * * * * * * * * * * * ");
gotoxy(26, 13);
printf("* Press any key to start! *");
gotoxy(26, 14);
printf("* * * * * * * * * * * * * * ");
gotoxy(51, 13);
if (getch() == 27) //ESC对应的ASCII码是27
{
if (fun_Esc() == 1) //推出调用判断函数
{
system("cls"); //清屏
exit(0); //退出程序
}
}
gotoxy(23, 12);
printf(" ");
gotoxy(23, 13);
printf(" ");
gotoxy(23, 14);
printf(" ");
while (1)
{
fun_Play();
}
} //主函数截止
//开始设计游戏界面
int fun_Play() //调用游戏函数
{
int x, y, i, j; //定义整形变量
unsigned int Timer; //无符号变量
char ch; //字符型变量
char cur;
int fun_Esc();
time_t t; //time_t实际上是long型,从一个时间点(一般是1970年1月1日0时0分0秒)到当前的秒数
srand((unsigned)time(&t)); //随机生成一个正数
gotoxy(26, 12);//gotoxy函数用于屏幕输出,功能是将光标移动到屏幕指定位置。在屏幕左上角被定义为光标的原点(0,0)
printf(" ");
gotoxy(26, 13);
printf(" ");
gotoxy(26, 14);
printf(" ");
y = 6; //对y进行赋值
Timer = 100; //对Timer进行赋值
i = rand() % 3; //i等于一个从0到2的随机数
j = rand() % (9 - i); //j等于一个从0到(9-i)的随机数
ch = kw[i][j * 4]; //定义一个二维字符串数组
x = 18 + i * 2 + j * 4;
while (y <= 24) //while循环里面有两个大if语句
{
if (kbhit()) //函数名:kbhit()功能及返回值,检查当前是否有键盘输入,若有则返回一个非0值,否则返回0。(第一个大if语句)
{
cur = getch();
if (cur == ch || cur == ch + 32)//巧妙运用if语句
{
ch = '*'; Timer = 100;
}
else if (cur == 27)
{
if (fun_Esc() == 1)
{
system("cls");
exit(0);
}
}
else if (cur == '\r')
{
gotoxy(x, y - 1);
printf(" ");
gotoxy(26, 12);
printf("* * * * * * * * * * * * * * *");
gotoxy(26, 13);
printf("* Press any key to continue *");
gotoxy(26, 14);
printf("* * * * * * * * * * * * * * * ");
getch();
gotoxy(28, 13);
printf(" ");
}
else
{
WrongCounter++;
}
}
if (y > 6) //第二个大if语句
{
gotoxy(x, y - 1);
printf(" ");
}
gotoxy(x, y);
printf("%c", ch);
gotoxy(1, 1);
printf("AllCh: %ld\nRight: %ld\nWrong: %ld", AllCounter, RightCounter, WrongCounter);
Sleep(Timer);
y++; //while循环语句结束
}
AllCounter++;
if (ch == '*')
{
RightCounter++;
}
return 0;
}
int fun_Esc() //退出游戏函数
{
int key = '#';
gotoxy(26, 12); //游戏光标(下同)
printf("* * * * * * * * * * * * * * *");
gotoxy(26, 13);
printf(" Are you sure to exit? (Y/N) "); //输出 “是否退出游戏”
gotoxy(26, 14);
printf("* * * * * * * * * * * * * * *");
gotoxy(51, 13);
while (key != 'Y' && key != 'y' && key != 'N' && key != 'n') //确定是否退出游戏
{
key = getch(); //输入一个字母直接读取
if (key == 'Y' || key == 'y')
{
return 1;
}
if (key == 'N' || key == 'n')
{
gotoxy(24, 12);
printf(" ");
gotoxy(24, 13);
printf(" ");
gotoxy(24, 14);
printf(" ");
return 0;
}
}
return 0;
}
5.创作不易,点个赞,加个关注呗吧💖