用c++写一个简单的打字游戏

来源:互联网 发布:知我药妆产品是正品吗 编辑:程序博客网 时间:2024/05/19 21:40
#include <graphics.h>  //图形界面的头文件
#include <conio.h>
#include <time.h>     //随机数
#include <stdio.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")


HWND hWnd;


void Welcome();
void PlayGame();


//主函数
int main()
{
Welcome();
PlayGame();



closegraph();
return 0;
}


//欢迎的界面
void Welcome()
{
initgraph(640, 480);  //640  480
loadimage(NULL, L"1.jpg");
srand((unsigned int)time(NULL));
//mci:media contral interface 多媒体控制接口
mciSendString(L"open 追光者.mp3 alias zz", 0, 0, 0);
mciSendString(L"play zz", 0, 0, 0);


setbkmode(0); // 透明的 英文怎么写? TRANSPARENT

settextcolor(YELLOW);
settextstyle(50, 0, L"微软雅黑");
outtextxy(185, 50, L"打 字 练 习 系 统");


settextcolor(BLACK);
settextstyle(20, 0, L"宋体");
outtextxy(220, 180, L"没什么好说的");
outtextxy(220, 230, L"我给大家拜个早年吧");
outtextxy(220, 280, L"新年快乐~-~");
outtextxy(220, 330, L"对了,记得把输入法切换成大写");
outtextxy(380, 380, L"—小小");




while (!_kbhit())
{
settextcolor(RGB(rand() % 256, rand() % 256, rand() % 256));
outtextxy(250, 430, L"按任意键继续...");
Sleep(200);
}
_getch();
}




void PlayGame()
{
cleardevice();
while (1)
{
hWnd = GetHWnd();
char obj;//目标字母
char key;  //按键的字母
int objx, objy;//目标字母的坐标


obj = rand() % 26 + 65;
objx = rand() % 610 + 5;


settextstyle(30, 0, L"微软雅黑");

settextcolor(RGB(rand() % 156 + 100, rand() % 156 + 100, rand() % 256));
for (objy = 0; objy < 480; objy++)
{
cleardevice();
outtextxy(objx, objy, obj);
Sleep(10);

if (_kbhit())
{
key = _getch();
if (key == obj)
{
break;
}
else
{
MessageBox(hWnd, L"按错了,还需要继续努力哦!", L"game over", MB_OK);
exit(0);
}
}
}
if (objy >= 480)
{
MessageBox(hWnd, L"太慢了,还需要继续努力哦!", L"game over", MB_OK);
exit(0);
}
}
}
原创粉丝点击