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

Add bullet hitting enemy

上级 cf5c1bd7
......@@ -4,16 +4,21 @@
CImageList CBullet::bulletImages; // 子弹图像
CBullet::CBullet(int x, int y, int _speed) {
CBullet::CBullet(int x, int y, int _speed, int _damage) {
mPoint.x = x;
mPoint.y = y;
speed = _speed;
damage = _damage;
}
int CBullet::getSpeed() const {
return speed;
}
int CBullet::getDamage() const {
return damage;
}
CRect CBullet::GetRect() {
return CRect(mPoint, CPoint(mPoint.x + BULLET_WIDTH, mPoint.y + BULLET_HEIGHT));
}
......
......@@ -10,17 +10,22 @@
//子弹飞行速度
#define BULLET_SPEED 30
// 子弹杀伤力
#define BULLET_DAMAGE 1
class CBullet : public CGameObject
{
private:
int speed;
int damage;
protected:
static CImageList bulletImages; // 子弹图像
public:
CBullet(int x = 0, int y = 0, int _speed = BULLET_SPEED);
CBullet(int x = 0, int y = 0, int _speed = BULLET_SPEED, int _damage = BULLET_DAMAGE);
int getSpeed() const;
int getDamage() const;
CRect GetRect();
......
......@@ -3,14 +3,6 @@
#include "resource.h"
//敌机的高度
#define ENEMY_HEIGHT 90
//敌机的宽度
#define ENEMY_WIDTH 120
//敌机飞行速度
#define ENEMY_SPEED 3
//敌机血量
#define ENEMY_HP 2
//静态图像列表
......@@ -25,6 +17,10 @@ int CEnemy::getHp() const {
return hp;
}
void CEnemy::decreaseHp(int x) {
hp = max(0, hp - x);
}
bool CEnemy::isAlive() const {
return hp > 0;
}
......
#pragma once
#include "GameObject.h"
//敌机的高度
#define ENEMY_HEIGHT 90
//敌机的宽度
#define ENEMY_WIDTH 120
//敌机飞行速度
#define ENEMY_SPEED 3
//敌机血量
#define ENEMY_HP 1
class CEnemy : public CGameObject
{
private:
......@@ -13,6 +22,7 @@ protected:
public:
CEnemy(int window_width, int window_heigh);
int getHp() const;
void decreaseHp(int x);
bool isAlive() const;
void setHp(int _hp);
......
......@@ -272,8 +272,7 @@ void CPlaneWarView::OnTimer(UINT_PTR nIDEvent)
myplane->Draw(&cdc, FALSE, isProtect);
}
//随机添加敌机,敌机随机发射炸弹,此时敌机速度与数量和关卡有关
if (myplane != NULL && isPause == 0 && isBoss == FALSE)
{
if (myplane != NULL && isPause == 0 && isBoss == FALSE) {
//敌机产生定时器触发
if (nIDEvent == 2) {
CEnemy* enemy = new CEnemy(rect.right, rect.bottom);
......@@ -283,19 +282,16 @@ void CPlaneWarView::OnTimer(UINT_PTR nIDEvent)
//超出边界的敌机进行销毁
POSITION stPos = NULL, tPos = NULL;
stPos = enemyList.GetHeadPosition();
while (stPos != NULL)
{
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)
{
|| enemy->GetPoint().y<rect.top || enemy->GetPoint().y>rect.bottom) {
enemyList.RemoveAt(tPos);
delete enemy;
}
else
{
else {
//没出界,绘制
enemy->Draw(&cdc, passNum, FALSE);
}
......@@ -307,28 +303,54 @@ void CPlaneWarView::OnTimer(UINT_PTR nIDEvent)
while (stPos != NULL) {
tPos = stPos;
CBullet* bullet = (CBullet*)bulletList.GetNext(stPos);
//判断敌机是否出界
if (bullet->GetPoint().x<rect.left || bullet->GetPoint().x>rect.right
|| bullet->GetPoint().y<rect.top || bullet->GetPoint().y>rect.bottom) {
// 判断敌机是否出界
if (bullet->GetPoint().x < rect.left || bullet->GetPoint().x > rect.right
|| bullet->GetPoint().y < rect.top || bullet->GetPoint().y > rect.bottom) {
bulletList.RemoveAt(tPos);
delete bullet;
}
else {
//没出界,绘制
// 没出界,绘制
bullet->Draw(&cdc, 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);
// 子弹打中敌机
POSITION bulletPos = bulletList.GetHeadPosition(), tmpBulletPos = bulletPos;
while (bulletPos != NULL) {
tmpBulletPos = bulletPos;
CBullet* bullet = (CBullet*)bulletList.GetNext(bulletPos);
POSITION enemyPos = enemyList.GetHeadPosition(), tmpEnemtPos = enemyPos;
while (enemyPos != NULL) {
tmpEnemtPos = enemyPos;
CEnemy* enemy = (CEnemy*)enemyList.GetNext(enemyPos);
CRect tmpRect;
if (tmpRect.IntersectRect(&(bullet->GetRect()), &(enemy->GetRect()))) {
// 子弹和敌机区域有重合,即子弹打中敌机
bulletList.RemoveAt(tmpBulletPos);
enemy->decreaseHp(bullet->getDamage());
delete bullet;
bullet = NULL;
if (!enemy->isAlive()) {
enemyList.RemoveAt(tmpEnemtPos);
delete enemy;
enemy = NULL;
}
break;
}
}
}
}
//将二级缓冲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.
先完成此消息的编辑!
想要评论请 注册