-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshiting_dragon.cpp
More file actions
498 lines (474 loc) · 10.9 KB
/
shiting_dragon.cpp
File metadata and controls
498 lines (474 loc) · 10.9 KB
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <time.h>
#include <graphics.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
/////////////////////以下是 宏定义参数/////////////////////
#define WIDTH 800 //屏幕宽度
#define HEIGHT 780 //屏幕高度
#define B_WIDTH 60 //泡泡/泡泡枪的宽度
#define B_HEIGHT 60 //泡泡/泡泡枪的高度
#define S_WIDTH 220 //计分板的宽度
#define S_HEIGHT 80 //计分板的高度
#define LB_WIDTH 540 //左侧背景的宽度
#define RB_WIDTH 260 //右侧背景的宽度
#define cur_positioni 12 //泡泡枪的行数
#define score_per_Bubble 100 //每个泡泡的分数
////////////////////以下是 全局变量////////////////////
IMAGE picture[7]; //创建泡泡图像对象
IMAGE initbackimage, scoreimage, Leftbackimage, Rightbackimage; //创建背景图像对象
IMAGE start,restart, end; //创建图标图像对象
int array2D[13][9]; //定义一个二维数组
int i; //数组的行数
int j; //数组的列数
int cur_position=4; //泡泡枪当前的位置(列数)
int pre_position = 4; //泡泡枪之前的位置(列数)
//int bobCount = 0; //连通泡泡计数
int thisBubble = 0; //这个泡泡
int nextBubble = 0; //下一个泡泡
int shootingBubble = 0; //发射中的泡泡
int GamePhase = 0; //游戏阶段 (0代表载入阶段 1代表开始阶段 2代表游戏阶段 3代表结束阶段)
int mouseX; //鼠标位置坐标X
int mouseY; //鼠标位置坐标Y
bool isMouseDown; //鼠标按下
int number=0; //泡泡消除个数
int count=0; //记录总的泡泡消除个数
int score=0; //分数
/////////////////////以下是 函数声明/////////////////////
//void LoadResource();
void Menu();
void StartPage();
void ProduceInitialBubble();
void ShootBubble();
int StopBubble(int x, int y);
void rmBob(int x, int y);
void RemoveBubble(int x, int y);
void RiseBubble();
void ProduceNewBubble();
void Score(int number);
void Manager();
void PutImage();
void Gameover();
void Draw();
void EndPage();
bool judge_valid_area(int x, int y);
int main()
{
setbuf(stdin, NULL);
//置随机数种子
srand((unsigned)time(NULL));
//初始化设备,加载图片
initgraph(WIDTH, HEIGHT,SHOWCONSOLE);
//开启缓冲,防止闪屏
BeginBatchDraw();
// 鼠标消息变量
MOUSEMSG mmsg;
while (true)
{
//处理鼠标消息
while (MouseHit())
{
mmsg = GetMouseMsg();
switch (mmsg.uMsg)
{
case WM_MOUSEMOVE: mouseX = mmsg.x; mouseY = mmsg.y; break;
case WM_LBUTTONDOWN: isMouseDown = true; break;
//case WM_LBUTTONUP: isMouseDown = false; break;
}
}
if (GamePhase == 2)
{
isMouseDown = false;
}
if (GamePhase == 3)
{
fflush(NULL);
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
}
//逻辑处理
Manager();
//绘图处理
Draw();
//显示缓存的绘制内容6
FlushBatchDraw();
}
//结束批量绘制,并执行未完成的绘制任务
EndBatchDraw();
system("pause");
closegraph();
return 0;
}
void Menu()
{
loadimage(&initbackimage, "Backimage\\initbackimage.jpg", WIDTH, HEIGHT);//加载菜单页面背景
loadimage(&start, "Signal\\start.jpg", 300, 200);//加载开始按钮图片
putimage(0, 0, &initbackimage);
putimage(250, 350, &start);
mciSendString("open backmusic.mp3 alias music", 0, 0, 0);//打开音乐文件
//mciSendString("play music repeat", 0, 0, 0);//播放音乐
if (mouseX> 250 && mouseX<550 && mouseY>350 && mouseY < 550)
{
if (isMouseDown)
{
GamePhase = 1;
isMouseDown = false;
}
}
}
void StartPage()
{
memset(array2D, 0, sizeof(array2D));
loadimage(&picture[0], "Bubble\\1.jpg", B_WIDTH, B_HEIGHT);//加载黑色泡泡图片 泡泡大小60*60
loadimage(&picture[1], "Bubble\\2.jpg", B_WIDTH, B_HEIGHT);//加载蓝色泡泡图片 泡泡大小60*60
loadimage(&picture[2], "Bubble\\3.jpg", B_WIDTH, B_HEIGHT);//加载黄色泡泡图片 泡泡大小60*60
loadimage(&picture[3], "Bubble\\4.jpg", B_WIDTH, B_HEIGHT);//加载紫色泡泡图片 泡泡大小60*60
loadimage(&picture[4], "Bubble\\5.jpg", B_WIDTH, B_HEIGHT);//加载绿色泡泡图片 泡泡大小60*60
loadimage(&picture[5], "Bubble\\6.jpg", B_WIDTH, B_HEIGHT);//加载泡泡枪图片
loadimage(&picture[6], "Bubble\\7.jpg", B_WIDTH, B_HEIGHT);//加载空白图片
loadimage(&scoreimage, "Backimage\\score.jpg", S_WIDTH, S_HEIGHT);//加载计分板图片
loadimage(&Leftbackimage, "Backimage\\Leftbackimage.jpg", LB_WIDTH, HEIGHT);//加载背景图片
loadimage(&Rightbackimage, "Backimage\\Rightbackimage.jpg", RB_WIDTH, HEIGHT); // 加载记分板背景图片
putimage(540, 0, &Rightbackimage); // 绘制右侧背景
putimage(0, 0, &Leftbackimage);// 绘制左侧背景
putimage(560, 80, &scoreimage);//绘制计分板
GamePhase = 2;
outtextxy(560, 160,"0");
ProduceInitialBubble();
}
void ProduceInitialBubble()
{
for (i = 0; i < 6; i++)//初始化二维数组
{
for (j = 0; j < 9; j++)
{
array2D[i][j] = rand() % 5 + 1;//随机分配1~5代表不同颜色的泡泡
}
}
array2D[12][cur_position] = 6;//初始泡泡枪的位置
thisBubble = rand() % 5 + 1;//初始发射泡泡的分数
array2D[11][cur_position] = thisBubble;
}
/*void NextBubble() //随机生成下一次要发射泡泡
{
if ( )
{
nextBubble = rand() % 5 + 1;
array2D[11][cur_position] = nextBubble;
}
}*/
void ShootBubble()
{
shootingBubble = thisBubble;
thisBubble = nextBubble;
int x = 0;
for (int i = 10; i >= 0; i--)
{
array2D[i][cur_position] = shootingBubble;
x = i;
array2D[i + 1][cur_position] = 0;
if (StopBubble(i, cur_position) == 1)
break;
Sleep(80);
}
RemoveBubble(x, cur_position);
RiseBubble(); //如果泡泡悬空
if (count >= 9)
ProduceNewBubble();
}
//碰撞检测
int StopBubble(int x, int y)
{
if (array2D[x - 1][y] != 0) //直线上
{
return 1;
}
return 0;
}
void RemoveBubble(int x, int y)
{
//递归消除
number = 0;
rmBob(x, y);
if (number <= 2)
{
for (i = 0; i < 11; i++)
{
for (j = 0; j < 9; j++)
{
if (array2D[i][j] == -1)
{
array2D[i][j] = shootingBubble;
}
}
}
}
else
Score(number);
//printMap();
}
void rmBob(int x, int y)
{
//如果匹配
if (array2D[x][y] == shootingBubble)
{
number++;
count = number;
array2D[x][y] = -1;
if (x > 0) {
rmBob(x - 1, y);
}
if (x < 10) {
rmBob(x + 1, y);
}
if (y > 0) {
rmBob(x, y - 1);
}
if (y < 8) {
rmBob(x, y + 1);
}
}
}
void RiseBubble()
{
for (int i = 0; i <= 10; i++)
{
for (int j = 0; j < 9; j++)
{
if (array2D[i][j])
{
if (judge_valid_area(i, j))
{
while (i > 0 && !array2D[i - 1][j])
{
array2D[i - 1][j] = array2D[i][j];
array2D[i][j] = 0;
i--;
}
}
}
}
}
}
bool judge_valid_area(int x, int y)
{
//没有上升空间了
if (x == 0)
return false;
//最左侧泡泡
else if (y == 0)
{
//右侧、上侧不接壤
if (!array2D[x - 1][y] && !array2D[x][y + 1])
return true;
else
return false;
}
//最右侧泡泡
else if (y == 8)
{
//左侧、上侧不接壤
if (!array2D[x - 1][y] && !array2D[x][y - 1])
return true;
else
return false;
}
//中间区域的泡泡
else
{
//左侧、上侧、右侧不接壤
if (!array2D[x - 1][y] && !array2D[x][y + 1] && !array2D[x][y - 1])
return true;
else
return false;
}
}
void ProduceNewBubble()
{
if (count >= 9)
{
for (i = 10; i>0; i--)
{
for (j = 0; j<9; j++)
array2D[i+1][j] = array2D[i][j];
} //每一行往下移动,给第一行留下空间
}
for (i = 0; i<9; i++)
{
array2D[0][i] = rand() % 5; //第一行的每一个随机赋予值
}
count = 0;
}
void Score(int n)
{
char str[20]="";
score += n * score_per_Bubble;
printf("%d,", score);
sprintf(str, "%d", score);
outtextxy(560, 160, str);
}
void Manager()
{
if (GamePhase == 2) //游戏阶段
{
char key = 0;
int flag1 = 0, flag2 = 0;
if (_kbhit())
{
key = _getch();
switch (key)
{
case 97://a
if (cur_position > 0)
{
array2D[12][cur_position] = 0; //泡泡枪的当前位置
array2D[11][cur_position] = 0; //泡泡枪上的泡泡位置
pre_position = cur_position;
cur_position--;
flag1 = 1;
}
break;
case 100://d
if (cur_position < 8)
{
array2D[12][cur_position] = 0;
array2D[11][cur_position] = 0;
pre_position = cur_position;
cur_position++;
flag1 = 1;
}
break;
case 119://w
/*for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 9; j++)
printf("%d ", array2D[i][j]);
printf("\n");
}
printf("\n");*/
nextBubble = rand() % 5 + 1;
array2D[11][cur_position] = nextBubble;
flag2 = 0;
ShootBubble();
flag1 = 1;
break;
default:
break;
}
if (flag1 == 1)
{
array2D[12][cur_position] = 6;
array2D[11][cur_position] = thisBubble;
flag1 = 0;
}
}
int end_sen = 0;
for (int i = 0; i < 9; i++)
{
int consistant = 1;
for (int j = 0; j <= 10; j++)
{
if (!array2D[j][i])
consistant = 0;
}
if (consistant)
{
end_sen = 1;
break;
}
}
if (end_sen)
{
GamePhase = 3;
}
}
}
void PutImage()//根据二维数组的内容绘制所有图片
{
int x, y;
for (i = 0; i < 13; i++)
{
for (j = 0; j < 9; j++)
{
x = j * 60;
y = i * 60;
switch (array2D[i][j])
{
case -1:
putimage(x, y, &picture[6]);
array2D[i][j] = 0;
case 0:
if (pre_position != cur_position)
{
putimage(pre_position * 60,12*60, &picture[6]);
putimage(pre_position * 60, 11 * 60, &picture[6]);
}
break;
case 1:putimage(x, y, &picture[0]);//1代表黑色泡泡
break;
case 2:putimage(x, y, &picture[1]);//2代表蓝色泡泡
break;
case 3:putimage(x, y, &picture[2]);//3代表黄色泡泡
break;
case 4:putimage(x, y, &picture[3]);//4代表紫色泡泡
break;
case 5:putimage(x, y, &picture[4]);//5代表绿色泡泡
break;
case 6:putimage(x, y, &picture[5]);//6代表泡泡枪
break;
default:
break;
}
}
}
/*if (pre_position != cur_position)
{
putimage(12, pre_position, &picture[6]);
}*/
}
void Gameover()
{
for (i = 0; i<9; i++)
{
if (array2D[10][i] != 0)
GamePhase = 3;
}
}
void Draw()
{
switch (GamePhase)//游戏阶段 0代表载入阶段 1代表开始阶段 2代表游戏阶段 3代表结束阶段
{
case 0: Menu(); break;
case 1: StartPage();break;
case 2: PutImage(); break;
case 3: EndPage(); break;
}
}
void EndPage()
{
loadimage(&end, "Signal\\end.jpg", 250, 280);
loadimage(&restart, "Signal\\restart.jpg", 250, 280);
putimage(100, 300, &restart);
putimage(450, 300, &end);
if (mouseX > 100 && mouseX < 350 && mouseY>300 && mouseY < 580)
{
if (isMouseDown)
{
GamePhase = 1; //restart
isMouseDown = false;
}
}
else if (mouseX > 450 && mouseX < 700 && mouseY>300 && mouseY < 580)
{
if (isMouseDown)
{
GamePhase = 0; //end
isMouseDown = false;
}
}
else
isMouseDown = false;
}