AircraftBattleView.cpp 15.1 KB
Newer Older
CjhGm's avatar
CjhGm 已提交
1

CjhGm's avatar
CjhGm 已提交
2
// AircraftBattleView.cpp : CAircraftBattleView 类的实现
CjhGm's avatar
CjhGm 已提交
3
4
5
6
//

#include "stdafx.h"
#include <mmsystem.h>
CjhGm's avatar
CjhGm 已提交
7
#include <iostream>
CjhGm's avatar
CjhGm 已提交
8
9
10
11
12
#pragma comment(lib, "WINMM.LIB")
#include <MMSYSTEM.H>
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
CjhGm's avatar
CjhGm 已提交
13
#include "AircraftBattle.h"
CjhGm's avatar
CjhGm 已提交
14
#endif
CjhGm's avatar
gogogo    
CjhGm 已提交
15

CjhGm's avatar
CjhGm 已提交
16
17
#include "AircraftBattleDoc.h"
#include "AircraftBattleView.h"
CjhGm's avatar
CjhGm 已提交
18
#include "CEnemy.h"
CjhGm's avatar
CjhGm 已提交
19
20
#include "MyPlane.h"
#include "CBullet.h"
CjhGm's avatar
CjhGm 已提交
21
22
23
24

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CjhGm's avatar
CjhGm 已提交
25
#include <cassert>
CjhGm's avatar
CjhGm 已提交
26
27


CjhGm's avatar
CjhGm 已提交
28
// CAircraftBattleView
CjhGm's avatar
CjhGm 已提交
29

CjhGm's avatar
CjhGm 已提交
30
IMPLEMENT_DYNCREATE(CAircraftBattleView, CView)
CjhGm's avatar
CjhGm 已提交
31

CjhGm's avatar
CjhGm 已提交
32
BEGIN_MESSAGE_MAP(CAircraftBattleView, CView)
CjhGm's avatar
CjhGm 已提交
33
34
35
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
CjhGm's avatar
CjhGm 已提交
36
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CAircraftBattleView::OnFilePrintPreview)
CjhGm's avatar
CjhGm 已提交
37
38
39
40
41
42
43
44
45
46
47
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
	ON_WM_CREATE()
	ON_WM_TIMER()
	ON_WM_KEYDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_DESTROY()
	ON_WM_CLOSE()
END_MESSAGE_MAP()

CjhGm's avatar
CjhGm 已提交
48
// CAircraftBattleView 构造/析构
CjhGm's avatar
CjhGm 已提交
49

CjhGm's avatar
CjhGm 已提交
50
CAircraftBattleView::CAircraftBattleView()
CjhGm's avatar
CjhGm 已提交
51
52
53
{
	//默认战机飞行速度、战机生命值、战机得分、关卡、是否过关、是否暂停

CjhGm's avatar
gogogo    
CjhGm 已提交
54
	// TODO: 在此处添加构造代码
CjhGm's avatar
CjhGm 已提交
55
56
}

CjhGm's avatar
CjhGm 已提交
57
CAircraftBattleView::~CAircraftBattleView()
CjhGm's avatar
CjhGm 已提交
58
59
60
{
}

CjhGm's avatar
CjhGm 已提交
61
BOOL CAircraftBattleView::PreCreateWindow(CREATESTRUCT& cs)
CjhGm's avatar
CjhGm 已提交
62
63
64
65
66
67
68
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CView::PreCreateWindow(cs);
}

CjhGm's avatar
CjhGm 已提交
69
70
// CAircraftBattleView 绘制
void CAircraftBattleView::OnDraw(CDC* pDC)
CjhGm's avatar
CjhGm 已提交
71
{
CjhGm's avatar
CjhGm 已提交
72
	CAircraftBattleDoc* pDoc = GetDocument();
CjhGm's avatar
CjhGm 已提交
73
74
75
76
77
78
79
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: 在此处为本机数据添加绘制代码
}

CjhGm's avatar
CjhGm 已提交
80
// CAircraftBattleView 打印
CjhGm's avatar
CjhGm 已提交
81

CjhGm's avatar
CjhGm 已提交
82
void CAircraftBattleView::OnFilePrintPreview()
CjhGm's avatar
CjhGm 已提交
83
84
85
86
87
88
{
#ifndef SHARED_HANDLERS
	AFXPrintPreview(this);
#endif
}

CjhGm's avatar
CjhGm 已提交
89
BOOL CAircraftBattleView::OnPreparePrinting(CPrintInfo* pInfo)
CjhGm's avatar
CjhGm 已提交
90
91
92
93
94
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

CjhGm's avatar
CjhGm 已提交
95
void CAircraftBattleView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
CjhGm's avatar
CjhGm 已提交
96
97
98
99
{
	// TODO: 添加额外的打印前进行的初始化过程
}

CjhGm's avatar
CjhGm 已提交
100
void CAircraftBattleView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
CjhGm's avatar
CjhGm 已提交
101
102
103
104
{
	// TODO: 添加打印后进行的清理过程
}

CjhGm's avatar
CjhGm 已提交
105
void CAircraftBattleView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
CjhGm's avatar
CjhGm 已提交
106
107
108
109
110
111
{
#ifndef SHARED_HANDLERS
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}

CjhGm's avatar
CjhGm 已提交
112
// CAircraftBattleView 诊断
CjhGm's avatar
CjhGm 已提交
113
114

#ifdef _DEBUG
CjhGm's avatar
CjhGm 已提交
115
void CAircraftBattleView::AssertValid() const
CjhGm's avatar
CjhGm 已提交
116
117
118
119
{
	CView::AssertValid();
}

CjhGm's avatar
CjhGm 已提交
120
void CAircraftBattleView::Dump(CDumpContext& dc) const
CjhGm's avatar
CjhGm 已提交
121
122
123
124
{
	CView::Dump(dc);
}

CjhGm's avatar
CjhGm 已提交
125
CAircraftBattleDoc* CAircraftBattleView::GetDocument() const // 非调试版本是内联的
CjhGm's avatar
CjhGm 已提交
126
{
CjhGm's avatar
CjhGm 已提交
127
128
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAircraftBattleDoc)));
	return (CAircraftBattleDoc*)m_pDocument;
CjhGm's avatar
CjhGm 已提交
129
130
131
132
133
}
#endif //_DEBUG

//重写实现

CjhGm's avatar
CjhGm 已提交
134
// CAircraftBattleView 消息处理程序
CjhGm's avatar
CjhGm 已提交
135
//视图创建监听
CjhGm's avatar
CjhGm 已提交
136
int CAircraftBattleView::OnCreate(LPCREATESTRUCT lpCreateStruct)
CjhGm's avatar
CjhGm 已提交
137
138
139
140
141
142
143
144
145
146
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码

	//-----------初始化工作------------
	//加载游戏对象图片
	CMyPlane::LoadImage();
	CMyPlane::LoadImageProtect();
CjhGm's avatar
CjhGm 已提交
147
	CEnemy::LoadImage();
CjhGm's avatar
CjhGm 已提交
148
	CBullet::LoadImage();
CjhGm's avatar
CjhGm 已提交
149
150
151
152
153
	//加载标题图片
	CBitmap startbmp;
	startbmp.LoadBitmapW(IDB_BMP_TITLE);
	startIMG.Create(346, 96, ILC_COLOR24 | ILC_MASK, 1, 0);
	startIMG.Add(&startbmp, RGB(0, 0, 0));
CjhGm's avatar
gogogo    
CjhGm 已提交
154

CjhGm's avatar
CjhGm 已提交
155
	//场景初始化失败
CjhGm's avatar
CjhGm 已提交
156
	if (!scene.InitScene()) {
CjhGm's avatar
CjhGm 已提交
157
158
159
160
161
		AfxMessageBox(L"图片资源加载失败");
		exit(0);
	}

	//参数初始化
CjhGm's avatar
CjhGm 已提交
162
	myplane = new CMyPlane(false);
CjhGm's avatar
CjhGm 已提交
163
164
	isStarted = false;
	isPause = false;
CjhGm's avatar
CjhGm 已提交
165
	isOver = false;
CjhGm's avatar
CjhGm 已提交
166
	myScore = 0;
CjhGm's avatar
CjhGm 已提交
167
168
169
170
171
172
173

	SetTimer(4, 40, NULL);//背景滚动定时器
	SetMyTimer();
	return 0;
}

//计时器监听
CjhGm's avatar
CjhGm 已提交
174
void CAircraftBattleView::OnTimer(UINT_PTR nIDEvent)
CjhGm's avatar
CjhGm 已提交
175
176
177
178
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	//双缓冲
CjhGm's avatar
gogogo    
CjhGm 已提交
179
	CDC* pDC = GetDC();
CjhGm's avatar
CjhGm 已提交
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
	if (pDC == NULL) {
		CView::OnTimer(nIDEvent);
		return;
	}
	//获得客户区矩形区域
	GetClientRect(&rect);
	//内存缓冲CDC
	CDC cdc;

	//内存中承载临时图像的缓冲位图
	CBitmap* cacheBitmap = new CBitmap;
	//用当前设备CDC初始化缓冲CDC
	cdc.CreateCompatibleDC(pDC);
	//绑定pDC和缓冲位图的关系,cdc先输出到缓冲位图中,输出完毕之后再一次性将缓冲位图输出到屏幕
	cacheBitmap->CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
	//替换cdc原本的缓冲区为缓冲位图,这样cdc输出的内容就写到了缓冲位图中
CjhGm's avatar
gogogo    
CjhGm 已提交
196
	CBitmap* pOldBit = cdc.SelectObject(cacheBitmap);
CjhGm's avatar
CjhGm 已提交
197

CjhGm's avatar
CjhGm 已提交
198
	if (!isStarted && !isOver) {
CjhGm's avatar
CjhGm 已提交
199
		//欢迎界面
CjhGm's avatar
CjhGm 已提交
200
		scene.StickScene(&cdc, 0, rect);
CjhGm's avatar
gogogo    
CjhGm 已提交
201
		startIMG.Draw(&cdc, 0, CPoint(rect.right / 2 - 173, 100), ILD_TRANSPARENT);
CjhGm's avatar
CjhGm 已提交
202
203
204
		//设置透明背景
		cdc.SetBkMode(TRANSPARENT);
		HFONT tipFont;
CjhGm's avatar
gogogo    
CjhGm 已提交
205
		tipFont = CreateFont(30, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 10, 0);
CjhGm's avatar
CjhGm 已提交
206
		cdc.SelectObject(tipFont);
CjhGm's avatar
gogogo    
CjhGm 已提交
207
208
209
210
211
212
213
214
215
		cdc.SetTextColor(RGB(0, 0, 0));
		cdc.TextOutW(rect.right / 2 - 200, 210, _T("点击鼠标左键或空格键开始游戏"));
		HFONT textFont;
		textFont = CreateFont(18, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 10, 0);
		cdc.SelectObject(textFont);
		cdc.SetTextColor(RGB(128, 128, 0));
		const int space = 30, off = 180;
		cdc.TextOutW(rect.right / 2 - off, 210 + space, _T("方向控制:方向键、ASDW、鼠标"));
		cdc.TextOutW(rect.right / 2 - off, 210 + 2 * space, _T("射击:空格键、鼠标左键"));
CjhGm's avatar
CjhGm 已提交
216
217
218
219
220
		cdc.TextOutW(rect.right / 2 - off, 210 + 3 * space, _T("暂停:Q键"));
		cdc.TextOutW(rect.right / 2 - off, 210 + 4 * space, _T("战机升级:V键"));
		cdc.TextOutW(rect.right / 2 - off, 210 + 5 * space, _T("无敌模式:Y键"));
		cdc.TextOutW(rect.right / 2 - off, 210 + 6 * space, _T("初始生命值:10"));
		cdc.TextOutW(rect.right / 2 - off, 210 + 7 * space, _T("敌机生命值:2"));
CjhGm's avatar
gogogo    
CjhGm 已提交
221
222
223
224
		cdc.TextOutW(rect.right / 2 - off, 210 + 11 * space, _T("消灭一个敌机加1分,如果分数达到要求即可进入Boss模式,打赢Boss即可进入下一关。"));
		cdc.TextOutW(rect.right / 2 - off, 210 + 12 * space, _T("魔法值随着游戏进程增加,可通过使用魔法值使用防护罩、战机升级、战机大招的使用。"));
		cdc.TextOutW(rect.right / 2 - off, 210 + 13 * space, _T("游戏过程中会有一定程度的血包出现以恢复生命值。"));
		cdc.TextOutW(rect.right / 2 - off, 210 + 14 * space, _T("随着关卡增多,敌机、炮弹速度和数量均增加,通过10关即可通关!"));
CjhGm's avatar
CjhGm 已提交
225
	}
CjhGm's avatar
CjhGm 已提交
226
	else if (isStarted && !isOver) {
CjhGm's avatar
CjhGm 已提交
227
		// 游戏界面
CjhGm's avatar
CjhGm 已提交
228
		scene.StickScene(&cdc, 1, rect);
CjhGm's avatar
CjhGm 已提交
229
230
231
232
		if (nIDEvent == 4) {
			//滚动背景
			scene.MoveBg();
		}
CjhGm's avatar
CjhGm 已提交
233
		// 刷新显示战机
CjhGm's avatar
CjhGm 已提交
234
		if (myplane != NULL) {
CjhGm's avatar
CjhGm 已提交
235
			myplane->Draw(&cdc, FALSE, FALSE);
CjhGm's avatar
CjhGm 已提交
236
		}
CjhGm's avatar
CjhGm 已提交
237
		// 随机添加敌机,敌机随机发射炸弹
CjhGm's avatar
CjhGm 已提交
238
239
		if (myplane != NULL && !isPause) {
			// 敌机产生定时器触发
CjhGm's avatar
CjhGm 已提交
240
241
			if (nIDEvent == 2) {
				CEnemy* enemy = new CEnemy(rect.right, rect.bottom);
CjhGm's avatar
CjhGm 已提交
242
				enemyList.AddTail(enemy);	//随机产生敌机
CjhGm's avatar
CjhGm 已提交
243
244
			}
		}
CjhGm's avatar
CjhGm 已提交
245
		// 超出边界的敌机进行销毁
CjhGm's avatar
CjhGm 已提交
246
247
		POSITION stPos = NULL, tPos = NULL;
		stPos = enemyList.GetHeadPosition();
CjhGm's avatar
CjhGm 已提交
248
		while (stPos != NULL) {
CjhGm's avatar
CjhGm 已提交
249
250
			tPos = stPos;
			CEnemy* enemy = (CEnemy*)enemyList.GetNext(stPos);
CjhGm's avatar
CjhGm 已提交
251
			// 判断敌机是否出界
CjhGm's avatar
CjhGm 已提交
252
253
			if (enemy->GetPoint().x < rect.left || enemy->GetPoint().x > rect.right
				|| enemy->GetPoint().y < rect.top || enemy->GetPoint().y > rect.bottom) {
CjhGm's avatar
CjhGm 已提交
254
255
256
				enemyList.RemoveAt(tPos);
				delete enemy;
			}
CjhGm's avatar
CjhGm 已提交
257
			else {
CjhGm's avatar
CjhGm 已提交
258
				//没出界,绘制
CjhGm's avatar
CjhGm 已提交
259
				enemy->Draw(&cdc, 1, FALSE);
CjhGm's avatar
CjhGm 已提交
260
261
			}
		}
CjhGm's avatar
CjhGm 已提交
262

CjhGm's avatar
CjhGm 已提交
263
264
265
266
267
268
		// 发射子弹,超出边界的子弹进行销毁
		stPos = NULL, tPos = NULL;
		stPos = bulletList.GetHeadPosition();
		while (stPos != NULL) {
			tPos = stPos;
			CBullet* bullet = (CBullet*)bulletList.GetNext(stPos);
CjhGm's avatar
CjhGm 已提交
269
270
271
			// 判断敌机是否出界
			if (bullet->GetPoint().x < rect.left || bullet->GetPoint().x > rect.right
				|| bullet->GetPoint().y < rect.top || bullet->GetPoint().y > rect.bottom) {
CjhGm's avatar
CjhGm 已提交
272
273
274
275
				bulletList.RemoveAt(tPos);
				delete bullet;
			}
			else {
CjhGm's avatar
CjhGm 已提交
276
				// 没出界,绘制
CjhGm's avatar
CjhGm 已提交
277
278
279
280
				bullet->Draw(&cdc, FALSE);
			}
		}

CjhGm's avatar
CjhGm 已提交
281
282
283
284
285
		// 子弹打中敌机
		POSITION bulletPos = bulletList.GetHeadPosition(), tmpBulletPos = bulletPos;
		while (bulletPos != NULL) {
			tmpBulletPos = bulletPos;
			CBullet* bullet = (CBullet*)bulletList.GetNext(bulletPos);
CjhGm's avatar
CjhGm 已提交
286
			POSITION enemyPos = enemyList.GetHeadPosition(), tmpEnemyPos = enemyPos;
CjhGm's avatar
CjhGm 已提交
287
			while (enemyPos != NULL) {
CjhGm's avatar
CjhGm 已提交
288
				tmpEnemyPos = enemyPos;
CjhGm's avatar
CjhGm 已提交
289
290
291
292
293
294
295
296
297
				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()) {
CjhGm's avatar
CjhGm 已提交
298
						myScore += enemy->getScore();
CjhGm's avatar
CjhGm 已提交
299
						enemyList.RemoveAt(tmpEnemyPos);
CjhGm's avatar
CjhGm 已提交
300
301
302
303
304
305
306
						delete enemy;
						enemy = NULL;
					}
					break;
				}
			}
		}
CjhGm's avatar
CjhGm 已提交
307
308
309
310
311
312
313
314
315

		// 战机撞到敌机
		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()))) {
				// 战机和敌机区域有重合,即战机撞到敌机
CjhGm's avatar
CjhGm 已提交
316
				myplane->decreaseHp(2 * enemy->getDamage());
CjhGm's avatar
CjhGm 已提交
317
				myScore += enemy->getScore();
CjhGm's avatar
CjhGm 已提交
318
319
320
321
322
323
				enemyList.RemoveAt(tmpEnemyPos);
				delete enemy;
				enemy = NULL;
				break;
			}
		}
CjhGm's avatar
CjhGm 已提交
324
325
326
327
328

		//游戏界面输出该游戏当前信息
		if (myplane != NULL)
		{
			HFONT font;
CjhGm's avatar
CjhGm 已提交
329
			font = CreateFont(20, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 10, 0);
CjhGm's avatar
CjhGm 已提交
330
331
332
333
334
			cdc.SelectObject(font);
			CString str;
			cdc.SetTextColor(RGB(255, 0, 0));
			//设置透明背景
			cdc.SetBkMode(TRANSPARENT);
CjhGm's avatar
CjhGm 已提交
335

CjhGm's avatar
CjhGm 已提交
336
337
			cdc.SelectObject(font);
			cdc.SetTextColor(RGB(255, 0, 0));
CjhGm's avatar
CjhGm 已提交
338
339
340
			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);
CjhGm's avatar
CjhGm 已提交
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
			//输出血条
			CBrush brush;
			brush.CreateSolidBrush(RGB(255, 0, 0));
			CBrush* oldBrush = cdc.SelectObject(&brush);
			int leftPos, topPos = 0, rightPos, buttomPos = 12;
			leftPos = rect.right - 12 * PLANE_HP;
			rightPos = leftPos + 12 * myplane->getHp();
			cdc.Rectangle(leftPos, topPos, rightPos, buttomPos);
			brush.DeleteObject();

			//输出血条中的详细血值
			HFONT textFont;
			textFont = CreateFont(12, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 10, 0);
			cdc.SelectObject(textFont);
			cdc.SetTextColor(RGB(255, 255, 255));
			str.Format(_T("%d/%d"), myplane->getHp(), PLANE_HP);
			cdc.TextOutW(rect.right - 12 * PLANE_HP + 12 * 4, 0, str);
		}

		if (myplane != NULL && !myplane->isAlive()) {
			gameOver(pDC, cdc, cacheBitmap);
			isOver = true;
		}
	}
	else if (isOver) {
		// 游戏结束
		HFONT textFont;
		textFont = CreateFont(18, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 10, 0);
		cdc.SelectObject(textFont);
		//设置透明背景
		cdc.SetBkMode(TRANSPARENT);
		cdc.SetTextColor(RGB(255, 255, 255));
		//显示最后结果
		CString str;
		cdc.TextOutW(rect.right / 2 - 100, rect.bottom / 2 - 30, _T("GAME OVER!"));
CjhGm's avatar
CjhGm 已提交
376
377
378
379
		str.Format(_T("您的得分为:%d"), myScore);
		cdc.TextOutW(rect.right / 2 - 100, rect.bottom / 2 - 5, str);
		cdc.TextOutW(rect.right / 2 - 100, rect.bottom / 2 + 30, _T("不要灰心!再来一次!"));
		cdc.TextOutW(rect.right / 2 - 100, rect.bottom / 2 + 55, _T("是否重新开始?Y/N"));
CjhGm's avatar
CjhGm 已提交
380
	}
CjhGm's avatar
CjhGm 已提交
381
382
383
384
385
386
387
388
389
390

	//将二级缓冲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);
CjhGm's avatar
CjhGm 已提交
391
}
CjhGm's avatar
CjhGm 已提交
392

CjhGm's avatar
CjhGm 已提交
393
//键盘按下监听
CjhGm's avatar
CjhGm 已提交
394
void CAircraftBattleView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
CjhGm's avatar
CjhGm 已提交
395
396
{
	//按空格进入游戏
CjhGm's avatar
CjhGm 已提交
397
	if (!isStarted) {
CjhGm's avatar
CjhGm 已提交
398
		if (GetKeyState(VK_SPACE) < 0) {
CjhGm's avatar
CjhGm 已提交
399
			isStarted = true;
CjhGm's avatar
CjhGm 已提交
400
401
		}
	}
CjhGm's avatar
CjhGm 已提交
402
	else if (!isOver) {
CjhGm's avatar
CjhGm 已提交
403
404
		if (myplane != NULL && GetKeyState(VK_SPACE) < 0) {
			// 按空格键发射子弹
CjhGm's avatar
CjhGm 已提交
405
			CBullet* bullet1, * bullet2, * bullet3;
CjhGm's avatar
CjhGm 已提交
406
			bullet1 = bullet2 = bullet3 = nullptr;
CjhGm's avatar
CjhGm 已提交
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
			if (myScore < 100) {
				bullet1 = new CBullet(myplane->GetPoint().x + PLANE_WIDTH / 2 - BULLET_WIDTH / 2, myplane->GetPoint().y, myplane->getDamage(), 0, 30, true);
				bulletList.AddTail(bullet1);
			}
			else if (myScore < 300) {
				bullet1 = new CBullet(myplane->GetPoint().x + PLANE_WIDTH / 2 - BULLET_WIDTH / 2 - 15, myplane->GetPoint().y, myplane->getDamage(), 0, 30, true);
				bullet2 = new CBullet(myplane->GetPoint().x + PLANE_WIDTH / 2 - BULLET_WIDTH / 2 + 15, myplane->GetPoint().y, myplane->getDamage(), 0, 30, true);
				bulletList.AddTail(bullet1);
				bulletList.AddTail(bullet2);
			}
			else {
				bullet1 = new CBullet(myplane->GetPoint().x + PLANE_WIDTH / 2 - BULLET_WIDTH / 2, myplane->GetPoint().y, myplane->getDamage(), 0, 30, true);
				bullet2 = new CBullet(myplane->GetPoint().x + PLANE_WIDTH / 2 - BULLET_WIDTH / 2 - 10, myplane->GetPoint().y, myplane->getDamage(), 10, 30, true);
				bullet3 = new CBullet(myplane->GetPoint().x + PLANE_WIDTH / 2 - BULLET_WIDTH / 2 + 10, myplane->GetPoint().y, myplane->getDamage(), -10, 30, true);
				bulletList.AddTail(bullet1);
				bulletList.AddTail(bullet2);
				bulletList.AddTail(bullet3);
CjhGm's avatar
CjhGm 已提交
424
			}
CjhGm's avatar
CjhGm 已提交
425
		}
CjhGm's avatar
CjhGm 已提交
426
	}
CjhGm's avatar
CjhGm 已提交
427
428
429
430
431
432
433
434
435
436
437
	else {
		assert(isOver);
		if (GetKeyState('Y') < 0) {
			isOver = false;
			Restart();
		}
		else if (GetKeyState('N') < 0) {
			MyDialog dlg;
			dlg.DoModal();
		}
	}
CjhGm's avatar
CjhGm 已提交
438
439
440
441
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

//鼠标移动监听,控制战机位置
CjhGm's avatar
CjhGm 已提交
442
void CAircraftBattleView::OnMouseMove(UINT nFlags, CPoint point)
CjhGm's avatar
CjhGm 已提交
443
444
445
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

CjhGm's avatar
gogogo    
CjhGm 已提交
446
	if (myplane != NULL && isPause == 0) {
CjhGm's avatar
CjhGm 已提交
447
		//绘制新游戏对象
CjhGm's avatar
CjhGm 已提交
448
		myplane->SetPoint(point.x - PLANE1_WIDTH / 2, point.y - PLANE1_HEIGHT / 2);
CjhGm's avatar
CjhGm 已提交
449
450
451
452
453
	}
	CView::OnMouseMove(nFlags, point);
}

//按下鼠标左键监听
CjhGm's avatar
CjhGm 已提交
454
void CAircraftBattleView::OnLButtonDown(UINT nFlags, CPoint point)
CjhGm's avatar
CjhGm 已提交
455
456
457
458
459
460
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	if (isStarted == FALSE) {
		isStarted = TRUE;
	}
CjhGm's avatar
CjhGm 已提交
461
	else if (myplane != NULL) {
CjhGm's avatar
CjhGm 已提交
462
		// 按鼠标左键发射子弹
CjhGm's avatar
CjhGm 已提交
463
		CBullet* bullet = new CBullet(myplane->GetPoint().x + PLANE_WIDTH / 2 - BULLET_WIDTH / 2, myplane->GetPoint().y, myplane->getDamage(), 0, 30, true);
CjhGm's avatar
CjhGm 已提交
464
465
		bulletList.AddTail(bullet);
	}
CjhGm's avatar
CjhGm 已提交
466
467
468
469
470

	CView::OnLButtonDown(nFlags, point);
}

//鼠标右键监听
CjhGm's avatar
CjhGm 已提交
471
void CAircraftBattleView::OnRButtonUp(UINT /* nFlags */, CPoint point)
CjhGm's avatar
CjhGm 已提交
472
{
CjhGm's avatar
CjhGm 已提交
473
474
475
476
	if (isStarted == TRUE && myplane != NULL) {
		// 按鼠标右键发射炮弹
		// MyTODO
	}
CjhGm's avatar
CjhGm 已提交
477
478
479
480
481
}

//生命周期

//窗口销毁
CjhGm's avatar
CjhGm 已提交
482
void CAircraftBattleView::OnDestroy()
CjhGm's avatar
CjhGm 已提交
483
484
485
486
487
{
	// 销毁指针资源
	if (myplane != NULL)
		delete myplane;

CjhGm's avatar
gogogo    
CjhGm 已提交
488
	//释放内存资源
CjhGm's avatar
CjhGm 已提交
489
490
491
492
493
494
	scene.ReleaseScene();

	CView::OnDestroy();
}

//游戏重新开始
CjhGm's avatar
CjhGm 已提交
495
void CAircraftBattleView::Restart()
CjhGm's avatar
CjhGm 已提交
496
497
498
499
500
501
502
503
504
{
	// TODO: 在此处添加游戏重新开始初始化参数
	//战机重新加载
	myplane = new CMyPlane(FALSE);

	//清空敌机链表
	if (enemyList.GetCount() > 0)
		enemyList.RemoveAll();
	//清空战机子弹链表
CjhGm's avatar
CjhGm 已提交
505
506
	if (bulletList.GetCount() > 0)
		bulletList.RemoveAll();
CjhGm's avatar
CjhGm 已提交
507
508

	//参数重新初始化
CjhGm's avatar
CjhGm 已提交
509
	isPause = false;
CjhGm's avatar
CjhGm 已提交
510
511
	isOver = false;
	myScore = 0;
CjhGm's avatar
CjhGm 已提交
512
513
514
515
516
	//isStarted = FALSE;
	SetMyTimer();
}

//游戏暂停
CjhGm's avatar
CjhGm 已提交
517
void CAircraftBattleView::Pause()
CjhGm's avatar
CjhGm 已提交
518
519
520
521
522
523
524
{
	// TODO: 在此处添加游戏暂停操作
	isPause = TRUE;
	Sleep(1000);
}

// 生命值归零,游戏结束
CjhGm's avatar
CjhGm 已提交
525
void CAircraftBattleView::gameOver(CDC* pDC, CDC& cdc, CBitmap* cacheBitmap)
CjhGm's avatar
CjhGm 已提交
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
{
	//结束游戏界面
	//释放计时器
	KillTimer(1);
	KillTimer(2);
	KillTimer(3);
	//KillTimer(4);
	KillTimer(5);
	//播放游戏结束音乐
	PlaySound((LPCTSTR)IDR_WAV_GAMEOVER, AfxGetInstanceHandle(), SND_RESOURCE | SND_ASYNC);
	//清屏
	CBitmap* tCache = cacheBitmap;
	cacheBitmap = new CBitmap;
	cacheBitmap->CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
	cdc.SelectObject(cacheBitmap);
	//内存中承载临时图像的缓冲位图
	delete tCache;
CjhGm's avatar
CjhGm 已提交
543
	//isStop = FLAG_STOP;
CjhGm's avatar
CjhGm 已提交
544
545
546
}

//设置计时器
CjhGm's avatar
CjhGm 已提交
547
void CAircraftBattleView::SetMyTimer()
CjhGm's avatar
CjhGm 已提交
548
549
{
	SetTimer(1, 17, NULL);//刷新界面定时器
CjhGm's avatar
CjhGm 已提交
550
551
	SetTimer(2, 400 - 1 * 30, NULL);//产生敌机定时器
	SetTimer(3, 2000 - 1 * 100, NULL);//产生敌机炮弹频率
CjhGm's avatar
gogogo    
CjhGm 已提交
552

CjhGm's avatar
CjhGm 已提交
553
554
555
	SetTimer(5, 2000, NULL);//控制魔法值变化频率
}

CjhGm's avatar
CjhGm 已提交
556
//void CAircraftBattleView::OnClose()
CjhGm's avatar
CjhGm 已提交
557
558
559
560
561
562
563
564
565
566
//{
//	isPause = TRUE;
//	// TODO: 在此添加消息处理程序代码和/或调用默认值
//	KillTimer(1);
//	KillTimer(2);
//	KillTimer(3);
//	KillTimer(4);
//	KillTimer(5 );
//	CView::OnClose();
//}