提交 9989a700 编辑于 作者: CjhGm's avatar CjhGm
浏览文件

Add myplane crashes enemy

上级 6c0fc6f8
...@@ -48,9 +48,6 @@ BOOL CAircraftBattleDoc::OnNewDocument() ...@@ -48,9 +48,6 @@ BOOL CAircraftBattleDoc::OnNewDocument()
return TRUE; return TRUE;
} }
// CAircraftBattleDoc 序列化 // CAircraftBattleDoc 序列化
void CAircraftBattleDoc::Serialize(CArchive& ar) void CAircraftBattleDoc::Serialize(CArchive& ar)
......
...@@ -294,9 +294,9 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent) ...@@ -294,9 +294,9 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent)
while (bulletPos != NULL) { while (bulletPos != NULL) {
tmpBulletPos = bulletPos; tmpBulletPos = bulletPos;
CBullet* bullet = (CBullet*)bulletList.GetNext(bulletPos); CBullet* bullet = (CBullet*)bulletList.GetNext(bulletPos);
POSITION enemyPos = enemyList.GetHeadPosition(), tmpEnemtPos = enemyPos; POSITION enemyPos = enemyList.GetHeadPosition(), tmpEnemyPos = enemyPos;
while (enemyPos != NULL) { while (enemyPos != NULL) {
tmpEnemtPos = enemyPos; tmpEnemyPos = enemyPos;
CEnemy* enemy = (CEnemy*)enemyList.GetNext(enemyPos); CEnemy* enemy = (CEnemy*)enemyList.GetNext(enemyPos);
CRect tmpRect; CRect tmpRect;
if (tmpRect.IntersectRect(&(bullet->GetRect()), &(enemy->GetRect()))) { if (tmpRect.IntersectRect(&(bullet->GetRect()), &(enemy->GetRect()))) {
...@@ -306,7 +306,7 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent) ...@@ -306,7 +306,7 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent)
delete bullet; delete bullet;
bullet = NULL; bullet = NULL;
if (!enemy->isAlive()) { if (!enemy->isAlive()) {
enemyList.RemoveAt(tmpEnemtPos); enemyList.RemoveAt(tmpEnemyPos);
delete enemy; delete enemy;
enemy = NULL; enemy = NULL;
} }
...@@ -314,6 +314,22 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent) ...@@ -314,6 +314,22 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent)
} }
} }
} }
// 战机撞到敌机
POSITION enemyPos = enemyList.GetHeadPosition(), tmpEnemyPos = enemyPos;
while (enemyPos != NULL) {
tmpEnemyPos = enemyPos;
CEnemy* enemy = (CEnemy*)enemyList.GetNext(enemyPos);
CRect tmpRect;
if (tmpRect.IntersectRect(&(myplane->GetRect()), &(enemy->GetRect()))) {
// 战机和敌机区域有重合,即战机撞到敌机
myplane->decreaseHp(10 * enemy->getDamage());
enemyList.RemoveAt(tmpEnemyPos);
delete enemy;
enemy = NULL;
break;
}
}
} }
//将二级缓冲cdc中的数据推送到一级级缓冲pDC中,即输出到屏幕中 //将二级缓冲cdc中的数据推送到一级级缓冲pDC中,即输出到屏幕中
...@@ -326,13 +342,14 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent) ...@@ -326,13 +342,14 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent)
ReleaseDC(pDC); ReleaseDC(pDC);
CView::OnTimer(nIDEvent); CView::OnTimer(nIDEvent);
} }
//键盘按下监听 //键盘按下监听
void CAircraftBattleView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) void CAircraftBattleView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{ {
//按空格进入游戏 //按空格进入游戏
if (isStarted == FALSE) { if (isStarted == false) {
if (GetKeyState(VK_SPACE) < 0) { if (GetKeyState(VK_SPACE) < 0) {
isStarted = TRUE; isStarted = true;
} }
} }
else { else {
......
...@@ -2,36 +2,16 @@ ...@@ -2,36 +2,16 @@
#include "CEnemy.h" #include "CEnemy.h"
#include "resource.h" #include "resource.h"
//静态图像列表 //静态图像列表
CImageList CEnemy::enemyImages; CImageList CEnemy::enemyImages;
CEnemy::CEnemy(int window_width, int window_height) : hp(ENEMY_HP), speed(ENEMY_SPEED) { CEnemy::CEnemy(int window_width, int window_height) : CPlane(ENEMY_HP, ENEMY_DAMAGE, ENEMY_SPEED) {
mPoint.x = rand() % window_width; mPoint.x = rand() % window_width;
mPoint.y = 0; mPoint.y = 0;
} }
int CEnemy::getHp() const {
return hp;
}
void CEnemy::decreaseHp(int x) {
hp = max(0, hp - x);
}
bool CEnemy::isAlive() const {
return hp > 0;
}
void CEnemy::setHp(int _hp) {
hp = _hp;
}
//绘制当前敌机位置 //绘制当前敌机位置
BOOL CEnemy::Draw(CDC* pDC, int passNum, BOOL bPause) BOOL CEnemy::Draw(CDC* pDC, int passNum, BOOL bPause) {
{
if (bPause == 0) { if (bPause == 0) {
int index = passNum % 5; int index = passNum % 5;
//敌机位置随机变化,只改变纵坐标,随机数为了让敌机不匀速飞行 //敌机位置随机变化,只改变纵坐标,随机数为了让敌机不匀速飞行
...@@ -43,8 +23,7 @@ BOOL CEnemy::Draw(CDC* pDC, int passNum, BOOL bPause) ...@@ -43,8 +23,7 @@ BOOL CEnemy::Draw(CDC* pDC, int passNum, BOOL bPause)
else return FALSE; else return FALSE;
} }
BOOL CEnemy::Draw(CDC* pDC, BOOL bPause) BOOL CEnemy::Draw(CDC* pDC, BOOL bPause) {
{
if (bPause == 0) { if (bPause == 0) {
//敌机位置随机变化,只改变纵坐标,随机数为了让敌机不匀速飞行 //敌机位置随机变化,只改变纵坐标,随机数为了让敌机不匀速飞行
//+ rand() % 10 //+ rand() % 10
...@@ -56,8 +35,7 @@ BOOL CEnemy::Draw(CDC* pDC, BOOL bPause) ...@@ -56,8 +35,7 @@ BOOL CEnemy::Draw(CDC* pDC, BOOL bPause)
} }
//加载图像 //加载图像
BOOL CEnemy::LoadImage() BOOL CEnemy::LoadImage() {
{
//加载游戏对象的图标对象 //加载游戏对象的图标对象
CBitmap enemyBmp; CBitmap enemyBmp;
if (!enemyBmp.LoadBitmapW(IDB_BMP_ENEMYDOWN)) if (!enemyBmp.LoadBitmapW(IDB_BMP_ENEMYDOWN))
...@@ -71,7 +49,6 @@ BOOL CEnemy::LoadImage() ...@@ -71,7 +49,6 @@ BOOL CEnemy::LoadImage()
return TRUE; return TRUE;
} }
CRect CEnemy::GetRect() CRect CEnemy::GetRect() {
{
return CRect(mPoint, CPoint(mPoint.x + ENEMY_WIDTH, mPoint.y + ENEMY_HEIGHT)); return CRect(mPoint, CPoint(mPoint.x + ENEMY_WIDTH, mPoint.y + ENEMY_HEIGHT));
} }
\ No newline at end of file
#pragma once #pragma once
#include "GameObject.h" #include "GameObject.h"
#include "CPlane.h"
//敌机的高度 #define ENEMY_HEIGHT 90 // 敌机的高度
#define ENEMY_HEIGHT 90 #define ENEMY_WIDTH 120 // 敌机的宽度
//敌机的宽度 #define ENEMY_SPEED 3 // 敌机飞行速度
#define ENEMY_WIDTH 120 #define ENEMY_HP 1 // 敌机血量
//敌机飞行速度 #define ENEMY_DAMAGE 1 // 敌机伤害
#define ENEMY_SPEED 3
//敌机血量
#define ENEMY_HP 1
class CEnemy : public CGameObject class CEnemy : public CPlane
{ {
private:
int hp;
int speed;
protected: protected:
static CImageList enemyImages; //敌机图像 static CImageList enemyImages; //敌机图像
public: public:
CEnemy(int window_width, int window_heigh); CEnemy(int window_width, int window_heigh);
int getHp() const;
void decreaseHp(int x);
bool isAlive() const;
void setHp(int _hp);
CRect GetRect(); CRect GetRect();
......
#include "stdafx.h"
#include "CPlane.h"
CPlane::CPlane(int _hp, int _damage, int _speed) : hp(_hp), damage(_damage), speed(_speed) {}
int CPlane::getHp() const {
return hp;
}
int CPlane::getDamage() const {
return damage;
}
int CPlane::getSpeed() const {
return speed;
}
void CPlane::decreaseHp(int x) {
hp = max(0, hp - x);
}
bool CPlane::isAlive() const {
return hp > 0;
}
\ No newline at end of file
#pragma once
#include "GameObject.h"
class CPlane : public CGameObject {
protected:
int hp;
int damage;
int speed;
public:
CPlane(int _hp, int _damage, int _speed);
int getHp() const;
int getSpeed() const;
int getDamage() const;
void decreaseHp(int x);
bool isAlive() const;
};
...@@ -10,20 +10,15 @@ CImageList CMyPlane::images; ...@@ -10,20 +10,15 @@ CImageList CMyPlane::images;
CImageList CMyPlane::images1; CImageList CMyPlane::images1;
CImageList CMyPlane::imagespro; CImageList CMyPlane::imagespro;
CMyPlane::CMyPlane(bool isUpdate, int _damage, int _hp) { CMyPlane::CMyPlane(bool isUpdate): CPlane(PLANE_HP, PLANE_DAMAGE, PLANE_SPEED) {
index = isUpdate ? 1 : 0; index = isUpdate ? 1 : 0;
mPoint.x = PLANE_X; mPoint.x = PLANE_X;
mPoint.y = PLANE_Y; mPoint.y = PLANE_Y;
progress = 0; progress = 0;
damage = _damage;
hp = _hp;
} }
CMyPlane::~CMyPlane(void) {} CMyPlane::~CMyPlane(void) {}
int CMyPlane::getDamage() const {
return damage;
}
//绘制战机 //绘制战机
BOOL CMyPlane::Draw(CDC* pDC, BOOL bPause, BOOL isProtect) BOOL CMyPlane::Draw(CDC* pDC, BOOL bPause, BOOL isProtect)
......
#pragma once #pragma once
#include "GameObject.h" #include "GameObject.h"
#include "CPlane.h"
#define PLANE_WIDTH 120 // 升级战机图像宽 #define PLANE_WIDTH 120 // 升级战机图像宽
#define PLANE_HEIGHT 150 // 升级战机图像高 #define PLANE_HEIGHT 150 // 升级战机图像高
...@@ -9,24 +10,17 @@ ...@@ -9,24 +10,17 @@
#define PLANE_PRO_HEIGHT 166 // 保护罩战机图像高 #define PLANE_PRO_HEIGHT 166 // 保护罩战机图像高
#define PLANE_X 400 // 战机初始位置X #define PLANE_X 400 // 战机初始位置X
#define PLANE_Y 300 // 战机初始位置Y #define PLANE_Y 300 // 战机初始位置Y
//#define PLANE_SPEED 30 // 战机默认速度 #define PLANE_SPEED 30 // 战机默认速度
#define PLANE_DAMAGE 1 // 战机默认伤害 #define PLANE_DAMAGE 1 // 战机默认伤害
#define PLANE_HP 10 // 战机默认生命 #define PLANE_HP 10 // 战机默认生命
//战机类 //战机类
class CMyPlane :public CGameObject class CMyPlane :public CPlane
{ {
private:
int damage;
int hp;
public: public:
CMyPlane(bool isUpdate = false, int _damage = PLANE_DAMAGE, int _hp = PLANE_HP); CMyPlane(bool isUpdate = false);
~CMyPlane(void); ~CMyPlane(void);
int getSpeed() const;
int getDamage() const;
//绘制 //绘制
BOOL Draw(CDC* pDC, BOOL bPause, BOOL isProtect); BOOL Draw(CDC* pDC, BOOL bPause, BOOL isProtect);
BOOL Draw(CDC* pDC, BOOL bPause); BOOL Draw(CDC* pDC, BOOL bPause);
......
...@@ -194,6 +194,7 @@ ...@@ -194,6 +194,7 @@
<ClInclude Include="CBullet.h" /> <ClInclude Include="CBullet.h" />
<ClInclude Include="CEnemy.h" /> <ClInclude Include="CEnemy.h" />
<ClInclude Include="ClassView.h" /> <ClInclude Include="ClassView.h" />
<ClInclude Include="CPlane.h" />
<ClInclude Include="FileView.h" /> <ClInclude Include="FileView.h" />
<ClInclude Include="GameObject.h" /> <ClInclude Include="GameObject.h" />
<ClInclude Include="MainFrm.h" /> <ClInclude Include="MainFrm.h" />
...@@ -215,6 +216,7 @@ ...@@ -215,6 +216,7 @@
<ClCompile Include="CBullet.cpp" /> <ClCompile Include="CBullet.cpp" />
<ClCompile Include="CEnemy.cpp" /> <ClCompile Include="CEnemy.cpp" />
<ClCompile Include="ClassView.cpp" /> <ClCompile Include="ClassView.cpp" />
<ClCompile Include="CPlane.cpp" />
<ClCompile Include="FileView.cpp" /> <ClCompile Include="FileView.cpp" />
<ClCompile Include="GameObject.cpp" /> <ClCompile Include="GameObject.cpp" />
<ClCompile Include="MainFrm.cpp" /> <ClCompile Include="MainFrm.cpp" />
......
...@@ -75,6 +75,9 @@ ...@@ -75,6 +75,9 @@
<ClInclude Include="AircraftBattleDoc.h"> <ClInclude Include="AircraftBattleDoc.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="CPlane.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="stdafx.cpp"> <ClCompile Include="stdafx.cpp">
...@@ -128,6 +131,9 @@ ...@@ -128,6 +131,9 @@
<ClCompile Include="AircraftBattleView.cpp"> <ClCompile Include="AircraftBattleView.cpp">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="CPlane.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="PlaneWar.rc"> <ResourceCompile Include="PlaneWar.rc">
......
Supports Markdown
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册