2002年,同时有在学习MFC,那时候打字游戏也同时做了MFC版本 #include <afxwin.h> #include <stdlib.h> #include <time.h> / 定義資料及宣告全域變數 // TCHAR *szType="ABCDEFGHIJKLMNOPQRSTUVWXYZ";//隨機樣本 TCHAR szRand[10]; //要秀的10個字母 BOOL bLife[10]; //判斷字是否活著 POINT coord[10]; //10個字母的座標 #define gravity 10 //定義重力加速度:p..其實是落下距離 #define ID_TIMER 1 //定義計時器ID /// // class MyFrameWindow // class MyFrameWindow:public CFrameWnd { private: int i; public: afx_msg void OnPaint(); afx_msg void OnChar(UINT,UINT,UINT); afx_msg void OnTimer(UINT); DECLARE_MESSAGE_MAP() }; void MyFrameWindow::OnPaint() { CString szBuffer; CPaintDC paintDC(this); paintDC.MoveTo(0,150); paintDC.LineTo(300,150); for(i=0;i<10;i++) { if(bLife[i]) { szBuffer=szRand[i]; paintDC.TextOut(coord[i].x,coord[i].y,szBuffer); } } } void MyFrameWindow::OnChar(UINT nChar,UINT nRepCnt,UINT nFlags) { //CFrameWnd::OnChar(nChar,nRepCnt,nFlags); TCHAR choice=nChar; if(choice>='a'&&choice<='z') choice^=32; //換成大寫 for(i=0;i<10;i++) { if(choice==szRand[i])//字一樣 { coord[i].y=10; szRand[i]=szType[rand()%26]; break; } } } void MyFrameWindow::OnTimer(UINT timerId) { srand(time(NULL)); int now; now=rand()%10; coord[now].y+=gravity; if(coord[now].y>=150) bLife[now]=FALSE; Invalidate(TRUE); } /// / 訊息映射表(MyFrameWindow) /// BEGIN_MESSAGE_MAP(MyFrameWindow,CFrameWnd) ON_WM_PAINT() //負責重繪 ON_WM_CHAR() //抓字元 ON_WM_TIMER() //計時器 END_MESSAGE_MAP() /// class TypeApp // class TypeApp:public CWinApp { public: TypeApp() :CWinApp("Type") {} BOOL InitInstance(); void GameInit();//設定遊戲資訊 }Type; BOOL TypeApp::InitInstance() { CFrameWnd *MyFrame=new MyFrameWindow; m_pMainWnd=MyFrame; MyFrame->Create(NULL,(LPCTSTR)"打字練習"); MyFrame->ShowWindow(SW_SHOW); GameInit(); MyFrame->SetTimer(ID_TIMER,500,NULL); return TRUE; } void TypeApp::GameInit() { srand(time(NULL)); int i; for(i=0;i<10;i++) { szRand[i]=szType[rand()%26];//隨機產生字母 coord[i].x=10+25*i; coord[i].y=10; bLife[i]=TRUE; } } //