整理一下以前学习时找到的程序。
一、游戏框架
游戏流程
直接随机生成字母,从界面最上方落下(且只能单一落下),按键匹配后消失,速度会越来越快。
二、游戏代码
程序需要用到 <graphics.h>,需要安装 EasyX,下载直接安装即可。
网址:https://easyx.cn/
/*
1.制作游戏界面
1.1 graphics
2.开始游戏
2.1 获取键值
2.2 判断是否正确
3.主函数
*/
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#include<graphics.h>
//制作游戏界面
void Welcome()
{
//界面大小
initgraph(600, 400);
//文字
setbkmode(0);
settextcolor(WHITE);
settextstyle(50, 0, L"楷体");
outtextxy(200, 50, L"打字游戏");
settextstyle(20, 0, L"楷体");
outtextxy(240, 350, L"按任意键继续");
system("pause");
}
int Playgame()
{
//随机种子
srand((unsigned int)time(NULL));
char letter;
int x, y;
char key;
int n = 1, speed = 1;
while (1)
{
letter = rand() % 26 + 65; //随机生成字母
x = rand() % 600; //随机x位置生成
for (y = 0; y < 400; y += speed) //逐渐下落
{
cleardevice();
outtextxy(x, y, letter);
if (n % 10 == 0) //加速
{
n = 1;
speed++;
}
Sleep(100);
if (_kbhit())
{
key = _getch();
if (key == letter) //判断正确
{
n++;
break;
}
}
}
}
}
int main()
{
Welcome();
Playgame();
return 0;
}