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

Add enemy

上级 80e75d8d
...@@ -163,6 +163,7 @@ int CAircraftBattleView::OnCreate(LPCREATESTRUCT lpCreateStruct) ...@@ -163,6 +163,7 @@ int CAircraftBattleView::OnCreate(LPCREATESTRUCT lpCreateStruct)
isStarted = false; isStarted = false;
isPause = false; isPause = false;
isOver = false; isOver = false;
myScore = 0;
SetTimer(4, 40, NULL);//背景滚动定时器 SetTimer(4, 40, NULL);//背景滚动定时器
SetMyTimer(); SetMyTimer();
...@@ -196,7 +197,7 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent) ...@@ -196,7 +197,7 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent)
if (!isStarted && !isOver) { if (!isStarted && !isOver) {
//欢迎界面 //欢迎界面
scene.StickScene(&cdc, -1, rect); scene.StickScene(&cdc, 0, rect);
startIMG.Draw(&cdc, 0, CPoint(rect.right / 2 - 173, 100), ILD_TRANSPARENT); startIMG.Draw(&cdc, 0, CPoint(rect.right / 2 - 173, 100), ILD_TRANSPARENT);
//设置透明背景 //设置透明背景
cdc.SetBkMode(TRANSPARENT); cdc.SetBkMode(TRANSPARENT);
...@@ -233,7 +234,7 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent) ...@@ -233,7 +234,7 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent)
if (myplane != NULL) { if (myplane != NULL) {
myplane->Draw(&cdc, FALSE, FALSE); myplane->Draw(&cdc, FALSE, FALSE);
} }
// 随机添加敌机,敌机随机发射炸弹,此时敌机速度与数量和关卡有关 // 随机添加敌机,敌机随机发射炸弹
if (myplane != NULL && !isPause) { if (myplane != NULL && !isPause) {
// 敌机产生定时器触发 // 敌机产生定时器触发
if (nIDEvent == 2) { if (nIDEvent == 2) {
...@@ -248,8 +249,8 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent) ...@@ -248,8 +249,8 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent)
tPos = stPos; tPos = stPos;
CEnemy* enemy = (CEnemy*)enemyList.GetNext(stPos); CEnemy* enemy = (CEnemy*)enemyList.GetNext(stPos);
// 判断敌机是否出界 // 判断敌机是否出界
if (enemy->GetPoint().x<rect.left || enemy->GetPoint().x>rect.right 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); enemyList.RemoveAt(tPos);
delete enemy; delete enemy;
} }
...@@ -294,6 +295,7 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent) ...@@ -294,6 +295,7 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent)
delete bullet; delete bullet;
bullet = NULL; bullet = NULL;
if (!enemy->isAlive()) { if (!enemy->isAlive()) {
myScore += enemy->getScore();
enemyList.RemoveAt(tmpEnemyPos); enemyList.RemoveAt(tmpEnemyPos);
delete enemy; delete enemy;
enemy = NULL; enemy = NULL;
...@@ -312,6 +314,7 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent) ...@@ -312,6 +314,7 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent)
if (tmpRect.IntersectRect(&(myplane->GetRect()), &(enemy->GetRect()))) { if (tmpRect.IntersectRect(&(myplane->GetRect()), &(enemy->GetRect()))) {
// 战机和敌机区域有重合,即战机撞到敌机 // 战机和敌机区域有重合,即战机撞到敌机
myplane->decreaseHp(2 * enemy->getDamage()); myplane->decreaseHp(2 * enemy->getDamage());
myScore += enemy->getScore();
enemyList.RemoveAt(tmpEnemyPos); enemyList.RemoveAt(tmpEnemyPos);
delete enemy; delete enemy;
enemy = NULL; enemy = NULL;
...@@ -323,27 +326,18 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent) ...@@ -323,27 +326,18 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent)
if (myplane != NULL) if (myplane != NULL)
{ {
HFONT font; HFONT font;
font = CreateFont(15, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 10, 0); font = CreateFont(20, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 10, 0);
cdc.SelectObject(font); cdc.SelectObject(font);
CString str; CString str;
cdc.SetTextColor(RGB(255, 0, 0)); cdc.SetTextColor(RGB(255, 0, 0));
//设置透明背景 //设置透明背景
cdc.SetBkMode(TRANSPARENT); cdc.SetBkMode(TRANSPARENT);
/*str.Format(_T("当前关卡:%d"), passNum);
cdc.TextOutW(10, 0, str);
str.Format(_T("当前命数:%d"), lifeNum);
cdc.TextOutW(110, 0, str);
str.Format(_T("当前得分:%d"), passScore);
cdc.TextOutW(10, 15, str);
if (test == TRUE) {
cdc.TextOutW(10, 200, _T("无敌模式!!!"));
}*/
HFONT font1;
font1 = CreateFont(8, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 10, 0);
cdc.SelectObject(font); cdc.SelectObject(font);
cdc.SetTextColor(RGB(255, 0, 0)); cdc.SetTextColor(RGB(255, 0, 0));
cdc.TextOutW(rect.right - 12 * PLANE_HP - 45, 0, _T("血量:")); cdc.TextOutW(rect.right - 12 * PLANE_HP - 60, 0, _T("血量:"));
str.Format(_T("得分:%d"), myScore);
cdc.TextOutW(rect.right - 12 * PLANE_HP - 60, 20, str);
//输出血条 //输出血条
CBrush brush; CBrush brush;
brush.CreateSolidBrush(RGB(255, 0, 0)); brush.CreateSolidBrush(RGB(255, 0, 0));
...@@ -379,10 +373,10 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent) ...@@ -379,10 +373,10 @@ void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent)
//显示最后结果 //显示最后结果
CString str; CString str;
cdc.TextOutW(rect.right / 2 - 100, rect.bottom / 2 - 30, _T("GAME OVER!")); cdc.TextOutW(rect.right / 2 - 100, rect.bottom / 2 - 30, _T("GAME OVER!"));
//str.Format(_T("您的得分为:%d"), myScore); str.Format(_T("您的得分为:%d"), myScore);
cdc.TextOutW(rect.right / 2 - 100, rect.bottom / 2 - 10, str); cdc.TextOutW(rect.right / 2 - 100, rect.bottom / 2 - 5, str);
cdc.TextOutW(rect.right / 2 - 100, rect.bottom / 2 + 10, _T("不要灰心!再来一次!")); cdc.TextOutW(rect.right / 2 - 100, rect.bottom / 2 + 30, _T("不要灰心!再来一次!"));
cdc.TextOutW(rect.right / 2 - 100, rect.bottom / 2 + 40, _T("是否重新开始?Y/N")); cdc.TextOutW(rect.right / 2 - 100, rect.bottom / 2 + 55, _T("是否重新开始?Y/N"));
} }
//将二级缓冲cdc中的数据推送到一级级缓冲pDC中,即输出到屏幕中 //将二级缓冲cdc中的数据推送到一级级缓冲pDC中,即输出到屏幕中
...@@ -405,7 +399,7 @@ void CAircraftBattleView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) ...@@ -405,7 +399,7 @@ void CAircraftBattleView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
isStarted = true; isStarted = true;
} }
} }
else if (!isOver){ else if (!isOver) {
if (myplane != NULL && GetKeyState(VK_SPACE) < 0) { if (myplane != NULL && GetKeyState(VK_SPACE) < 0) {
// 按空格键发射子弹 // 按空格键发射子弹
CBullet* bullet = new CBullet(myplane->GetPoint().x + PLANE_WIDTH / 2 - BULLET_WIDTH / 2, myplane->GetPoint().y, myplane->getDamage()); CBullet* bullet = new CBullet(myplane->GetPoint().x + PLANE_WIDTH / 2 - BULLET_WIDTH / 2, myplane->GetPoint().y, myplane->getDamage());
...@@ -489,24 +483,14 @@ void CAircraftBattleView::Restart() ...@@ -489,24 +483,14 @@ void CAircraftBattleView::Restart()
//清空敌机链表 //清空敌机链表
if (enemyList.GetCount() > 0) if (enemyList.GetCount() > 0)
enemyList.RemoveAll(); enemyList.RemoveAll();
//清空战机链表
if (meList.GetCount() > 0)
meList.RemoveAll();
//清空战机子弹链表 //清空战机子弹链表
if (bulletList.GetCount() > 0) if (bulletList.GetCount() > 0)
bulletList.RemoveAll(); bulletList.RemoveAll();
//清空敌机炸弹链表
if (ballList.GetCount() > 0)
ballList.RemoveAll();
//清空爆炸链表
if (explosionList.GetCount() > 0)
explosionList.RemoveAll();
//清空血包列表
if (bloodList.GetCount() > 0)
bloodList.RemoveAll();
//参数重新初始化 //参数重新初始化
isPause = false; isPause = false;
isOver = false;
myScore = 0;
//isStarted = FALSE; //isStarted = FALSE;
SetMyTimer(); SetMyTimer();
} }
......
...@@ -41,17 +41,16 @@ public: ...@@ -41,17 +41,16 @@ public:
//创建存储游戏对象的对象链表 //创建存储游戏对象的对象链表
CObList enemyList; CObList enemyList;
CObList meList;
CObList bulletList; CObList bulletList;
CObList ballList;
CObList explosionList;
CObList bloodList;
CRect rect;//窗口屏幕矩形 CRect rect;//窗口屏幕矩形
bool isStarted; // 标记欢迎界面是否加载完成 bool isStarted; // 标记欢迎界面是否加载完成
bool isPause; // 是否暂停 bool isPause; // 是否暂停
bool isOver; // 是否游戏结束 bool isOver; // 是否游戏结束
int myScore; // 游戏得分
CImageList startIMG; CImageList startIMG;
// 操作 // 操作
......
...@@ -8,6 +8,11 @@ CImageList CEnemy::enemyImages; ...@@ -8,6 +8,11 @@ CImageList CEnemy::enemyImages;
CEnemy::CEnemy(int window_width, int window_height) : CPlane(ENEMY_HP, ENEMY_DAMAGE, 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;
score = ENEMY_SCORE;
}
int CEnemy::getScore() const {
return score;
} }
//绘制当前敌机位置 //绘制当前敌机位置
......
...@@ -2,19 +2,23 @@ ...@@ -2,19 +2,23 @@
#include "GameObject.h" #include "GameObject.h"
#include "CPlane.h" #include "CPlane.h"
#define ENEMY_HEIGHT 90 // 敌机的高度 #define ENEMY_HEIGHT 90 // 敌机的高度
#define ENEMY_WIDTH 120 // 敌机的宽度 #define ENEMY_WIDTH 120 // 敌机的宽度
#define ENEMY_SPEED 3 // 敌机飞行速度 #define ENEMY_SPEED 3 // 敌机飞行速度
#define ENEMY_HP 1 // 敌机血量 #define ENEMY_HP 1 // 敌机血量
#define ENEMY_DAMAGE 1 // 敌机伤害 #define ENEMY_DAMAGE 1 // 敌机伤害
#define ENEMY_SCORE 10 // 敌机分数
class CEnemy : public CPlane class CEnemy : public CPlane
{ {
private:
int score;
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 getScore() const;
CRect GetRect(); CRect GetRect();
......
Supports Markdown
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册