C++实现的打字母游戏示例
发布时间:2020-12-31 17:35:05 所属栏目:经验 来源:网络整理
导读:本篇章节讲解C++实现的打字母游戏。供大家参考研究具体如下: // 打字母的游戏// 编译代码请先安装 VC 绘图库(V20091123)#include graphics.h#include conio.h#include time.h// 欢迎界面void welcome(){ // 输出屏幕提示 cleardevice(); setcolor
|
本篇章节讲解C++实现的打字母游戏。分享给大家供大家参考,具体如下:
// 打字母的游戏
// 编译代码请先安装 VC 绘图库(V20091123)
#include <graphics.h>
#include <conio.h>
#include <time.h>
// 欢迎界面
void welcome()
{
// 输出屏幕提示
cleardevice();
setcolor(YELLOW);
setfont(64,"黑体");
outtextxy(200,50,"打字游戏");
setcolor(WHITE);
setfont(16,"宋体");
outtextxy(100,200,"就是很传统的那个掉字母然后按相应键就消失的游戏");
outtextxy(100,280,"功能并不很完善,比如生命数、分数等都没有写");
outtextxy(100,320,"感兴趣的自己加进去吧");
// 实现闪烁的“按任意键继续”
int c=255;
while(!kbhit())
{
setcolor(RGB(c,0));
outtextxy(280,400,"按任意键继续");
c-=8;
if (c<0) c=255;
Sleep(20);
}
getch();
cleardevice();
}
// 退出界面
void goodbye()
{
cleardevice();
setcolor(YELLOW);
setfont(48,"黑体");
outtextxy(104,"多写程序 不老青春");
getch();
}
// 主函数
void main()
{
// 初始化屏幕为 640x480
initgraph(640,480);
welcome(); // 显示欢迎界面
srand(time(NULL)); // 设置随机种子
setfont(16,"Arial"); // 设置字母的字体和大小
char target[2] = " "; // 定义字母字符串
char key; // 定义用户按键变量
// 主循环
while(true)
{
target[0] = 65 + rand() % 26; // 产生任意大写字母
int x = rand()%630;
for (int y=0; y<460; y++)
{
setcolor(WHITE); // 设置字母的颜色
outtextxy(x,y,target); // 显示字母
if(kbhit())
{
key = getch();
if((key == target[0]) || (key == target[0] + 32))
{
// 设置清除
setcolor(BLACK);
outtextxy(x,target); // 清除原字符
break; // 跳出循环,进行下一个字符
}
else if (key == 27)
{
goto EXIT; // 如果按 ESC,退出游戏主循环
}
}
// 延时,并清除原字符
Sleep(10);
setcolor(BLACK);
outtextxy(x,target);
}
}
EXIT:
// 退出部分
goodbye();
// 关闭图形界面
closegraph();
}
希望本文所述对大家C++程序设计有所帮助。 (编辑:92站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |


