打字母游戏
#include "stdafx.h" #include <conio.h> #include <time.h> #include "graphics.h" #include <iostream> using namespace std; //获取显示器的分辨率 const int g_nScreenX=GetSystemMetrics(SM_CXSCREEN); //1280 X 800 const int g_nScreenY=GetSystemMetrics(SM_CYSCREEN); void RunGame(); //界面 void MakeFrame() { initgraph(g_nScreenX,g_nScreenY); int nColor=255; settextstyle(50,50,L"宋体"); outtextxy(g_nScreenX-800,g_nScreenY-600,L"打字游戏"); bool bIndex=true; while(!kbhit()) { if(bIndex) { setcolor(nColor-50); bIndex=false; } else { setcolor(nColor); bIndex=true; } settextstyle(40,40,L"宋体"); outtextxy(g_nScreenX-850,g_nScreenY-200,L"任意键进入游戏"); Sleep(100); } RunGame(); } //游戏进行 void RunGame() { cleardevice(); srand((unsigned)time(NULL)); int nRandPos; //随机X坐标位置 int nYPos=0; //Y轴下降位移 int nScore=0; //分数 DWORD dwSpeed=1; //速度 char cGet='a'; //输入字符 wchar_t szScore[MAXBYTE]={0}; while(1) { TCHAR cRandShow='A'+rand()%26; //随机产生字符 nRandPos=rand()%(g_nScreenX-50); setfillcolor(BLACK); //填充颜色为黑色 for(int i=0,nYPos=0;i<16;++i,nYPos+=50) { outtextxy(nRandPos,nYPos,cRandShow);//输出字符位置 Sleep(800/dwSpeed); bar(nRandPos,nYPos,nRandPos+50,nYPos+50);//擦出字符 if(_kbhit()) { cGet=_getch(); if(cGet==cRandShow||cGet==cRandShow+32) { cout<<'\a'; nScore++; i=MAXBYTE; } if(cGet==27) exit(0); } //将 int 转换成char 便于输出 settextstyle(30,30,L"宋体"); setcolor(RGB(0,255,0)); swprintf_s(szScore,L"当前分数为:%d",nScore); outtextxy(0,0,szScore); settextstyle(50,50,L"宋体"); setcolor(RGB(255,0,0)); } //每10个加速 if(nScore/10==0&&nScore!=0) dwSpeed++; } } int _tmain(int argc, _TCHAR* argv[]) { MakeFrame(); system("pause"); return 0; } s