附录4:防空战游戏程序 362 附录4:防空战游戏程序 ∥/ Example11-5:地空战游戏程序 #include <afxwin h #include resource. h //定义飞机类 class CPlane: public COb ject CPoint m pointPlane;//飞机位置 CBitmap m bmpPlane /飞机图象 m nWidth //飞机图象宽 int nHeight //飞机图象高 CPlane o voidShowPlane(CDC *pDC, CDC pMemDC, CRect Client) CRect GetPlaneo freturn CRect(m pointPlane. x, m pointPlaney m nWidth ane y+m nHeight): I void ChangePos o voidResetPosoIm pointPlane x =0: K //飞机类的成员函数 //构造函数 CPlane: CPlane o m pointPlane= CPoint(o, 50 m bmpPlane. LoadBitmap(IDb plane BITMAP BM m bmpPlane. GetBitmap( &BM) m nWidth BM. bmWidth /显示飞机 void CPlane:: ShowPlane(CDC *pDC, CDC *pMemDC, CRect Client) pMemDC->SelectObject(&m bmpPlane) pDC->BitBlt(m pointPlane. x, m pointPlane y
附录 4:防空战游戏程序 - 362 - 附录 4:防空战游戏程序 // Example 11-5: 地空战游戏程序 #include <afxwin.h> #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; } // 自定义消息