-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBnbApp.cpp
341 lines (298 loc) · 6.86 KB
/
BnbApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "BnbApp.h"
IMPLEMENT(CBnbApp)
CBnbApp::CBnbApp()
{
m_seclectScene = MAIN_SCENE;
isKey_stopMusic = false;
mainScene = NULL;
helpScene = NULL;
twoGameScene = NULL;
playMusic = NULL;
}
CBnbApp::~CBnbApp()
{
delete mainScene;
delete twoGameScene;
delete helpScene;
delete playMusic;
mainScene = NULL;
helpScene = NULL;
twoGameScene = NULL;
playMusic = NULL;
}
void CBnbApp::OnCreateGame()
{
// 各场景初始化
mainScene = new CMainScene;
if (mainScene != NULL) mainScene->MainSceneInit(m_hIns);
else MessageBox(NULL, TEXT("场景加载失败"), TEXT("提示"),MB_OK | MB_ICONERROR);
// 播放相应背景音乐
playMusic = new CPlayMusic;
if (playMusic != NULL)
{
this->PlayBackMusic();
}
else
{
MessageBox( NULL, TEXT("音乐加载失败!"), TEXT("提示"), MB_OK | MB_ICONERROR );
}
}
void CBnbApp::OnGameDraw()
{
HDC hdc = GetDC(m_hMainWnd);
// 解决闪屏问题
HDC hdcMem = ::CreateCompatibleDC(hdc);
HBITMAP hBitmap = ::CreateCompatibleBitmap(hdc,800,600);
SelectObject(hdcMem,hBitmap);
// 绘图:不同场景
if(this->m_seclectScene == ONE_GAME_SCENE && (twoGameScene != NULL))
{
twoGameScene->TwoGameSceneShow(hdcMem);
}
else if (this->m_seclectScene == TWO_GAME_SCENE && (twoGameScene != NULL))
{
twoGameScene->TwoGameSceneShow(hdcMem);
}
else if (this->m_seclectScene == HLEP_GAME_SCENE && (helpScene != NULL))
{
helpScene->HelpSceneShow(hdcMem);
}
else
{
mainScene->MainSceneShow(hdcMem);
}
BitBlt(hdc,0,0,800,600,hdcMem,0,0,SRCCOPY);
DeleteDC(hdcMem);
DeleteObject(hBitmap);
ReleaseDC(m_hMainWnd,hdc);
}
void CBnbApp::OnGameRun(WPARAM nTimerID)
{
if ((m_seclectScene == TWO_GAME_SCENE || m_seclectScene == ONE_GAME_SCENE) && (twoGameScene != NULL))
{
twoGameScene->OnTwoGameRun(nTimerID);
}
//重绘
this->OnGameDraw();
}
void CBnbApp::OnKeyDown(WPARAM nKey)
{
switch (nKey)
{
// F3键: 当在双人游戏界面按下F3可返回主场景 MAIN_SCENE
case VK_F3:
if (this->m_seclectScene == TWO_GAME_SCENE || this->m_seclectScene == ONE_GAME_SCENE)
{
// 如果鼠标停留退出选项 返回主场景 需将标记位置为 false
if (twoGameScene->m_isSelect)
{
twoGameScene->m_isSelect = false;
}
// 切换回主场景
if (mainScene == NULL)
{
mainScene = new CMainScene;
mainScene->MainSceneInit(m_hIns);
}
m_seclectScene = MAIN_SCENE;
// 释放游戏场景对象
delete twoGameScene;
twoGameScene = NULL;
this->OnGameDraw();
// 播放相应背景音乐
if (!this->isKey_stopMusic)
{
this->PlayBackMusic();
}
}
break;
// F8键 背景音乐开关
case VK_F8:
if (this->isKey_stopMusic)
{
playMusic = new CPlayMusic;
this->isKey_stopMusic = false;
}
else
{
this->isKey_stopMusic = true;
}
this->PlayBackMusic();
break;
}
// 将按键传入游戏场景
if (twoGameScene != NULL)
{
twoGameScene->OnKeyDown(nKey);
}
}
void CBnbApp::OnKeyUp(WPARAM nKey)
{
if (twoGameScene != NULL)
{
twoGameScene->OnKeyUp(nKey);
}
}
void CBnbApp::OnLButtonDown(POINT point)
{
if (twoGameScene != NULL)
{
//暂时调用双人游戏的按键实现按键按下出泡泡
twoGameScene->OnLButtonDown(m_hIns,point);
}
}
void CBnbApp::OnLButtonUp(POINT point)
{
// 如果当前场景为主场景并且鼠标在可选范围内,鼠标左键才允许选择不同场景
if (m_seclectScene == MAIN_SCENE && (mainScene->m_seclectNum == ONE_GAME || mainScene->m_seclectNum == TWO_GAME || mainScene->m_seclectNum == HLEP_GAME || mainScene->m_seclectNum == QUIT_GAME))
{
this->ChangeScene();
}
// 如果当前场景为帮助场景并且鼠标在返回框内,鼠标左键才允许返回主场景
if (m_seclectScene == HLEP_GAME_SCENE && helpScene->m_isSelect)
{
if (mainScene == NULL)
{
mainScene = new CMainScene;
mainScene->MainSceneInit(m_hIns);
}
m_seclectScene = MAIN_SCENE;
helpScene->m_isSelect = false; // 返回后将帮助场景的鼠标位置标记置为false
// 释放帮助场景对象
if (helpScene)
{
delete helpScene;
helpScene = NULL;
}
}
if ((m_seclectScene == TWO_GAME_SCENE || m_seclectScene == ONE_GAME_SCENE) && twoGameScene->m_isSelect)
{
if ( MessageBox( NULL, TEXT("大人: 游戏正在进行, 确认退出么?"), \
TEXT("退出"), MB_OKCANCEL | MB_ICONQUESTION ) == IDOK )
{
PostQuitMessage(0);
}
}
// 重绘
this->OnGameDraw();
}
void CBnbApp::OnMouseMove(POINT point)
{
//当前场景为主场景情况下,传入鼠标位置,看是否悬浮于主界面某个选项
if (m_seclectScene == MAIN_SCENE)
{
mainScene->MouseMove(point);
}
//当前场景为帮助场景情况下,传入鼠标位置,看是否悬浮于返回选项
if (m_seclectScene == HLEP_GAME_SCENE)
{
helpScene->MouseMove(point);
}
// 当前场景为游戏场景情况下,传入鼠标位置,看是否悬浮于推出选项
if (m_seclectScene == TWO_GAME_SCENE || m_seclectScene == ONE_GAME_SCENE)
{
twoGameScene->MouseMove(point);
}
// 重绘
this->OnGameDraw();
}
void CBnbApp::ChangeScene()
{
// 鼠标点击后 根据鼠标悬浮的标记 判断应切换到哪个场景
if (mainScene->m_seclectNum == ONE_GAME)
{
if (twoGameScene == NULL)
{
twoGameScene = new CTwoGameScene;
twoGameScene->TwoGameSceneInit(m_hIns,m_hMainWnd);
}
this->m_seclectScene = ONE_GAME_SCENE;
// 释放主场景对象
if (mainScene)
{
delete mainScene;
mainScene = NULL;
}
}
else if (mainScene->m_seclectNum == TWO_GAME)
{
// 创建游戏场景对象并初始化
if (twoGameScene == NULL)
{
twoGameScene = new CTwoGameScene;
twoGameScene->TwoGameSceneInit(m_hIns,m_hMainWnd);
}
this->m_seclectScene = TWO_GAME_SCENE;
// 释放主场景对象
if (mainScene)
{
delete mainScene;
mainScene = NULL;
}
}
else if (mainScene->m_seclectNum == HLEP_GAME)
{
// 创建帮助场景对象并初始化
if (helpScene == NULL)
{
helpScene = new CHelpScene;
helpScene->HelpSceneInit(m_hIns);
}
this->m_seclectScene = HLEP_GAME_SCENE;
// 释放主场景对象
if (mainScene)
{
delete mainScene;
mainScene = NULL;
}
}
else if (mainScene->m_seclectNum == QUIT_GAME)
{
if ( MessageBox( NULL, TEXT("大人: 真的不想玩了么? 请您三思啊!"), \
TEXT("退出"), MB_OKCANCEL | MB_ICONQUESTION ) == IDOK )
{
PostQuitMessage(0);
}
else
{
this->m_seclectScene = MAIN_SCENE;
}
}
// 主场景进去帮助场景,背景音乐不变
if (this->m_seclectScene != HLEP_GAME_SCENE && (!this->isKey_stopMusic))
{
this->PlayBackMusic();
}
// 重绘
this->OnGameDraw();
}
void CBnbApp::PlayBackMusic()
{
// F8键按下 播放或停止背景音乐
if (this->isKey_stopMusic)
{
if(!playMusic->isStop)
{
playMusic->SotpBackMusic();
// 删除播放音乐对象
delete playMusic;
playMusic = NULL;
}
else
{
if (playMusic != NULL)
{
if (this->m_seclectScene == ONE_GAME_SCENE) playMusic->PlayBackMusic(ONEGAME_BACK_MUSIC);
else if(this->m_seclectScene == TWO_GAME_SCENE) playMusic->PlayBackMusic(TWOGAME_BACK_MUSIC);
else playMusic->PlayBackMusic(MAIN_BACK_MUSIC);
}
}
}
// F8键未按下 根据场景播放不同背景音乐
else
{
if (this->m_seclectScene == ONE_GAME_SCENE) playMusic->PlayBackMusic(ONEGAME_BACK_MUSIC);
else if(this->m_seclectScene == TWO_GAME_SCENE) playMusic->PlayBackMusic(TWOGAME_BACK_MUSIC);
else playMusic->PlayBackMusic(MAIN_BACK_MUSIC);
}
}