1.介绍
先启动程序,在MFC下进行初始化操作,把相关的游戏对象初始化例如战机、敌机、背景、子弹和炸弹等,同时初始化绘图环境如内存DC、设备DC、位图等。然后不断的绘制游戏对象,运用算法实现游戏规则。
2.程序
CFoePlane.h
#pragma once
#include"公共.h"
#include"PlayerPlane.h"
#include"Gunner.h"
class CFoePlane
{
public:
HBITMAP m_hBmpFoePlane;
int m_nShowID;
int m_nBlood;
int x;
int y;
public:
CFoePlane(void);
virtual~CFoePlane(void);
public:
bool IsBoom();
virtual bool IsFoePlanrHitPlayerPlane(CPlayerPlane& player)=0;
virtual bool IsGunnerHitFoePlane(CGunner* gunner)=0;
virtual void FoePlaneMove()=0;
virtual void InitFoePlane(HINSTANCE hIns)=0;
virtual void ShowFoePlane(HDC hMemDC)=0;
void DownBlood();
};
CFoePlane.cpp
#include "FoePlane.h"
CFoePlane::CFoePlane(void)
{
m_hBmpFoePlane=0;
m_nShowID=0;
m_nBlood=0;
x=0;
y=0;
}
CFoePlane::~CFoePlane(void)
{
::DeleteObject(m_hBmpFoePlane);
}
bool CFoePlane::IsBoom()
{
if(m_nBlood==0)
{
return true;
}else
{
return false;
}
}
void CFoePlane::DownBlood()
{
m_nBlood--;
}
CMidFoePlane
CBigFoePlane.h
#pragma once
#include "foeplane.h"
class CBigFoePlane :
public CFoePlane
{
public:
CBigFoePlane(void);
~CBigFoePlane(void);
public:
virtual bool IsFoePlanrHitPlayerPlane(CPlayerPlane& player);
virtual bool IsGunnerHitFoePlane(CGunner* gunner);
virtual void FoePlaneMove();
virtual void InitFoePlane(HINSTANCE hIns);
virtual void ShowFoePlane(HDC hMemDC);
};
CBigFoePlane.cpp
#include "BigFoePlane.h"
CBigFoePlane::CBigFoePlane(void)
{
m_hBmpFoePlane=NULL;
m_nShowID=3;
m_nBlood=6;
x=rand()%(380-108);
y=-128;
}
CBigFoePlane::~CBigFoePlane(void)
{
}
bool CBigFoePlane::IsFoePlanrHitPlayerPlane(CPlayerPlane& player)
{
if(player.x+30>=x && player.x+30<=x+108 && player.y+50>=y&&player.y+50<=y+128)
{
return true;
}
if(player.x>=x && player.x<=x+108 && player.y>=y&&player.y<=y+128)
{
return true;
}
if(player.x+60>=x && player.x+60<=x+108 && player.y>=y&&player.y<=y+128)
{
return true;
}
}
bool CBigFoePlane::IsGunnerHitFoePlane(CGunner* gunner)
{
if(gunner->x>=x&&gunner->x<=x+108&&gunner->y>=y&&gunner->y<=y+128)
return true;
return false;
}
void CBigFoePlane::FoePlaneMove()
{
y+=2;
}
void CBigFoePlane::InitFoePlane(HINSTANCE hIns)
{
m_hBmpFoePlane=::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_BIGFOEPLANE));
}
void CBigFoePlane::ShowFoePlane(HDC hMemDC)
{
HDC hTemp=::CreateCompatibleDC(hMemDC);
::SelectObject(hTemp,m_hBmpFoePlane);
::BitBlt(hMemDC,x,y,108,128,hTemp,0,128*(3-m_nShowID),SRCAND);
::DeleteDC(hTemp);
}
CMidFoePlane.h
#pragma once
#include "foeplane.h"
class CMidFoePlane :
public CFoePlane
{
public:
CMidFoePlane(void);
~CMidFoePlane(void);
public:
virtual bool IsFoePlanrHitPlayerPlane(CPlayerPlane& player);
virtual bool IsGunnerHitFoePlane(CGunner* gunner);
virtual void FoePlaneMove();
virtual void InitFoePlane(HINSTANCE hIns);
virtual void ShowFoePlane(HDC hMemDC);
};
CMidFoePlane.cpp
#include "MidFoePlane.h"
CMidFoePlane::CMidFoePlane(void)
{
m_hBmpFoePlane=NULL;
m_nShowID=2;
m_nBlood=4;
x=rand()%(380-70);
y=-90;
}
CMidFoePlane::~CMidFoePlane(void)
{
}
bool CMidFoePlane::IsFoePlanrHitPlayerPlane(CPlayerPlane& player)
{
if(player.x+30>=x && player.x+30<=x+70 && player.y+50>=y&&player.y+50<=y+90)
{
return true;
}
if(player.x>=x && player.x<=x+70 && player.y>=y&&player.y<=y+90)
{
return true;
}
if(player.x+60>=x && player.x+60<=x+70 && player.y>=y&&player.y<=y+90)
{
return true;
}
}
bool CMidFoePlane::IsGunnerHitFoePlane(CGunner* gunner)
{
if(gunner->x>=x&&gunner->x<=x+70&&gunner->y>=y&&gunner->y<=y+90)
return true;
return false;
}
void CMidFoePlane::FoePlaneMove()
{
y=y+4;
}
void CMidFoePlane::InitFoePlane(HINSTANCE hIns)
{
m_hBmpFoePlane=::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_MIDFOEPLANE));
}
void CMidFoePlane::ShowFoePlane(HDC hMemDC)
{
HDC hTemp=::CreateCompatibleDC(hMemDC);
::SelectObject(hTemp,m_hBmpFoePlane);
::BitBlt(hMemDC,x,y,70,90,hTemp,0,90*(2-m_nShowID),SRCAND);
::DeleteDC(hTemp);
}
CSmallFoePlane.h
#pragma once
#include "foeplane.h"
class CSmallFoePlane :
public CFoePlane
{
public:
CSmallFoePlane(void);
~CSmallFoePlane(void);
public:
virtual bool IsFoePlanrHitPlayerPlane(CPlayerPlane& player);
virtual bool IsGunnerHitFoePlane(CGunner* gunner);
virtual void FoePlaneMove();
virtual void InitFoePlane(HINSTANCE hIns);
virtual void ShowFoePlane(HDC hMemDC);
};
CSmallFoePlane.cpp
#include "SmallFoePlane.h"
CSmallFoePlane::CSmallFoePlane(void)
{
m_hBmpFoePlane=NULL;
m_nShowID=1;
m_nBlood=2;
x=rand()%(380-34);
y=-28;
}
CSmallFoePlane::~CSmallFoePlane(void)
{
}
bool CSmallFoePlane::IsFoePlanrHitPlayerPlane(CPlayerPlane& player)
{
if(player.x+30>=x && player.x+30<=x+34 && player.y+50>=y&&player.y+50<=y+28)
{
return true;
}
if(player.x>=x && player.x<=x+34 && player.y>=y&&player.y<=y+28)
{
return true;
}
if(player.x+60>=x && player.x+60<=x+34 && player.y>=y&&player.y<=y+28)
{
return true;
}
}
bool CSmallFoePlane::IsGunnerHitFoePlane(CGunner* gunner)
{
if(gunner->x>=x && gunner->x<=x+34 && gunner->y>=y && gunner->y<=y+28 )
return true;
return false;
}
void CSmallFoePlane::FoePlaneMove()
{
y=y+6;
}
void CSmallFoePlane::InitFoePlane(HINSTANCE hIns)
{
m_hBmpFoePlane=::LoadBitmap(hIns,MAKEINTRESOURCE(IIDB_SMALLFOEPLANE ));
}
void CSmallFoePlane::ShowFoePlane(HDC hMemDC)
{
HDC hTemp=::CreateCompatibleDC(hMemDC);
::SelectObject(hTemp,m_hBmpFoePlane);
::BitBlt(hMemDC,x,y,34,28,hTemp,0,28*(1-m_nShowID),SRCAND);
::DeleteDC(hTemp);
}
CGunner.h
#pragma once
#include"公共.h"
class CGunner
{
public:
HBITMAP m_hBmpGunner;
int x;
int y;
public:
CGunner(void);
~CGunner(void);
public:
void GunnerMove();
void InitGunner(HINSTANCE hInsint,int x,int y);
void ShowGunner(HDC hMemDC);
};
CGunner.cpp
#include "Gunner.h"
#include"resource.h"
CGunner::CGunner(void)
{
m_hBmpGunner=NULL;
x=0;
y=0;
}
CGunner::~CGunner(void)
{
DeleteObject(m_hBmpGunner);
m_hBmpGunner=NULL;
}
void CGunner::GunnerMove()
{
y-=10;
}
void CGunner::InitGunner(HINSTANCE hIns,int x,int y)
{
this->x=x;
this->y=y;
m_hBmpGunner=::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_BITMAP1));
}
void CGunner::ShowGunner(HDC hMemDC)
{
HDC hTemp=::CreateCompatibleDC(hMemDC);
::SelectObject(hTemp,m_hBmpGunner);
::BitBlt(hMemDC,x,y,6,9,hTemp,0,0,SRCAND);
::DeleteDC(hTemp);
}
CPlaneCtrl.h
#pragma once
#include"CGameCtrl.h"
#include"Back.h"
#include"PlayerPlane.h"
#include"Gunner.h"
#include"FoePlane.h"
class CPlaneCtrl :
public CGameCtrl
{
public:
CBack back;
CPlayerPlane plane;
list<CGunner*>m_lstGunner;
list<CFoePlane*>m_lstFoePlane;
list<CFoePlane*>m_boomFoePlane;
bool PlaneMoveFlag;
bool flag;
public:
DECLARE();
public:
CPlaneCtrl(void);
~CPlaneCtrl(void);
void OnCreateGame();
void OnGameDraw();
void OnGameRun(WPARAM nTimeID);
void OnKeyDown(WPARAM nKeyChar);
void OnKeyUp(WPARAM nKeyChar);
void OnCloseGame();
void AllGunnerMove();
void AllGunnerShow(HDC hMemDC);
void AllFoePlaneMove();
void AllFoePlaneShow(HDC hMemDC);
void AllBoomFoePlaneShow(HDC hMemDC);
void CreateFoePlane();
void GunnerHitFoePlane();
void Change();
bool GameOver();
};
CPlaneCtrl.cpp
#include "PlaneCtrl.h"
#include"Back.h"
#include"Gunner.h"
#include"FoePlane.h"
#include"BigFoePlane.h"
#include"MidFoePlane.h"
#include"SmallFoePlane.h"
IMPLEMENT(CPlaneCtrl);
CPlaneCtrl::CPlaneCtrl(void)
{
PlaneMoveFlag =false;
flag=true;
}
CPlaneCtrl::~CPlaneCtrl(void)
{
list<CGunner*>::iterator ite=m_lstGunner.begin();
while(ite!=m_lstGunner.end())
{
delete(*ite);
++ite;
}
list<CFoePlane*>::iterator iteFoePlane=m_lstFoePlane.begin();
while(iteFoePlane!=m_lstFoePlane.end())
{
delete(*iteFoePlane);
++iteFoePlane;
}
list<CFoePlane*>::iterator iteBoomPlane=m_boomFoePlane.begin();
while(iteBoomPlane!=m_boomFoePlane.end())
{
delete(*iteBoomPlane);
++iteBoomPlane;
}
}
void CPlaneCtrl::OnCreateGame()
{
back.InitBack(m_hIns);
plane.InitPlayerPlane(m_hIns);
::SetTimer(m_hMainWnd,BACK_MOVE_TIMER_ID,50,NULL);
::SetTimer(m_hMainWnd,GUNNER_MOVE_TIMER_ID,30,NULL);
::SetTimer(m_hMainWnd,SEND_MOVE_TIMER_ID,300,NULL);
::SetTimer(m_hMainWnd,CREATE_FOEMOVE_TIMER_ID,1000,NULL);
::SetTimer(m_hMainWnd,FOEPLANE_MOVE_TIMER_ID,30,NULL);
::SetTimer(m_hMainWnd,BOOMPLANE_MOVE_TIMER_ID,30,NULL);
}
void CPlaneCtrl::OnGameDraw()
{
HDC dc=::GetDC(m_hMainWnd );
HDC tempDC=::CreateCompatibleDC(dc);//创建一个兼容性的tempdc
HBITMAP hbitmap=::CreateCompatibleBitmap (dc,380,550);
//导入TODO
::SelectObject (tempDC,hbitmap);
back.ShowBack (tempDC);
plane.PlayerPlaneShow (tempDC);
AllGunnerShow(tempDC);
AllFoePlaneShow(tempDC);
AllBoomFoePlaneShow(tempDC);
::BitBlt (dc,0,0,380,550,tempDC,0,0,SRCCOPY );
DeleteObject (hbitmap);
DeleteDC (tempDC);
ReleaseDC (m_hMainWnd,dc);
}
void CPlaneCtrl::OnGameRun(WPARAM nTimeID)
{
if(nTimeID==BACK_MOVE_TIMER_ID)
{
back.BackMove();
OnGameDraw();
}
if(nTimeID==GUNNER_MOVE_TIMER_ID)
{
AllGunnerMove();
this->GunnerHitFoePlane();
this->OnGameDraw();
}
if(nTimeID==SEND_MOVE_TIMER_ID)
{
plane.SendGunner(m_lstGunner, m_hIns);
this->OnGameDraw();
}
if(nTimeID==CREATE_FOEMOVE_TIMER_ID)
{
this->CreateFoePlane();
this->OnGameDraw();
}
if(nTimeID==FOEPLANE_MOVE_TIMER_ID)
{
AllFoePlaneMove();
if(GameOver()==true)
{
OnCloseGame();
::MessageBox(NULL,"游戏失败","提示",MB_OK);
}
this->OnGameDraw();
}
if(nTimeID==BOOMPLANE_MOVE_TIMER_ID)
{
Change();
this->OnGameDraw();
}
}
void CPlaneCtrl::OnKeyDown(WPARAM nKeyChar)
{
PlaneMoveFlag =false;
while(PlaneMoveFlag ==false)
{
MSG msg;
::GetMessage(&msg,NULL,0,0);
if(msg.message!=WM_KEYDOWN)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
plane.PlayerPlaneMove(nKeyChar);
this->OnGameDraw();
}
}
void CPlaneCtrl::OnKeyUp(WPARAM nKeyChar)
{
PlaneMoveFlag =true;
}
void CPlaneCtrl::OnCloseGame()
{
::KillTimer(m_hMainWnd,BACK_MOVE_TIMER_ID);
::KillTimer(m_hMainWnd,GUNNER_MOVE_TIMER_ID);
::KillTimer(m_hMainWnd,SEND_MOVE_TIMER_ID);
::KillTimer(m_hMainWnd,CREATE_FOEMOVE_TIMER_ID);
::KillTimer(m_hMainWnd,FOEPLANE_MOVE_TIMER_ID);
::KillTimer(m_hMainWnd,BOOMPLANE_MOVE_TIMER_ID);
PlaneMoveFlag=true;
}
void CPlaneCtrl::AllGunnerMove()
{
list<CGunner*>::iterator ite=m_lstGunner.begin();
while(ite!=m_lstGunner.end())
{
if((*ite)->y<0)
{
delete(*ite);
ite=m_lstGunner.erase(ite);
}
else
{
(*ite)->GunnerMove();
++ite;
}
}
}
void CPlaneCtrl::AllGunnerShow(HDC hMemDC)
{
list<CGunner*>::iterator ite=m_lstGunner.begin();
while(ite!=m_lstGunner.end())
{
(*ite)->ShowGunner(hMemDC);
++ite;
}
}
void CPlaneCtrl::AllFoePlaneMove()
{
list<CFoePlane*>::iterator ite=m_lstFoePlane.begin();
while(ite!=m_lstFoePlane.end())
{
if((*ite)->y>550)
{
delete(*ite);
ite=m_lstFoePlane.erase(ite);
}
else
{
(*ite)->FoePlaneMove();
++ite;
}
}
}
void CPlaneCtrl::AllFoePlaneShow(HDC hMemDC)
{
list<CFoePlane*>::iterator ite=m_lstFoePlane.begin();
while(ite!=m_lstFoePlane.end())
{
(*ite)->ShowFoePlane(hMemDC);
++ite;
}
}
void CPlaneCtrl::Change()
{
list<CFoePlane*>::iterator iteBoomPlane=m_boomFoePlane.begin();
while(iteBoomPlane!=m_boomFoePlane.end())
{
(*iteBoomPlane)->m_nShowID--;
++iteBoomPlane;
}
}
void CPlaneCtrl::AllBoomFoePlaneShow(HDC hMemDC)
{
list<CFoePlane*>::iterator iteBoomPlane=m_boomFoePlane.begin();
while(iteBoomPlane!=m_boomFoePlane.end())
{
(*iteBoomPlane)->ShowFoePlane(hMemDC);
++iteBoomPlane;
}
}
void CPlaneCtrl::CreateFoePlane()
{
CFoePlane *foeplane=NULL;
int index=rand()%15;
if(index>=1&&index<=3)
{
foeplane=new CBigFoePlane;
}
else if(index>=3&&index<=7)
{
foeplane=new CMidFoePlane;
}
else
{
foeplane=new CSmallFoePlane;
}
foeplane->InitFoePlane(m_hIns);
m_lstFoePlane.push_back(foeplane);
}
void CPlaneCtrl::GunnerHitFoePlane()
{
bool kflag=false;
list<CGunner*>::iterator ite=m_lstGunner.begin();
while(ite!=m_lstGunner.end())
{
kflag=false;
list<CFoePlane*>::iterator iteFoePlane=m_lstFoePlane.begin();
while(iteFoePlane!=m_lstFoePlane.end())
{
if((*iteFoePlane)->IsGunnerHitFoePlane(*ite)==true)
{
kflag=true;
delete (*ite);
ite=m_lstGunner.erase(ite);
(*iteFoePlane)->DownBlood();
if((*iteFoePlane)->IsBoom()==true)
{
m_boomFoePlane.push_back(*iteFoePlane);Change();
iteFoePlane=m_lstFoePlane.erase(iteFoePlane);
}
break;
}
++iteFoePlane;
}
if(kflag==false)
{
ite++;
}
}
}
bool CPlaneCtrl::GameOver()
{
list<CFoePlane*>::iterator ite=m_lstFoePlane.begin();
while(ite!=m_lstFoePlane.end())
{
if((*ite)->IsFoePlanrHitPlayerPlane(plane)==true)
{
return true;
}
else
{
++ite;
}
return false;
}
}
CPlayerPlane.h
#pragma once
#include"公共.h"
#include"Gunner.h"
class CPlayerPlane
{
public:
HBITMAP m_hBmpPlayerPlane;
int m_nGunnerStyle;
int x;
int y;
public:
CPlayerPlane(void);
~CPlayerPlane(void);
void InitPlayerPlane(HINSTANCE hIns);
void PlayerPlaneMove(int FX);
void PlayerPlaneShow(HDC hMemDC);
void SendGunner(list<CGunner*>& lstGunner,HINSTANCE hIns);
};
CPlayerPlane.cpp
#include "PlayerPlane.h"
CPlayerPlane::CPlayerPlane(void)
{
m_hBmpPlayerPlane=0;
m_nGunnerStyle=0;
x=160;
y=490;
}
CPlayerPlane::~CPlayerPlane(void)
{
::DeleteObject(m_hBmpPlayerPlane);
}
void CPlayerPlane::InitPlayerPlane(HINSTANCE hIns)
{
m_hBmpPlayerPlane=::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_PLANE));
}
void CPlayerPlane::PlayerPlaneMove(int FX)
{
if(FX==VK_UP)
{
if(y>0)
{
y--;
}
}
if(FX==VK_DOWN)
{
if(y<490)
{
y++;
}
}
if(FX==VK_LEFT)
{
if(x>0)
{
x--;
}
}
if(FX==VK_RIGHT)
{
if(x<320)
{
x++;
}
}
}
void CPlayerPlane::PlayerPlaneShow(HDC hMemDC)
{
HDC hTemp=::CreateCompatibleDC(hMemDC);
::SelectObject(hTemp,m_hBmpPlayerPlane);
::BitBlt(hMemDC,x,y,60,60,hTemp,0,0,SRCAND);
::DeleteDC(hTemp);
}
void CPlayerPlane::SendGunner(list<CGunner*>& lstGunner,HINSTANCE hIns)
{
if(m_nGunnerStyle==0)
{
CGunner *gunner=new CGunner;
gunner->InitGunner(hIns,x+27,y-9);
lstGunner.push_back(gunner);
}
}