飞机大战项目源码_text文本飞机大战源码-程序员宅基地

技术标签: 游戏  

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);
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_40956876/article/details/106578099

智能推荐

while循环&CPU占用率高问题深入分析与解决方案_main函数使用while(1)循环cpu占用99-程序员宅基地

文章浏览阅读3.8k次,点赞9次,收藏28次。直接上一个工作中碰到的问题,另外一个系统开启多线程调用我这边的接口,然后我这边会开启多线程批量查询第三方接口并且返回给调用方。使用的是两三年前别人遗留下来的方法,放到线上后发现确实是可以正常取到结果,但是一旦调用,CPU占用就直接100%(部署环境是win server服务器)。因此查看了下相关的老代码并使用JProfiler查看发现是在某个while循环的时候有问题。具体项目代码就不贴了,类似于下面这段代码。​​​​​​while(flag) {//your code;}这里的flag._main函数使用while(1)循环cpu占用99

【无标题】jetbrains idea shift f6不生效_idea shift +f6快捷键不生效-程序员宅基地

文章浏览阅读347次。idea shift f6 快捷键无效_idea shift +f6快捷键不生效

node.js学习笔记之Node中的核心模块_node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是-程序员宅基地

文章浏览阅读135次。Ecmacript 中没有DOM 和 BOM核心模块Node为JavaScript提供了很多服务器级别,这些API绝大多数都被包装到了一个具名和核心模块中了,例如文件操作的 fs 核心模块 ,http服务构建的http 模块 path 路径操作模块 os 操作系统信息模块// 用来获取机器信息的var os = require('os')// 用来操作路径的var path = require('path')// 获取当前机器的 CPU 信息console.log(os.cpus._node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是

数学建模【SPSS 下载-安装、方差分析与回归分析的SPSS实现(软件概述、方差分析、回归分析)】_化工数学模型数据回归软件-程序员宅基地

文章浏览阅读10w+次,点赞435次,收藏3.4k次。SPSS 22 下载安装过程7.6 方差分析与回归分析的SPSS实现7.6.1 SPSS软件概述1 SPSS版本与安装2 SPSS界面3 SPSS特点4 SPSS数据7.6.2 SPSS与方差分析1 单因素方差分析2 双因素方差分析7.6.3 SPSS与回归分析SPSS回归分析过程牙膏价格问题的回归分析_化工数学模型数据回归软件

利用hutool实现邮件发送功能_hutool发送邮件-程序员宅基地

文章浏览阅读7.5k次。如何利用hutool工具包实现邮件发送功能呢?1、首先引入hutool依赖<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.19</version></dependency>2、编写邮件发送工具类package com.pc.c..._hutool发送邮件

docker安装elasticsearch,elasticsearch-head,kibana,ik分词器_docker安装kibana连接elasticsearch并且elasticsearch有密码-程序员宅基地

文章浏览阅读867次,点赞2次,收藏2次。docker安装elasticsearch,elasticsearch-head,kibana,ik分词器安装方式基本有两种,一种是pull的方式,一种是Dockerfile的方式,由于pull的方式pull下来后还需配置许多东西且不便于复用,个人比较喜欢使用Dockerfile的方式所有docker支持的镜像基本都在https://hub.docker.com/docker的官网上能找到合..._docker安装kibana连接elasticsearch并且elasticsearch有密码

随便推点

Python 攻克移动开发失败!_beeware-程序员宅基地

文章浏览阅读1.3w次,点赞57次,收藏92次。整理 | 郑丽媛出品 | CSDN(ID:CSDNnews)近年来,随着机器学习的兴起,有一门编程语言逐渐变得火热——Python。得益于其针对机器学习提供了大量开源框架和第三方模块,内置..._beeware

Swift4.0_Timer 的基本使用_swift timer 暂停-程序员宅基地

文章浏览阅读7.9k次。//// ViewController.swift// Day_10_Timer//// Created by dongqiangfei on 2018/10/15.// Copyright 2018年 飞飞. All rights reserved.//import UIKitclass ViewController: UIViewController { ..._swift timer 暂停

元素三大等待-程序员宅基地

文章浏览阅读986次,点赞2次,收藏2次。1.硬性等待让当前线程暂停执行,应用场景:代码执行速度太快了,但是UI元素没有立马加载出来,造成两者不同步,这时候就可以让代码等待一下,再去执行找元素的动作线程休眠,强制等待 Thread.sleep(long mills)package com.example.demo;import org.junit.jupiter.api.Test;import org.openqa.selenium.By;import org.openqa.selenium.firefox.Firefox.._元素三大等待

Java软件工程师职位分析_java岗位分析-程序员宅基地

文章浏览阅读3k次,点赞4次,收藏14次。Java软件工程师职位分析_java岗位分析

Java:Unreachable code的解决方法_java unreachable code-程序员宅基地

文章浏览阅读2k次。Java:Unreachable code的解决方法_java unreachable code

标签data-*自定义属性值和根据data属性值查找对应标签_如何根据data-*属性获取对应的标签对象-程序员宅基地

文章浏览阅读1w次。1、html中设置标签data-*的值 标题 11111 222222、点击获取当前标签的data-url的值$('dd').on('click', function() { var urlVal = $(this).data('ur_如何根据data-*属性获取对应的标签对象

推荐文章

热门文章

相关标签