您的位置:首页 > 编程语言 > C语言/C++

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

2017-11-22 20:57 459 查看
#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);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: