附录4:防空战游戏程序 362 附录4:防空战游戏程序 ∥/ Example11-5:地空战游戏程序 #include SelectObject(&m bmpPlane) pDC->BitBlt(m pointPlane. x, m pointPlane y
附录 4:防空战游戏程序 - 362 - 附录 4:防空战游戏程序 // Example 11-5: 地空战游戏程序 #include #include "resource.h" // 定义飞机类 class CPlane: public CObject { CPoint m_pointPlane; // 飞机位置 CBitmap m_bmpPlane; // 飞机图象 int m_nWidth; // 飞机图象宽 int m_nHeight; // 飞机图象高 public: CPlane(); voidShowPlane(CDC *pDC, CDC *pMemDC, CRect Client); CRect GetPlane(){return CRect(m_pointPlane.x, m_pointPlane.y, m_pointPlane.x+m_nWidth, m_pointPlane.y+m_nHeight);} voidChangePos(); voidResetPos(){m_pointPlane.x = 0;} }; // 飞机类的成员函数 // 构造函数 CPlane::CPlane() { m_pointPlane= CPoint(0, 50); m_bmpPlane.LoadBitmap(IDB_PLANE); BITMAP BM; m_bmpPlane.GetBitmap(&BM); m_nWidth = BM.bmWidth; m_nHeight = BM.bmHeight; } // 显示飞机 void CPlane::ShowPlane(CDC *pDC, CDC *pMemDC, CRect Client) { pMemDC->SelectObject(&m_bmpPlane); pDC->BitBlt(m_pointPlane.x, m_pointPlane.y
附录4:防空战游戏程序 363 m nWidth, m nHeight, pMemDC, 0, 0, SRCAND) /改变飞机位置 void CPlane: ChangePoso f(m pointPlane x>788) m pointPlane X =0: lse m pointPlanex + 10 //定义炸弹类 class CBomb: public Cob ject CPoint m pointBomb;//炸弹位置 CBitmap m bmpBomb /炸弹图象 int m nWidth: /炸弹图象高 m nHeight //炸弹图象宽 COmbo void ShowBomb(CDC *pDC, CDC *pMemDC, CRect Client) CRect GetBombO freturn CRect(m pointBomb. x, m pointBomb y m pointBomb. x+m nWidth, m point Bomb. y+m nHeight): I void ChangePos(int x) void ResetPoso(m pointBomb. x=0, m pointBomb. y=80: 1 //炸弹类成员函数 //炸弹类构造函数 COmb: COmbo m pointBomb. x= 0 m pointBomb. y =80 m bmpBomb LoadBitmap (IDb bomb) BITMAP BM m bmpBomb. GetBitmap(&BM) m nWidth= BM. bmWidth m nHeight BM. bmHeight
附录 4:防空战游戏程序 - 363 - m_nWidth, m_nHeight, pMemDC,0,0,SRCAND); } // 改变飞机位置 void CPlane::ChangePos() { if(m_pointPlane.x>788) m_pointPlane.x = 0; else m_pointPlane.x += 10; } // 定义炸弹类 class CBomb: public CObject { CPoint m_pointBomb;// 炸弹位置 CBitmap m_bmpBomb; // 炸弹图象 int m_nWidth; // 炸弹图象高 int m_nHeight; // 炸弹图象宽 public: CBomb(); void ShowBomb(CDC *pDC, CDC *pMemDC, CRect Client); CRect GetBomb(){return CRect(m_pointBomb.x, m_pointBomb.y, m_pointBomb.x+m_nWidth, m_pointBomb.y+m_nHeight);} void ChangePos(int x); void ResetPos(){m_pointBomb.x=0, m_pointBomb.y=80;} }; // 炸弹类成员函数 // 炸弹类构造函数 CBomb::CBomb() { m_pointBomb.x = 0; m_pointBomb.y = 80; m_bmpBomb.LoadBitmap(IDB_BOMB); BITMAP BM; m_bmpBomb.GetBitmap(&BM); m_nWidth= BM.bmWidth; m_nHeight = BM.bmHeight; }
附录4:防空战游戏程序 /显示炸弹 void CBomb: Show Bomb(CDC *pDC, CDC *pMemDC, CRect Client) pDC->BitBlt(m pointBomb. x, m pointBomb y, m nWidth, m nHeight, pMemDC, 0, 0, SRCAND); /改变位置 void COmb: ChangePos(int x) m pointBomby if(m pointBomb. y)480) m pointBomb.y m pointBomb x //定义高炮类 class CLank: public COb ject CPoint m pointTank;//高炮位置 CBitmap m bmptank: /高炮图象 int m nWidth: /高炮图象宽 int m nHeight //高炮图象高 voidShowTank(CDC *pDC, CDC *pMemDC, CRect Client) CRect Get TankO freturn CRect(m point Tank. x, m pointTank y m pointTank. x+m nWidth, m pointTank. y+m nHeight): 1 void ChangePos (int tag void ResetPosoim pointTank. X=350: I //高炮类成员函数 //高炮类构造函数 Tank:: CRank m pointTank. X= 350 m pointTank. y =450: m bmp Tank. LoadBitmap (IDb TAnk)
附录 4:防空战游戏程序 - 364 - // 显示炸弹 void CBomb::ShowBomb(CDC *pDC, CDC *pMemDC, CRect Client) { pMemDC->SelectObject(&m_bmpBomb); pDC->BitBlt(m_pointBomb.x, m_pointBomb.y, m_nWidth, m_nHeight, pMemDC, 0, 0, SRCAND); } // 改变位置 void CBomb::ChangePos(int x) { m_pointBomb.y += 20; if(m_pointBomb.y>480) m_pointBomb.y = 80; m_pointBomb.x = x; } // 定义高炮类 class CTank: public CObject { CPoint m_pointTank;// 高炮位置 CBitmap m_bmpTank; // 高炮图象 int m_nWidth; // 高炮图象宽 int m_nHeight; // 高炮图象高 public: CTank(); voidShowTank(CDC *pDC, CDC *pMemDC, CRect Client); CRect GetTank(){return CRect(m_pointTank.x, m_pointTank.y, m_pointTank.x+m_nWidth, m_pointTank.y+m_nHeight);} voidChangePos(int tag); voidResetPos(){m_pointTank.x = 350;} }; // 高炮类成员函数 // 高炮类构造函数 CTank::CTank() { m_pointTank.x = 350; m_pointTank.y = 450; m_bmpTank.LoadBitmap(IDB_TANK);
附录4:防空战游戏程序 365 BITMAP BM m bmp Tank. Get Bitmap(&BM) Width= bm. bmwidth m nHeight BM. bmHeight //显示高炮 void CTank: ShowTank(CDC *pDC, CDC *pMemDC, CRect Client) pMemDC->SelectObject(&m bmp Tank) pDC->BitBlt(m pointTank x, m pointTank y, m nWidth, m nHeight, pMemDC, 0, 0, SRCAND) /改变位置 void CTank:: ChangePos (int tag) if(tag ==0&&m pointTank x>0) m pointTank. X - 20 else if(tag = 1 & m pointTank. x+m nWidth 798) m pointTank. x +=20 //定义炮弹类 class STone: public Cob ject CPoint m pointstone;//炮弹位置 CBitmap m bmpstone /炮弹图象 int m nWidth: /炮弹图象宽 int m nHeight //炮弹图象高 BOOM sHot;//是否已发射 BOOLHaveStone o return ! m sHot: 1 void Shot(int x) voidShowStone(CDC *pDC, CDC =pMemDC, CRect Client) CRect GetStoneo freturn CRect(m pointStone. x, m pointStone y m pointstone xtm nWidth, m pointStone. y+m nHeight); void ChangePos o voidResetPosoim sHot FALSE: H
附录 4:防空战游戏程序 - 365 - BITMAP BM; m_bmpTank.GetBitmap(&BM); m_nWidth= BM.bmWidth; m_nHeight = BM.bmHeight; } // 显示高炮 void CTank::ShowTank(CDC *pDC, CDC *pMemDC, CRect Client) { pMemDC->SelectObject(&m_bmpTank); pDC->BitBlt(m_pointTank.x, m_pointTank.y, m_nWidth, m_nHeight, pMemDC,0,0,SRCAND); } // 改变位置 void CTank::ChangePos(int tag) { if(tag == 0 && m_pointTank.x > 0) m_pointTank.x -= 20; else if(tag == 1 && m_pointTank.x+m_nWidth < 798) m_pointTank.x += 20; } // 定义炮弹类 class CStone: public CObject { CPoint m_pointStone; // 炮弹位置 CBitmap m_bmpStone; // 炮弹图象 int m_nWidth; // 炮弹图象宽 int m_nHeight; // 炮弹图象高 BOOLm_bShot; // 是否已发射 public: CStone(); BOOLHaveStone(){return !m_bShot;} voidShot(int x); voidShowStone(CDC *pDC, CDC *pMemDC, CRect Client); CRect GetStone(){return CRect(m_pointStone.x, m_pointStone.y, m_pointStone.x+m_nWidth, m_pointStone.y+m_nHeight);} voidChangePos(); voidResetPos(){m_bShot = FALSE;}
附录4:防空战游戏程序 void Refill (m bShot FALSE: 1 //炮弹类成员函数 //炮弹类构造函数 CStone:: CStone o m sHot false. m bmpStone. LoadBitmap (IDB STone) BITMAP BM m bmpStone. GetBitmap(&BM) m nWidth= BM. bmWidth m nHeight = BM. bmHeight //发射 void CStone: Shot(int x) sHot TrUe = m pointstone. y 440 //显示炮弹 void CStone: ShowStone(CDC *pDC, CDC *pMemDC, CRect Client) f(m sHot) pMemDC->SelectObject(&m bmpStone): pDC->BitBlt(m pointStone x, m pointStone y m nWidth, m nHeight, pMemDC, 0, 0, SRCAND /改变位置 void CStone: ChangePos o f(m sHot) m pointstoney /自定义消息
附录 4:防空战游戏程序 - 366 - voidRefill(){m_bShot = FALSE;} }; // 炮弹类成员函数 // 炮弹类构造函数 CStone::CStone() { m_bShot = FALSE; m_bmpStone.LoadBitmap(IDB_STONE); BITMAP BM; m_bmpStone.GetBitmap(&BM); m_nWidth= BM.bmWidth; m_nHeight = BM.bmHeight; } // 发射 void CStone::Shot(int x) { m_bShot = TRUE; m_pointStone.x = x+48; m_pointStone.y = 440; } // 显示炮弹 void CStone::ShowStone(CDC *pDC, CDC *pMemDC, CRect Client) { if(m_bShot) { pMemDC->SelectObject(&m_bmpStone); pDC->BitBlt(m_pointStone.x, m_pointStone.y, m_nWidth, m_nHeight, pMemDC,0,0,SRCAND); } } // 改变位置 void CStone::ChangePos() { if(m_bShot) m_pointStone.y -= 20; } // 自定义消息
附录4:防空战游戏程序 #define on Wm gameover 0x0402 #define I shot you #define YoU SHot ME //定义框架窗口类 class Cmy Wnd: public FRame Wnd CBomb m Bomb CStone m Stone CPoint m pointMoutain[5 public CMyWnd o void DrawFields(CDC =pDC, CRect Client BOOL ShotOn(CRect &bodyl, CRect &body 2) protected: afx msg void OnPaint afx msg void OnBegino afx msg void OnEnd o afx msg void On Timer ( UINT nIDEvent afx msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT n Flags) afx msg void On Game Over quint tag) DECLARE MESSAGE MAP O /消息映射 BEGIN MESSAGE MAP (CMy Wnd, CFrameWnd ON WM PAINT O ON COMMAND (ID BEGIN, OnBegin) ON COMMAND(ID END, OnEnd) ON WM TIMERO ON WM KEYDOWN O ON MESSAGE (ON WM GAMEOVER, OnGameOver) END MESSAGE MAP O //框架窗口类的成员函数 //构造函数 CMyWnd: CMy O
附录 4:防空战游戏程序 - 367 - #define ON_WM_GAMEOVER 0x0402 #define I_SHOT_YOU 0 #define YOU_SHOT_ME 1 // 定义框架窗口类 class CMyWnd: public CFrameWnd { CPlane m_Plane; CBomb m_Bomb; CTank m_Tank; CStone m_Stone; CPoint m_pointMoutain[5]; public: CMyWnd(); void DrawFields(CDC *pDC, CRect Client); BOOL ShotOn(CRect &body1, CRect &body2); protected: afx_msg void OnPaint(); afx_msg void OnBegin(); afx_msg void OnEnd(); afx_msg void OnTimer(UINT nIDEvent); afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); afx_msg void OnGameOver(UINT tag); DECLARE_MESSAGE_MAP() }; // 消息映射 BEGIN_MESSAGE_MAP(CMyWnd,CFrameWnd) ON_WM_PAINT() ON_COMMAND(ID_BEGIN, OnBegin) ON_COMMAND(ID_END, OnEnd) ON_WM_TIMER() ON_WM_KEYDOWN() ON_MESSAGE(ON_WM_GAMEOVER, OnGameOver) END_MESSAGE_MAP() // 框架窗口类的成员函数 // 构造函数 CMyWnd::CMyWnd() {
附录4:防空战游戏程序 368 m pointMoutain[o]= CPoint(300, 400) m pointMoutain[1]= CPoint(400, 300 m pointMoutain[2]= CPoint(500, 350) m pointMoutain[3]= CPoint (600, 250) m pointMoutain[4]=CPoint(800, 400) /更新窗口客户区 void CMyWnd:: OnPaint o CPaintDC dc(this) CDC MemO MemDC. Create CompatibleDC (NULL) GetClient Rect(&rect) DrawFields(&dc, rect) //画战场 m_ Plane. ShowPlan(&dc,& MemO,rect);//画对方飞机 m Bomb. ShowBomb(&dc //画炸弹 m Tank. Show Tank(&dc rec //画己方防空导弹车 m Stone. ShowStone(&dc DC,rect);//画导弹 /响应菜单消息:开始游戏 void CMy Wnd:: OnBeginO m Bomb. ResetPoso m Plane. ResetPos o m Stone. ResetPos o Invalidate SetTimer(1, 100, NULL) //响应菜单消息:游戏结束 void CMyWnd:: OnEnd O KillTimer(1) PostMessage(WM QUIT) //定时器消息响应函数 void CMy Wnd: OnTimer (UINT nIDEvent
附录 4:防空战游戏程序 - 368 - m_pointMoutain[0] = CPoint(300, 400); m_pointMoutain[1] = CPoint(400, 300); m_pointMoutain[2] = CPoint(500, 350); m_pointMoutain[3] = CPoint(600, 250); m_pointMoutain[4] = CPoint(800, 400); } // 更新窗口客户区 void CMyWnd::OnPaint() { CPaintDC dc(this); CDC MemDC; MemDC.CreateCompatibleDC(NULL); CRect rect; GetClientRect(&rect); DrawFields(&dc, rect); // 画战场 m_Plane.ShowPlane(&dc, &MemDC, rect); // 画对方飞机 m_Bomb.ShowBomb(&dc, &MemDC, rect); // 画炸弹 m_Tank.ShowTank(&dc, &MemDC, rect); // 画己方防空导弹车 m_Stone.ShowStone(&dc, &MemDC, rect); // 画导弹 } // 响应菜单消息: 开始游戏 void CMyWnd::OnBegin() { m_Bomb.ResetPos(); m_Plane.ResetPos(); m_Tank.ResetPos(); m_Stone.ResetPos(); Invalidate(); SetTimer(1, 100, NULL); } // 响应菜单消息: 游戏结束 void CMyWnd::OnEnd() { KillTimer(1); PostMessage(WM_QUIT); } // 定时器消息响应函数 void CMyWnd::OnTimer(UINT nIDEvent)
附录4:防空战游戏程序 369 InvalidateRect( m plane. GetPlane();//修改飞机位置 m Plane. ChangePos o InvalidateRectm Plane. GetPlane(, FALSe) InvalidateRect( m bomb. GetBomb o);∥/修改炸弹位置 m Bomb. ChangePos(m Plane. GetPlane(. left InvalidateRect(m Bomb. GetBomb O, FALSE if(!m Stone. HaveStone o) /修改炮弹位置 CRect rect m Stone. GetStone o if(rect. top 3) m Stone Refill els InvalidateRect(rect) InvalidateRect(m Stone. GetStone o //判断射击效果 if(!m Stone. HaveStoneo&& ShotOn(m Plane. GetPlane(, m Stone. GetStone o)) SendMessage(ON WM GAMEOVER, YOU SHOT ME) else if( ShotOn(m Tank. Get(, m Bomb. GetBomb O)) SendMessage(ON WM GAMEOVER, I SHOT YOU) /自定义的游戏结束消息响应函数 void CMyWnd:: OnGame Over (UiNt tag KillTimer(1) if(tag = I SHOT YOU) MessageBox("我砸到你了!!!") 1 MessageBox("你击中我了!!!"); //按键消息响应函数 void CMy Wnd: OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags if( n Char= VK LEFT)∥/使用〈键左移高炮
附录 4:防空战游戏程序 - 369 - { InvalidateRect(m_Plane.GetPlane()); // 修改飞机位置 m_Plane.ChangePos(); InvalidateRect(m_Plane.GetPlane(), FALSE); InvalidateRect(m_Bomb.GetBomb()); // 修改炸弹位置 m_Bomb.ChangePos(m_Plane.GetPlane().left); InvalidateRect(m_Bomb.GetBomb(), FALSE); if(!m_Stone.HaveStone()) // 修改炮弹位置 { CRect rect = m_Stone.GetStone(); if(rect.top < 3) m_Stone.Refill(); else { InvalidateRect(rect); m_Stone.ChangePos(); } InvalidateRect(m_Stone.GetStone(), FALSE); } // 判断射击效果 if(!m_Stone.HaveStone() && ShotOn(m_Plane.GetPlane(), m_Stone.GetStone())) SendMessage(ON_WM_GAMEOVER, YOU_SHOT_ME); else if(ShotOn(m_Tank.GetTank(), m_Bomb.GetBomb())) SendMessage(ON_WM_GAMEOVER, I_SHOT_YOU); } // 自定义的游戏结束消息响应函数 void CMyWnd::OnGameOver(UINT tag) { KillTimer(1); if(tag == I_SHOT_YOU) MessageBox("我砸到你了!!!"); else MessageBox("你击中我了!!!"); } // 按键消息响应函数 void CMyWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { if(nChar == VK_LEFT) // 使用 < 键左移高炮
附录4:防空战游戏程序 370 InvalidateRect(m Tank. GetTankO) Tank. ChangePos(O) InvalidateRect(m Tank. Get(, FALSE) else if( nChar==WK_RIGH)//使用〉键右移高炮 InvalidateRect(m Tank. Get Tank O) m Tank. ChangePos(1) InvalidateRect(m Tank. Get Tank O) else if( n Char=32& m stone. HaveStone o)//使用空格键发炮 m Stone Shot(m Tank. Get TankO. left /画战场 void CMyWnd:: DrawFields(CDC *pDC, CRect Client CBrush *kpOldBrush, brushSky, brushGrass, brushMoutain CRect rect(Client) brushsky. CreatesolidBrush(RGB(127,200,255);//画天空 pOldBrush pDC->SelectOb ject(&brushSky) pDC->Rectangle (rect pDC->SelectObject(poldBrush) brushgrass. CreateSolidBrush(RGB(0,255,0);//画草地 old Brush pDC->SelectObject(&brushGrass) DC->Rectangle(rect) pDC->SelectObject(poldBrush) brushmoutain. CreatesolidBrush(RGB(125,50,0);∥/画大山 oldBrush pDC->SelectObject( &brushMoutain pDC->Polygon(m pointMoutain, 5) DC->SelectObject(pOldBrush) //判断射击效果 BOOL CMyWnd: ShotOn( CRect &body 1, CRect &body 2) return body l PtInRect(body2. TopLefto)
附录 4:防空战游戏程序 - 370 - { InvalidateRect(m_Tank.GetTank()); m_Tank.ChangePos(0); InvalidateRect(m_Tank.GetTank(), FALSE); } else if(nChar == VK_RIGHT) // 使用 > 键右移高炮 { InvalidateRect(m_Tank.GetTank()); m_Tank.ChangePos(1); InvalidateRect(m_Tank.GetTank()); } else if(nChar == 32 && m_Stone.HaveStone()) // 使用空格键发炮 m_Stone.Shot(m_Tank.GetTank().left); } // 画战场 void CMyWnd::DrawFields(CDC *pDC, CRect Client) { CBrush *pOldBrush, brushSky, brushGrass, brushMoutain; CRect rect(Client); brushSky.CreateSolidBrush(RGB(127, 200, 255)); // 画天空 pOldBrush = pDC->SelectObject(&brushSky); pDC->Rectangle(rect); pDC->SelectObject(pOldBrush); brushGrass.CreateSolidBrush(RGB(0, 255, 0)); // 画草地 pOldBrush = pDC->SelectObject(&brushGrass); rect.top = 400; pDC->Rectangle(rect); pDC->SelectObject(pOldBrush); brushMoutain.CreateSolidBrush(RGB(125, 50, 0)); // 画大山 pOldBrush = pDC->SelectObject(&brushMoutain); pDC->Polygon(m_pointMoutain, 5); pDC->SelectObject(pOldBrush); } // 判断射击效果 BOOL CMyWnd::ShotOn(CRect &body1, CRect &body2) { return body1.PtInRect(body2.TopLeft()); }
附录4:防空战游戏程序 371 /定义应用程序类 class CMyApp public CWinApp BOOL InitInstance o //初始化实例函数 BOOL CMyApp: InitInstance O CMy Wnd * kpFrame new CMy Wnd pFrame->LoadFrame (IDR MAINFRAME pFrame->ShowWindow(SW SHOWMAXIMIZED) rame return truE; /说明应用程序类的全局对象 CMyApp ThisApp
附录 4:防空战游戏程序 - 371 - // 定义应用程序类 class CMyApp: public CWinApp { public: BOOLInitInstance(); }; // 初始化实例函数 BOOL CMyApp::InitInstance() { CMyWnd *pFrame = new CMyWnd; pFrame->LoadFrame(IDR_MAINFRAME); pFrame->ShowWindow(SW_SHOWMAXIMIZED); this->m_pMainWnd = pFrame; return TRUE; } // 说明应用程序类的全局对象 CMyApp ThisApp;