-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCastle.cpp
377 lines (341 loc) · 8.32 KB
/
Castle.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
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
376
377
/*
Castle.cpp:
流程控制逻辑函数、绘制迷宫算法
by @WoodenStone
生成迷宫 图论DFS
by @Mango-wen
*/
#include "Game.h"
//全局变量
extern char ch_msg; //键盘消息
extern mouse_msg m_msg; //鼠标消息
extern long long times, start_time; //计时器
int GameLevel_castle = 1;
//绘制界面
void CCastle::putRoom()
{
wchar_t time[25], cur_pass[50], tot_pass[50], gamelevel[20]; //计时、当前关卡、总关卡、难度等级
swprintf_s(cur_pass, L"第 %d 关\0", single_pass);
swprintf_s(tot_pass, L"共 %d 关\0", all_pass);
swprintf_s(time, L"使用时间 %lld s\0", times);
swprintf_s(gamelevel, L"难度等级:%d\0", GameLevel_castle);
//字体样式设置
setcolor(WHITE);
setbkmode(TRANSPARENT);
setfont(20, 0, "微软雅黑");
xyprintf(480, 25, cur_pass);
xyprintf(480, 55, tot_pass);
xyprintf(480, 115, time);
xyprintf(480, 85, gamelevel);
//获取图像资源
PIMAGE pimg_castle_road = newimage();
getimage(pimg_castle_road, "JPG", MAKEINTRESOURCEA(CASTLE_ROAD));
PIMAGE pimg_castle_wall = newimage();
getimage(pimg_castle_wall, "JPG", MAKEINTRESOURCEA(CASTLE_WALL));
PIMAGE pimg_castle_player = newimage();
getimage(pimg_castle_player, "JPG", MAKEINTRESOURCEA(CASTLE_PLAYER));
PIMAGE pimg_castle_end = newimage();
getimage(pimg_castle_end, "JPG", MAKEINTRESOURCEA(CASTLE_END));
//绘制迷宫
for (int i = x - 4; i <= x + 4; i++)
{
for (int j = y - 4; j <= y + 4; j++)
{
if (i < 1 || j < 1) {
continue;
}
if (maze.map[i][j] == WALL)
{
putimage((j - 1 - y) * 50 + 266, (i - 1 - x) * 50 + 266, 50, 50, pimg_castle_wall, 0, 0, 128, 128);
}
else if (maze.map[i][j] == ROAD)
{
putimage((j - 1 - y) * 50 + 266, (i - 1 - x) * 50 + 266, 50, 50, pimg_castle_road, 0, 0, 300, 300);
}
else if (maze.map[i][j] == END)
{
putimage((j - 1 - y) * 50 + 266, (i - 1 - x) * 50 + 266, 50, 50, pimg_castle_end, 0, 0, 100, 100);
maze.des_x = i;
maze.des_y = j;
}
else if (maze.map[i][j] == YOU)
{
putimage((j - 1 - y) * 50 + 266, (i - 1 - x) * 50 + 266, 50, 50, pimg_castle_player, 0, 0, 150, 150);
x = i;
y = j;
}
}
}
delimage(pimg_castle_road);
delimage(pimg_castle_wall);
delimage(pimg_castle_end);
delimage(pimg_castle_player);
}
//城堡stage核心
void CCastle::mainGame()
{
CButton* button = new CButton();
CAdventure* ad = new CAdventure();
bool ifhelped = false;
//模块标识
Stage.num = 3;
//设定按钮位置参数
msgBoxPosition msg_back;
msg_back.x = 120;
msg_back.y = 165;
msg_back.weight = 400;
msg_back.height = 150;
msgBoxPosition msg_Pause;
msg_Pause.x = 170;
msg_Pause.y = 165;
msg_Pause.weight = 300;
msg_Pause.height = 150;
//输入框变量
const int buffsize = 10;
char strbuff[150] = "";
memset(strbuff, 0, sizeof(strbuff));
bool error = false;
bool ifInput = false;
switch (GameLevel_castle) {
case 1:
break;
case 2:
maze.size_m += 6;
maze.size_n += 6;
break;
case 3:
maze.size_m += 10;
maze.size_n += 10;
break;
case 4:
maze.size_m += 14;
maze.size_n += 14;
break;
case 5:
maze.size_m += 28;
maze.size_n += 28;
break;
default:
GameLevel_castle = 1;
break;
}
int xClick = 0, yClick = 0;
PIMAGE pimg_castle_bg = newimage();
getimage(pimg_castle_bg, "images\\castle_bg.jpg", 0, 0);
for (; is_run(); delay_fps(60))
{
//clear();//清空数据函数
deepFS();
x = 2;
y = 2;//重置坐标
//设置初始时间为0
start_time = int(time(NULL));
for (; is_run(); delay_fps(60))
{
//累加时间
times = int(time(NULL)) - start_time;
ch_msg = (char)'#'; //清空键盘消息
if (kbhit()) //键盘消息获取
ch_msg = getch();
bool click_flag = false;
while (mousemsg()) //鼠标消息获取
{
m_msg = getmouse();
if (m_msg.is_left() && m_msg.is_down()) //存在左键点击事件
{
click_flag = true;
xClick = m_msg.x;
yClick = m_msg.y;
}
}
man_Move();//人物移动函数
if (x == maze.des_x && y == maze.des_y)
break; //到达终点判断
//绘制背景
button->drawBackground();
setbkcolor(EGERGB(102,110,147));
//绘制游戏区
putimage(16, 16, 450, 483, pimg_castle_bg, 0, 0, 734, 536);
//绘制当前游戏界面
putRoom();
//按钮设置
button->putButton_(513, 390, 603, 420, "回到主页");
button->putButton_(513, 350, 603, 380, "暂停");
button->putButton_(513, 310, 603, 340, "显示路径");
if (single_pass == 1)
{
button->putButton_(513, 430, 603, 460, "更改难度");
}
//点击回到主页
if (button->ifClick(xClick, yClick, 513, 390, 603, 420))
{
long long t = times;
wchar_t* text[10];
text[0] = L"确定回到主页?\n";
if (button->putMessageBox(msg_back, L"回到主页", text, 1, MY_CHOICE))
{
delete button;
button = NULL;
return;
}
else {
click_flag = false;
xClick = 0, yClick = 0;
start_time = int(time(NULL)) - t;
times = t;
}
}
//pause
if (button->ifClick(xClick, yClick, 513, 350, 603, 380))
{
long long t = times;
wchar_t* text[10];
text[0] = L"按“确定”结束暂停\n";
do {
getmouse();
} while (!(button->putMessageBox(msg_Pause, L"暂停", text, 1, 0)));
start_time = int(time(NULL)) - t;
times = t;
//IMPORTANT!重置标志值和已获取的鼠标位置信息,否则会导致界面停留不动
click_flag = false;
xClick = 0, yClick = 0;
}
//显示路径
if (button->ifClick(xClick, yClick, 513, 310, 603, 340))
{
click_flag = false;
xClick = 0, yClick = 0;
long long t = times;
if (!ifhelped) {
do {
solveByQueue(maze.size_m, maze.size_n, Stage.num);
} while (!(getch()));
ifhelped = true;
}
else {
do {
outtextxy(60, 60, "机会仅有一次,请少侠继续努力~\(≧▽≦)/~");
} while (!(getch()));
}
start_time = int(time(NULL)) - t;
times = t;
click_flag = false;
xClick = 0, yClick = 0;
}
if (single_pass == 1)
{
button->putButton_(513, 430, 603, 460, "更改难度");
if (button->ifClick(xClick, yClick, 513, 430, 603, 460))
{
click_flag = false;
xClick = 0, yClick = 0;
inputbox_getline("请输入难度等级", "输入难度等级1~5,1为最低,5为最高\n输入完毕请键入回车", strbuff, buffsize);
if (strbuff[0] == '\0')
{
error = false;
}
else if (strlen(strbuff) > 5) {
ifInput = error = true;
}
else if (sscanf(strbuff, "%d", &GameLevel_castle) == 1)
{
error = false;
ifInput = true;
}
else {
ifInput = error = true;
}
if (ifInput)
{
if (error)
{
setcolor(WHITE);
xyprintf(100, 60, "输入错误");
delay_ms(1000);
}
else
{
mainGame();
}
}
}
}
}
//通过一关卡
if (oneLevelClear())
break;
//通过全关卡
if (single_pass == all_pass)
{
gameClear();
break;
}
//下一关
single_pass++;
maze.size_m += 6;
maze.size_n += 6;
if (maze.size_n > 99 || maze.size_m > 99)
{
maze.size_n = 99;
maze.size_m = 99;
}
}
delimage(pimg_castle_bg);
delete(ad);
ad = NULL;
delete button;
button = NULL;
}
// 图论 DFS 随机生成迷宫
void CCastle::deepFS()
{
maze.Nv = 0; // 节点数清空
//maze.size_m = 21;
//maze.size_n = 21;
for (int i = 1; i <= maze.size_n; i++) // 初始化
{
for (int j = 1; j <= maze.size_m; j++)
{
if (i % 2 == 0 && j % 2 == 0)
{
maze.map[i][j] = ROAD;
flag[0][++maze.Nv] = maze.Nv;
flag[1][maze.Nv] = i;
flag[2][maze.Nv] = j;
}
else
{
maze.map[i][j] = WALL;
}
}
}
for (int i = 1; i <= maze.Nv; i++)
{
visit[i] = 0;
if (i % ((maze.size_n - 1) / 2) != 0)
{
maze.reg[i][i + 1] = 1;
maze.reg[i + 1][i] = 1;
}
if (i <= maze.Nv - (maze.size_n - 1) / 2)
{
maze.reg[i][i + (maze.size_n - 1) / 2] = 1;
maze.reg[i + (maze.size_n - 1) / 2][i] = 1;
}
}
dfs_search(maze.Nv); // 进行图论 DFS 生成
for (int i = 1; i <= maze.Nv; i++)
{
for (int j = 1; j <= maze.Nv; j++)
{
if (maze.reg[i][j] == 2 || maze.reg[j][i] == 2)
{
Connect(flag[1][i], flag[2][i], flag[1][j], flag[2][j]);
}
}
}
maze.map[2][2] = YOU;
if ((maze.size_n - 1) > 0 && maze.size_m > 0) {
maze.map[maze.size_n - 1][maze.size_m] = END; // 将图论 DFS 结果显示到迷宫中
}
}