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

add enemy

上级 0e93ef7e
#include "stdafx.h"
#include "CEnemy.h"
#include "resource.h"
//敌机的高度
#define ENEMY_HEIGHT 90
//敌机的宽度
#define ENEMY_WIDTH 120
//敌机飞行速度
#define ENEMY_SPEED 3
//敌机血量
#define ENEMY_HP 2
//静态图像列表
CImageList CEnemy::enemyImages;
CEnemy::CEnemy(int window_width, int window_height) : hp(ENEMY_HP), speed(ENEMY_SPEED) {
mPoint.x = rand() % window_width;
mPoint.y = 0;
}
int CEnemy::getHp() const {
return hp;
}
bool CEnemy::isAlive() const {
return hp > 0;
}
void CEnemy::setHp(int _hp) {
hp = _hp;
}
//绘制当前敌机位置
BOOL CEnemy::Draw(CDC* pDC, int passNum, BOOL bPause)
{
if (bPause == 0) {
int index = passNum % 5;
//敌机位置随机变化,只改变纵坐标,随机数为了让敌机不匀速飞行
//+ rand() % 10
mPoint.y += speed;
enemyImages.Draw(pDC, index, mPoint, ILD_TRANSPARENT);
return TRUE;
}
else return FALSE;
}
BOOL CEnemy::Draw(CDC* pDC, BOOL bPause)
{
if (bPause == 0) {
//敌机位置随机变化,只改变纵坐标,随机数为了让敌机不匀速飞行
//+ rand() % 10
mPoint.y += speed;
enemyImages.Draw(pDC, 0, mPoint, ILD_TRANSPARENT);
return TRUE;
}
else return FALSE;
}
//加载图像
BOOL CEnemy::LoadImage()
{
//加载游戏对象的图标对象
CBitmap enemyBmp;
if (!enemyBmp.LoadBitmapW(IDB_BMP_ENEMYDOWN))
return FALSE;
//创建CImageList对象
if (!enemyImages.Create(ENEMY_WIDTH, ENEMY_HEIGHT, ILC_COLOR24 | ILC_MASK, 5, 0))//nInitial初始个数
return FALSE;//cx,cy 图片的宽度
//图像链表中加入对象对应的图标对象,之后直接通过该链表访问图标对象
enemyImages.Add(&enemyBmp, RGB(0, 0, 0));
return TRUE;
}
CRect CEnemy::GetRect()
{
return CRect(mPoint, CPoint(mPoint.x + ENEMY_HEIGHT, mPoint.y + ENEMY_HEIGHT));
}
\ No newline at end of file
#pragma once
#include "GameObject.h"
class CEnemy : public CGameObject
{
private:
int hp;
int speed;
protected:
static CImageList enemyImages; //敌机图像
public:
CEnemy(int window_width, int window_heigh);
int getHp() const;
bool isAlive() const;
void setHp(int _hp);
CRect GetRect();
//绘制
BOOL Draw(CDC* pDC, BOOL bPause);
BOOL Draw(CDC* pDC, int passNum, BOOL bPause);
//加载敌机图像
static BOOL LoadImage();
};
......@@ -190,6 +190,7 @@
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="CEnemy.h" />
<ClInclude Include="ClassView.h" />
<ClInclude Include="FileView.h" />
<ClInclude Include="GameObject.h" />
......@@ -209,6 +210,7 @@
<ClInclude Include="ViewTree.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="CEnemy.cpp" />
<ClCompile Include="ClassView.cpp" />
<ClCompile Include="FileView.cpp" />
<ClCompile Include="GameObject.cpp" />
......
......@@ -69,6 +69,9 @@
<ClInclude Include="RestartDialog.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CEnemy.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="PlaneWar.cpp">
......@@ -116,6 +119,9 @@
<ClCompile Include="RestartDialog.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CEnemy.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="PlaneWar.rc">
......
......@@ -15,6 +15,7 @@
#include "PlaneWarDoc.h"
#include "PlaneWarView.h"
#include "CEnemy.h"
#ifdef _DEBUG
#define new DEBUG_NEW
......@@ -150,6 +151,7 @@ int CPlaneWarView::OnCreate(LPCREATESTRUCT lpCreateStruct)
//加载游戏对象图片
CMyPlane::LoadImage();
CMyPlane::LoadImageProtect();
CEnemy::LoadImage();
//加载标题图片
CBitmap startbmp;
startbmp.LoadBitmapW(IDB_BMP_TITLE);
......@@ -266,19 +268,48 @@ void CPlaneWarView::OnTimer(UINT_PTR nIDEvent)
if (myplane != NULL) {
myplane->Draw(&cdc, FALSE, isProtect);
}
}
//随机添加敌机,敌机随机发射炸弹,此时敌机速度与数量和关卡有关
if (myplane != NULL && isPause == 0 && isBoss == FALSE)
{
//敌机产生定时器触发
if (nIDEvent == 2) {
CEnemy* enemy = new CEnemy(rect.right, rect.bottom);
enemyList.AddTail(enemy);//随机产生敌机
}
}
//将二级缓冲cdc中的数据推送到一级级缓冲pDC中,即输出到屏幕中
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &cdc, 0, 0, SRCCOPY);
//释放二级cdc
cdc.DeleteDC();
//释放缓冲位图
cacheBitmap->DeleteObject();
//释放一级pDC
ReleaseDC(pDC);
CView::OnTimer(nIDEvent);
}
//超出边界的敌机进行销毁
POSITION stPos = NULL, tPos = NULL;
stPos = enemyList.GetHeadPosition();
while (stPos != NULL)
{
tPos = stPos;
CEnemy* enemy = (CEnemy*)enemyList.GetNext(stPos);
//判断敌机是否出界
if (enemy->GetPoint().x<rect.left || enemy->GetPoint().x>rect.right
|| enemy->GetPoint().y<rect.top || enemy->GetPoint().y>rect.bottom)
{
enemyList.RemoveAt(tPos);
delete enemy;
}
else
{
//没出界,绘制
enemy->Draw(&cdc, passNum, FALSE);
}
}
//将二级缓冲cdc中的数据推送到一级级缓冲pDC中,即输出到屏幕中
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &cdc, 0, 0, SRCCOPY);
//释放二级cdc
cdc.DeleteDC();
//释放缓冲位图
cacheBitmap->DeleteObject();
//释放一级pDC
ReleaseDC(pDC);
CView::OnTimer(nIDEvent);
}
}
//键盘按下监听
void CPlaneWarView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
......
支持 Markdown
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册