back.h
#pragma once
#include "Sys.h"
class CBack
{
public:
CBack(void);
~CBack(void);
public:
HBITMAP m_hBmpBackDown;// 图片
HBITMAP m_hBmpBackUp;
int x;
int y;
public:
void InitBack(HINSTANCE hIns); // 初始化背景
void ShowBack(HDC hMemDC); // 显示背景
void MoveBack(); // 背景移动
};
BigFoePlane.h
#pragma once
#include "foeplane.h"
class CBigFoePlane :
public CFoePlane
{
public:
CBigFoePlane(void);
~CBigFoePlane(void);
public:
virtual bool IsGunnerHitFoePlane(CGunner* pGunner);
virtual bool IsHitPlayer(CPlayer& plane);
virtual void InitFoePlane(HINSTANCE hIns);
virtual void MoveFoePlane();
virtual void ShowFoePlane(HDC hMemDC);
};
BlastFoe.h
#pragma once
#include "FoePlane.h"
class CBlastFoePlaneBox
{
public:
CBlastFoePlaneBox(void);
~CBlastFoePlaneBox(void);
public:
list<CFoePlane*> m_lstBlastFoePlane; // 爆炸的敌人飞机
public:
void AllBlastFoePlaneShow(HDC hMemDC);
void ChangeShowID();
};
FoePlane.h
#pragma once
#include "Sys.h"
#include "Gunner.h"
#include "Player.h"
class CFoePlane
{
public:
CFoePlane(void);
virtual ~CFoePlane(void);
public:
HBITMAP m_hBmpFoePlane;
int x;
int y;
int m_nBlood; // 血量
int m_nShowID; // 显示图片中哪一块
public:
virtual bool IsGunnerHitFoePlane(CGunner* pGunner)=0;
virtual bool IsHitPlayer(CPlayer& plane)=0;
virtual void InitFoePlane(HINSTANCE hIns)=0;
virtual void MoveFoePlane()=0;
virtual void ShowFoePlane(HDC hMemDC)=0;
public:
bool IsBoom();
void DownBlood();
};
FoePlaneBox.h
#pragma once
#include "FoePlane.h"
class CFoePlaneBox
{
public:
CFoePlaneBox(void);
~CFoePlaneBox(void);
public:
list<CFoePlane*> m_lstFoePlane; // 装敌人飞机的链表
public:
void CreateFoePlane(HINSTANCE hIns); // 创建敌人飞机
void AllFoePlaneMove();
void AllFoePlaneShow(HDC hMemDC);
};
GameApp.h
#pragma once
#include <windows.h>
#define DECLARE(ThisClass)\
CGameApp* CreateObject()\
{\
return new ThisClass;\
}
class CGameApp
{
public:
HINSTANCE m_hIns; // 实例句柄
HWND m_hMainWnd; // 主窗口
public:
CGameApp()
{
m_hIns = 0;
m_hMainWnd = 0;
}
virtual ~CGameApp()
{
}
public:
void SetHandle(HINSTANCE hIns,HWND hWnd)
{
m_hIns = hIns;
m_hMainWnd = hWnd;
}
public:
virtual void OnCreateGame() // WM_CREATE
{
}
virtual void OnGameDraw() // WM_PAINT
{
}
virtual void OnGameRun(WPARAM nTimerID) // WM_TIMER
{
}
virtual void OnKeyDown(WPARAM nKey) // WM_KEYDOWN
{
}
virtual void OnKeyUp(WPARAM nKey) // WM_KEYUP
{
}
virtual void OnLButtonDown(POINT point) // WM_LBUTTONDOWN
{
}
virtual void OnLButtonUp(POINT point) // WM_LBUTTONUP
{
}
virtual void OnMouseMove(POINT point) // WM_MOUSEMOVE
{
}
};
Gunner.h
#pragma once
#include "Sys.h"
class CGunner
{
public:
CGunner(void);
~CGunner(void);
public:
HBITMAP m_hBmpGunner;
int x;
int y;
public:
void InitGunner(HINSTANCE hIns, int x, int y);
void MoveGunner();
void ShowGunner(HDC hMemDC);
};
GunnerBox.h
#pragma once
#include "Sys.h"
#include "Gunner.h"
class CGunnerBox
{
public:
CGunnerBox(void);
~CGunnerBox(void);
public:
list<CGunner*> m_lstGunner; // 装炮弹的链表
public:
void AllGunnerMove();
void AllGunnerShow(HDC hdc);
};
MidFoePlane.h
#pragma once
#include "foeplane.h"
class CMidFoePlane :
public CFoePlane
{
public:
CMidFoePlane(void);
~CMidFoePlane(void);
public:
virtual bool IsGunnerHitFoePlane(CGunner* pGunner);
virtual bool IsHitPlayer(CPlayer& plane);
virtual void InitFoePlane(HINSTANCE hIns);
virtual void MoveFoePlane();
virtual void ShowFoePlane(HDC hMemDC);
};
PlaneApp.h
#pragma once
#include "gameapp.h"
#include "Back.h"
#include "Player.h"
#include "GunnerBox.h"
#include "FoePlaneBox.h"
#include "BlastFoe.h"
class CPlaneApp :
public CGameApp
{
public:
CBack back; // 背景
CPlayer plane; // 玩家
CGunnerBox gunbox; // 装炮弹的盒子
CFoePlaneBox foebox; // 装敌人飞机的盒子
CBlastFoePlaneBox blastbox; // 装爆炸的敌人飞机
public:
CPlaneApp(void);
~CPlaneApp(void);
public:
virtual void OnCreateGame(); // WM_CREATE
virtual void OnGameDraw(); // WM_PAINT
virtual void OnGameRun(WPARAM nTimerID); // WM_TIMER
virtual void OnKeyDown(WPARAM nKey); // WM_KEYDOWN
public:
void GunnerHitFoePlane(); // 炮弹打敌人飞机
bool IsGameOver(); // 游戏结束
};
Player.h
#pragma once
#include "Sys.h"
#include "GunnerBox.h"
class CPlayer
{
public:
CPlayer(void);
~CPlayer(void);
public:
HBITMAP m_hBmpPlayer;
int x;
int y;
public:
void InitPlayer(HINSTANCE hIns);
void MovePlayer(int FX);
void ShowPlayer(HDC hdc);
void SendGunner(HINSTANCE hIns, CGunnerBox& gunBox);
};
SmallFoePlane.h
#pragma once
#include "foeplane.h"
class CSmallFoePlane :
public CFoePlane
{
public:
CSmallFoePlane(void);
~CSmallFoePlane(void);
public:
virtual bool IsGunnerHitFoePlane(CGunner* pGunner);
virtual bool IsHitPlayer(CPlayer& plane);
virtual void InitFoePlane(HINSTANCE hIns);
virtual void MoveFoePlane();
virtual void ShowFoePlane(HDC hMemDC);
};
Sys.h
#pragma once
#include <windows.h>
#include <list>
#include "resource.h"
using namespace std;
#define BACK_MOVE_TIMER_ID 1
#define PLAYER_MOVE_TIMER_ID 2
#define SEND_GUNNER_TIMER_ID 3
#define GUNNER_MOVE_TIMER_ID 4
#define CREATE_FOEPLANE_TIMER_ID 5
#define FOEPLANE_MOVE_TIMER_ID 6
#define CHANGE_SHOWID_TIMER_ID 7
Back.cpp
#include "Back.h"
CBack::CBack(void)
{
m_hBmpBackDown = 0;
m_hBmpBackUp = 0;
x = 0;
y = 0;
}
CBack::~CBack(void)
{
// 删除图片
::DeleteObject(m_hBmpBackDown);
::DeleteObject(m_hBmpBackUp);
m_hBmpBackDown = 0;
m_hBmpBackUp = 0;
}
void CBack::InitBack(HINSTANCE hIns) // 初始化背景
{
// 加载资源
m_hBmpBackDown = ::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_BACK));
m_hBmpBackUp = ::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_BACK));
}
void CBack::ShowBack(HDC hMemDC) // 显示背景
{
HDC hdc = ::CreateCompatibleDC(hMemDC);
::SelectObject(hdc,m_hBmpBackUp);
::BitBlt(hMemDC,x,y-550,380,550,hdc,0,0,SRCCOPY);
::SelectObject(hdc,m_hBmpBackDown);
::BitBlt(hMemDC,x,y,380,550,hdc,0,0,SRCCOPY);
::DeleteDC(hdc);
}
void CBack::MoveBack() // 背景移动
{
if(y > 550)
y = 0;
else
y++;
}
BigFoePlane.cpp
#include "BigFoePlane.h"
CBigFoePlane::CBigFoePlane(void)
{
m_nBlood = 5;
// 随机一个位置
x = rand()%(380-108);
y = -128;
m_nShowID = 3;
}
CBigFoePlane::~CBigFoePlane(void)
{
}
// 炮弹打敌人飞机
bool CBigFoePlane::IsGunnerHitFoePlane(CGunner* pGunner)
{
if(pGunner->x >= this->x && pGunner->x <= this->x+108
&& pGunner->y >= this->y && pGunner->y <= this->y+128)
{
return true;
}
return false;
}
// 撞玩家飞机
bool CBigFoePlane::IsHitPlayer(CPlayer& plane)
{
// x+30,y
if(plane.x+30 >= this->x && plane.x+30 <= this->x+108
&& plane.y >= this->y && plane.y <= this->y+128)
return true;
// x+60,y+50
if(plane.x+60 >= this->x && plane.x+60 <= this->x+108
&& plane.y+50 >= this->y && plane.y+50 <= this->y+128)
return true;
// x,y+50
if(plane.x >= this->x && plane.x <= this->x+108
&& plane.y+50 >= this->y && plane.y+50 <= this->y+128)
return true;
return false;
}
void CBigFoePlane::InitFoePlane(HINSTANCE hIns)
{
// 加载位图
m_hBmpFoePlane = ::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_BIG));
}
void CBigFoePlane::MoveFoePlane()
{
y+=2;
}
void CBigFoePlane::ShowFoePlane(HDC hMemDC)
{
HDC hdc = ::CreateCompatibleDC(hMemDC);
::SelectObject(hdc,m_hBmpFoePlane);
::BitBlt(hMemDC,x,y,108,128,hdc,0,(3-m_nShowID)*128,SRCAND);
::DeleteDC(hdc);
}
BlastFoe.cpp
#include "BlastFoe.h"
CBlastFoePlaneBox::CBlastFoePlaneBox(void)
{
}
CBlastFoePlaneBox::~CBlastFoePlaneBox(void)
{
list<CFoePlane*>::iterator ite = m_lstBlastFoePlane.begin();
while(ite != m_lstBlastFoePlane.end())
{
delete (*ite);
ite = m_lstBlastFoePlane.erase(ite);
}
}
void CBlastFoePlaneBox::AllBlastFoePlaneShow(HDC hMemDC)
{
list<CFoePlane*>::iterator ite = m_lstBlastFoePlane.begin();
while(ite != m_lstBlastFoePlane.end())
{
(*ite)->ShowFoePlane(hMemDC);
++ite;
}
}
void CBlastFoePlaneBox::ChangeShowID()
{
list<CFoePlane*>::iterator ite = m_lstBlastFoePlane.begin();
while(ite != m_lstBlastFoePlane.end())
{
// 改变 ShowID
if( (*ite)->m_nShowID == 0)
{
// 爆炸效果显示完 删除这个敌人飞机
delete (*ite);
ite = m_lstBlastFoePlane.erase(ite);
}
else
{
(*ite)->m_nShowID--;
++ite;
}
}
}
FoePlane.cpp
#include "FoePlane.h"
CFoePlane::CFoePlane(void)
{
m_hBmpFoePlane = 0;
x = 0;
y = 0;
m_nBlood = 0;
m_nShowID = 0;
}
CFoePlane::~CFoePlane(void)
{
::DeleteObject(m_hBmpFoePlane);
m_hBmpFoePlane = 0;
}
bool CFoePlane::IsBoom()
{
// 判断是否爆炸
if(m_nBlood == 0)
return true;
else
return false;
}
void CFoePlane::DownBlood()
{
m_nBlood--;
}
FoePlaneBox.cpp
#include "FoePlaneBox.h"
#include "BigFoePlane.h"
#include "MidFoePlane.h"
#include "SmallFoePlane.h"
CFoePlaneBox::CFoePlaneBox(void)
{
}
CFoePlaneBox::~CFoePlaneBox(void)
{
// 删除链表中所有的敌人飞机
list<CFoePlane*>::iterator ite = m_lstFoePlane.begin();
while(ite != m_lstFoePlane.end())
{
delete(*ite);
ite = m_lstFoePlane.erase(ite);
}
}
void CFoePlaneBox::CreateFoePlane(HINSTANCE hIns) // 创建敌人飞机
{
// 创建一个敌人飞机对象
int index = rand()%12;
CFoePlane* pFoePlane = 0;
if(index >= 0 && index <= 1)
pFoePlane = new CBigFoePlane;
else if(index >= 2 && index <= 5)
pFoePlane = new CMidFoePlane;
else
pFoePlane = new CSmallFoePlane;
// 初始化这个敌人飞机
pFoePlane->InitFoePlane(hIns);
// 添加到链表中
m_lstFoePlane.push_back(pFoePlane);
}
void CFoePlaneBox::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)->MoveFoePlane();
++ite;
}
}
}
void CFoePlaneBox::AllFoePlaneShow(HDC hMemDC)
{
// 显示所有的敌人飞机
list<CFoePlane*>::iterator ite = m_lstFoePlane.begin();
while(ite != m_lstFoePlane.end())
{
(*ite)->ShowFoePlane(hMemDC);
++ite;
}
}
Gunner.cpp
#include "Gunner.h"
CGunner::CGunner(void)
{
m_hBmpGunner = 0;
x = 0;
y = 0;
}
CGunner::~CGunner(void)
{
::DeleteObject(m_hBmpGunner);
m_hBmpGunner = 0;
}
void CGunner::InitGunner(HINSTANCE hIns, int x1, int y1)
{
// 初始化位置
x = x1;
y = y1;
// 加载图
m_hBmpGunner = ::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_GUNNER));
}
void CGunner::MoveGunner()
{
y-=10;
}
void CGunner::ShowGunner(HDC hMemDC)
{
HDC hdc = ::CreateCompatibleDC(hMemDC);
::SelectObject(hdc,m_hBmpGunner);
::BitBlt(hMemDC,x,y,6,9,hdc,0,0,SRCAND);
::DeleteDC(hdc);
}
GunnerBox.cpp
#include "GunnerBox.h"
CGunnerBox::CGunnerBox(void)
{
}
CGunnerBox::~CGunnerBox(void)
{
// 删除所有的炮弹对象
list<CGunner*>::iterator ite = m_lstGunner.begin();
while(ite != m_lstGunner.end())
{
delete (*ite); // 删除对象
ite = m_lstGunner.erase(ite); // 删除节点
}
}
void CGunnerBox::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)->MoveGunner();
++ite;
}
}
}
void CGunnerBox::AllGunnerShow(HDC hdc)
{
// 所有炮弹的显示
list<CGunner*>::iterator ite = m_lstGunner.begin();
while(ite != m_lstGunner.end())
{
(*ite)->ShowGunner(hdc); // 显示炮弹
++ite;
}
}
MidFoePlane.cpp
#include "MidFoePlane.h"
CMidFoePlane::CMidFoePlane(void)
{
m_nBlood = 3;
// 随机一个位置
x = rand()%(380-70);
y = -90;
m_nShowID = 2;
}
CMidFoePlane::~CMidFoePlane(void)
{
}
// 炮弹打敌人飞机
bool CMidFoePlane::IsGunnerHitFoePlane(CGunner* pGunner)
{
if(pGunner->x >= this->x && pGunner->x <= this->x+70
&& pGunner->y >= this->y && pGunner->y <= this->y+90)
{
return true;
}
return false;
}
// 撞玩家飞机
bool CMidFoePlane::IsHitPlayer(CPlayer& plane)
{
// x+30,y
if(plane.x+30 >= this->x && plane.x+30 <= this->x+70
&& plane.y >= this->y && plane.y <= this->y+90)
return true;
// x+60,y+50
if(plane.x+60 >= this->x && plane.x+60 <= this->x+70
&& plane.y+50 >= this->y && plane.y+50 <= this->y+90)
return true;
// x,y+50
if(plane.x >= this->x && plane.x <= this->x+70
&& plane.y+50 >= this->y && plane.y+50 <= this->y+90)
return true;
return false;
}
void CMidFoePlane::InitFoePlane(HINSTANCE hIns)
{
// 加载位图
m_hBmpFoePlane = ::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_MID));
}
void CMidFoePlane::MoveFoePlane()
{
y+=4;
}
void CMidFoePlane::ShowFoePlane(HDC hMemDC)
{
HDC hdc = ::CreateCompatibleDC(hMemDC);
::SelectObject(hdc,m_hBmpFoePlane);
::BitBlt(hMemDC,x,y,70,90,hdc,0,(2-m_nShowID)*90,SRCAND);
::DeleteDC(hdc);
}
PlaneApp.cpp
#include "PlaneApp.h"
DECLARE(CPlaneApp)
CPlaneApp::CPlaneApp(void)
{
}
CPlaneApp::~CPlaneApp(void)
{
}
void CPlaneApp::OnCreateGame() // WM_CREATE
{
// 1. 初始化背景
back.InitBack(m_hIns);
// 2. 初始化玩家飞机
plane.InitPlayer(m_hIns);
// 3. 启动所有的定时器
::SetTimer(m_hMainWnd,BACK_MOVE_TIMER_ID,100,0); // 控制背景移动的定时器
::SetTimer(m_hMainWnd,PLAYER_MOVE_TIMER_ID,1,0); // 控制玩家的移动
::SetTimer(m_hMainWnd,SEND_GUNNER_TIMER_ID,350,0); // 控制玩家发炮弹
::SetTimer(m_hMainWnd,GUNNER_MOVE_TIMER_ID,10,0); //控制所有炮弹移动
::SetTimer(m_hMainWnd,CREATE_FOEPLANE_TIMER_ID,1000,0); // 控制创建敌人飞机
::SetTimer(m_hMainWnd,FOEPLANE_MOVE_TIMER_ID,30,0); // 控制敌人飞机移动
::SetTimer(m_hMainWnd,CHANGE_SHOWID_TIMER_ID,150,0);
}
void CPlaneApp::OnGameDraw() // WM_PAINT
{
PAINTSTRUCT ps = {0};
HDC hdc = ::BeginPaint(m_hMainWnd,&ps);
HDC hMemDC = ::CreateCompatibleDC(hdc);
HBITMAP hBitmap = ::CreateCompatibleBitmap(hdc,380,550);
::SelectObject(hMemDC,hBitmap);
// ------------------------------------------------
back.ShowBack(hMemDC); // 显示背景
plane.ShowPlayer(hMemDC); // 显示玩家飞机
gunbox.AllGunnerShow(hMemDC); // 显示所有炮弹
foebox.AllFoePlaneShow(hMemDC); // 显示所有敌人飞机
blastbox.AllBlastFoePlaneShow(hMemDC); // 显示爆炸的敌人飞机
::BitBlt(hdc,0,0,380,550,hMemDC,0,0,SRCCOPY);
// ------------------------------------------------
::DeleteObject(hBitmap);
::DeleteDC(hMemDC);
::EndPaint(m_hMainWnd,&ps);
}
void CPlaneApp::OnGameRun(WPARAM nTimerID) // WM_TIMER
{
if(nTimerID == CHANGE_SHOWID_TIMER_ID)
blastbox.ChangeShowID();
if(nTimerID == CREATE_FOEPLANE_TIMER_ID) // 创建敌人飞机
foebox.CreateFoePlane(m_hIns);
if(nTimerID == FOEPLANE_MOVE_TIMER_ID) // 所有敌人飞机移动
{
foebox.AllFoePlaneMove();
if(this->IsGameOver() == true)
{
::KillTimer(m_hMainWnd,BACK_MOVE_TIMER_ID); // 控制背景移动的定时器
::KillTimer(m_hMainWnd,PLAYER_MOVE_TIMER_ID); // 控制玩家的移动
::KillTimer(m_hMainWnd,SEND_GUNNER_TIMER_ID); // 控制玩家发炮弹
::KillTimer(m_hMainWnd,GUNNER_MOVE_TIMER_ID); //控制所有炮弹移动
::KillTimer(m_hMainWnd,CREATE_FOEPLANE_TIMER_ID); // 控制创建敌人飞机
::KillTimer(m_hMainWnd,FOEPLANE_MOVE_TIMER_ID); // 控制敌人飞机移动
::KillTimer(m_hMainWnd,CHANGE_SHOWID_TIMER_ID);
MessageBox(0,"GameOVer","提示",MB_OK);
}
}
if(nTimerID == SEND_GUNNER_TIMER_ID) // 玩家飞机发射炮弹
plane.SendGunner(m_hIns,gunbox);
if(nTimerID == GUNNER_MOVE_TIMER_ID) // 所有炮弹移动
{
gunbox.AllGunnerMove();
this->GunnerHitFoePlane(); // 判断炮弹打敌人飞机
}
if(nTimerID == BACK_MOVE_TIMER_ID) // 背景移动
back.MoveBack();
if(PLAYER_MOVE_TIMER_ID == nTimerID) // 玩家移动
{
if(::GetAsyncKeyState(VK_UP)) // 获取键盘的状态
plane.MovePlayer(VK_UP);
if(::GetAsyncKeyState(VK_DOWN)) // 获取键盘的状态
plane.MovePlayer(VK_DOWN);
if(::GetAsyncKeyState(VK_LEFT)) // 获取键盘的状态
plane.MovePlayer(VK_LEFT);
if(::GetAsyncKeyState(VK_RIGHT)) // 获取键盘的状态
plane.MovePlayer(VK_RIGHT);
}
// 重绘
RECT rect = {0,0,380,550};
::InvalidateRect(m_hMainWnd,&rect,FALSE);
}
void CPlaneApp::OnKeyDown(WPARAM nKey) // WM_KEYDOWN
{
if(nKey == VK_SPACE)
{
blastbox.m_lstBlastFoePlane.splice(blastbox.m_lstBlastFoePlane.end(),
foebox.m_lstFoePlane);
}
玩家飞机移动
//plane.MovePlayer(nKey);
重绘
//RECT rect = {0,0,380,550};
//::InvalidateRect(m_hMainWnd,&rect,FALSE);
}
void CPlaneApp::GunnerHitFoePlane()
{
bool bflag = false; // 标记炮弹是否打中敌人飞机
//---- 遍历 炮弹盒子里的所有炮弹----
list<CGunner*>::iterator iteGun = gunbox.m_lstGunner.begin();
while(iteGun != gunbox.m_lstGunner.end())
{
//---- 遍历所有的敌人飞机------
list<CFoePlane*>::iterator iteFoe = foebox.m_lstFoePlane.begin();
while(iteFoe != foebox.m_lstFoePlane.end())
{
// 判断iteGun这个炮弹是否打中iteFoe敌人飞机
if( (*iteFoe)->IsGunnerHitFoePlane(*iteGun) == true )
{
bflag = true;
// 删除这个炮弹
delete (*iteGun);
iteGun = gunbox.m_lstGunner.erase(iteGun);
// 敌人飞机掉血
(*iteFoe)->DownBlood();
// 判断是否爆炸
if((*iteFoe)->IsBoom() == true)
{
//把这个敌人飞机放到另一个链表中显示爆炸效果
blastbox.m_lstBlastFoePlane.push_back(*iteFoe);
// 删除这个节点
iteFoe = foebox.m_lstFoePlane.erase(iteFoe);
}
break;
}
iteFoe++;
}
//-------------------
if(bflag == false)
++iteGun;
else
bflag = false;
}
}
bool CPlaneApp::IsGameOver()
{
list<CFoePlane*>::iterator ite = foebox.m_lstFoePlane.begin();
while(ite != foebox.m_lstFoePlane.end())
{
// 判断 是否撞到玩家飞机
if((*ite)->IsHitPlayer(plane) == true)
return true;
++ite;
}
return false;
}
Player.cpp
#include "Player.h"
CPlayer::CPlayer(void)
{
m_hBmpPlayer = 0;
x = 160; // 初始化玩家飞机的位置
y = 490;
}
CPlayer::~CPlayer(void)
{
::DeleteObject(m_hBmpPlayer);
m_hBmpPlayer = 0;
}
void CPlayer::InitPlayer(HINSTANCE hIns)
{
// 加载位图
m_hBmpPlayer = ::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_PLAYER));
}
void CPlayer::MovePlayer(int FX)
{
if(FX == VK_UP)
{
if(y > 0)
y-=2;
}
if(FX == VK_DOWN)
{
if(y < 490)
y+=2;
}
if(FX == VK_LEFT)
{
if(x > 0)
x-=2;
}
if(FX == VK_RIGHT)
{
if(x < 320)
x+=2;
}
}
void CPlayer::ShowPlayer(HDC hdc)
{
HDC hMemDC = ::CreateCompatibleDC(hdc);
::SelectObject(hMemDC,m_hBmpPlayer);
::BitBlt(hdc,x,y,60,60,hMemDC,0,0,SRCAND);
::DeleteDC(hMemDC);
}
void CPlayer::SendGunner(HINSTANCE hIns, CGunnerBox& gunBox)
{
// 创建炮弹对象
CGunner* pGunner = new CGunner;
// 初始化炮弹
pGunner->InitGunner(hIns,x+27,y-9);
// 放到盒子里
gunBox.m_lstGunner.push_back(pGunner);
}
SmallFoePlane.cpp
#include "SmallFoePlane.h"
CSmallFoePlane::CSmallFoePlane(void)
{
m_nBlood = 1;
// 随机一个位置
x = rand()%(380-34);
y = -28;
m_nShowID = 1;
}
CSmallFoePlane::~CSmallFoePlane(void)
{
}
// 炮弹打敌人飞机
bool CSmallFoePlane::IsGunnerHitFoePlane(CGunner* pGunner)
{
if(pGunner->x >= this->x && pGunner->x <= this->x+34
&& pGunner->y >= this->y && pGunner->y <= this->y+28)
{
return true;
}
return false;
}
// 撞玩家飞机
bool CSmallFoePlane::IsHitPlayer(CPlayer& plane)
{
// x+30,y
if(plane.x+30 >= this->x && plane.x+30 <= this->x+34
&& plane.y >= this->y && plane.y <= this->y+28)
return true;
// x+60,y+50
if(plane.x+60 >= this->x && plane.x+60 <= this->x+34
&& plane.y+50 >= this->y && plane.y+50 <= this->y+28)
return true;
// x,y+50
if(plane.x >= this->x && plane.x <= this->x+34
&& plane.y+50 >= this->y && plane.y+50 <= this->y+28)
return true;
return false;
}
void CSmallFoePlane::InitFoePlane(HINSTANCE hIns)
{
// 加载位图
m_hBmpFoePlane = ::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_SMALL));
}
void CSmallFoePlane::MoveFoePlane()
{
y+=6;
}
void CSmallFoePlane::ShowFoePlane(HDC hMemDC)
{
HDC hdc = ::CreateCompatibleDC(hMemDC);
::SelectObject(hdc,m_hBmpFoePlane);
::BitBlt(hMemDC,x,y,34,28,hdc,0,(1-m_nShowID)*28,SRCAND);
::DeleteDC(hdc);
}
WinMain.cpp
#include <windows.h>
#include "GameApp.h"
#include <time.h>
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
HINSTANCE hIns = 0;
int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR lpCmdLine,int nShowCmd)
{
srand((unsigned int)time(0));
//-----------------------------------------------------
HWND hwnd = 0;
MSG msg; // 装消息的结构体
WNDCLASSEX wndclass;
//-----------------------------------------------------
hIns = hInstance;
//----------------------创建窗口过程-----------------------------------
// 1. 设计
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.cbSize = sizeof(wndclass);
wndclass.hbrBackground = (HBRUSH)COLOR_WINDOW;
wndclass.hCursor = LoadCursor(0,MAKEINTRESOURCE(IDC_ARROW));
wndclass.hIcon = 0;
wndclass.hIconSm = 0;
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WindowProc;
wndclass.lpszClassName = "lele";
wndclass.lpszMenuName = 0;
wndclass.style = CS_HREDRAW|CS_VREDRAW;
// 2. 注册
if(RegisterClassEx(&wndclass) == FALSE)
{
MessageBox(0,"注册失败","提示",MB_OK);
return 0;
}
// 3. 创建
hwnd = CreateWindow("lele","O(∩_∩)O哈哈~",WS_OVERLAPPEDWINDOW,0,0,380+16,550+38,0,0,hInstance,0);
if(hwnd == 0)
{
MessageBox(0,"创建失败","提示",MB_OK);
return 0;
}
// 4. 显示窗口
ShowWindow(hwnd,SW_SHOW);
//---------------------------创建窗口过程------------------------------------------------
//----------------------------消息循环-------------------------------------------
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//----------------------------消息循环-------------------------------------------
return 0;
}
//=================================处理消息========================================================
CGameApp* pApp = 0;
CGameApp* CreateObject();
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
{
// 创建一个游戏的对象
pApp = CreateObject();
if(pApp != 0)
{
// 设置句柄的值
pApp->SetHandle(hIns,hwnd);
// 初始化游戏 加载资源
pApp->OnCreateGame();
}
}
break;
case WM_PAINT:
{
if(pApp != 0)
{
// 显示游戏
pApp->OnGameDraw();
}
}
break;
case WM_TIMER:
{
if(pApp != 0)
{
// 游戏运行
pApp->OnGameRun(wParam);
}
}
break;
case WM_KEYDOWN:
{
if(pApp != 0)
{
// 键盘控制
pApp->OnKeyDown(wParam);
}
}
break;
case WM_KEYUP:
{
if(pApp != 0)
{
// 键盘控制
pApp->OnKeyUp(wParam);
}
}
break;
case WM_LBUTTONDOWN:
{
if(pApp != 0)
{
POINT point;
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
// 鼠标控制
pApp->OnLButtonDown(point);
}
}
break;
case WM_LBUTTONUP:
{
if(pApp != 0)
{
POINT point;
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
// 鼠标控制
pApp->OnLButtonUp(point);
}
}
break;
case WM_MOUSEMOVE:
{
if(pApp != 0)
{
POINT point;
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
// 鼠标控制
pApp->OnMouseMove(point);
}
}
break;
case WM_CLOSE:
{
// 删除游戏
delete pApp;
pApp = 0;
PostQuitMessage(0);
}
break;
}
return DefWindowProc( hwnd, uMsg, wParam, lParam);
}