本文实例为大家分享了C语言宾果消消乐的具体代码,供大家参考,具体内容如下
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
|
```c #include<graphics.h> #include<stdio.h> #include<windows.h> #include <conio.h> #include<time.h> #include<mmsystem.h> #define MAPSIZE 10 int map[MAPSIZE][MAPSIZE]; int map2[MAPSIZE][MAPSIZE]; bool state[MAPSIZE][MAPSIZE]; bool state1[MAPSIZE][MAPSIZE]; typedef struct game { int game_state; int counts; int score; int targets; int level; int state; int level_state; int tool_number; int tool_state; int start_state; PIMAGE img[21]; } Game; Game game; int high=0; void init_img() { int i; for (i=0; i<21; i++) game.img[i]=newimage(); getimage(game.img[0], "game\\underground.jpg" ); getimage(game.img[1], "game\\red.jpg" ); getimage(game.img[2], "game\\green.jpg" ); getimage(game.img[3], "game\\blue.jpg" ); getimage(game.img[4], "game\\orange.jpg" ); getimage(game.img[5], "game\\purple.jpg" ); getimage(game.img[6], "game\\brown.jpg" ); getimage(game.img[7], "game\\123.jpg" ); getimage(game.img[8], "game\\end.jpg" ); getimage(game.img[9], "game\\bomb.jpg" ); getimage(game.img[10], "game\\win.png" ); getimage(game.img[11], "game\\hon.jpg" ); getimage(game.img[12], "game\\lu.jpg" ); getimage(game.img[13], "game\\lan.jpg" ); getimage(game.img[14], "game\\cheng.jpg" ); getimage(game.img[15], "game\\zi.jpg" ); getimage(game.img[16], "game\\hui.jpg" ); getimage(game.img[17], "game\\receive.png" ); getimage(game.img[18], "game\\in.jpg" ); getimage(game.img[19], "game\\black.jpg" ); getimage(game.img[20], "game\\start.jpg" ); } void init_map() { int i,j; srand ( time (0)); for (i=0; i<MAPSIZE; i++) for (j=0; j<MAPSIZE; j++) { state[i][j]= false ; map[i][j]= rand ()%6+1; map2[i][j]=map[i][j]; //备份图形 } } void read( int *high) { FILE *fp; fp= fopen ( "bag.b" , "rb" ); if (fp==NULL) return ; fread (high, sizeof ( int ),1,fp); fclose (fp); } void save( int *high) { FILE *fp; fp= fopen ( "bag.b" , "wb" ); if (fp==NULL) return ; fwrite (high, sizeof ( int ),1,fp); fclose (fp); } //显示关卡 void show_level() { char str[4],str2[8]; putimage(0,0,game.img[0]); sprintf (str, "%4d " , game.level); sprintf (str2, "%4d " , game.targets); setfont(40,0, "隶书" ); setcolor(EGERGB(255,255,255)); setbkmode(TRANSPARENT); outtextxy(160,330, "第 关" ); outtextxy(160,330,str); setfont(30,0, "隶书" ); outtextxy(150,400, "目标分:" ); outtextxy(270,400,str2); Sleep(1000); cleardevice(); } //显示分数 void show_word() { char str[4],str1[8],str2[8],scores[8],str3[4]; if (game.score>high) { high=game.score; save(&high); } putimage(0,0,game.img[0]); setfont(30,0, "隶书" ); setcolor(EGERGB(250, 210, 0)); setbkmode(TRANSPARENT); outtextxy(10,15, "最高记录: " ); outtextxy(10,55, "关卡: " ); outtextxy(150,55, "目标分: " ); sprintf (str, "%4d " , game.level); sprintf (str1, "%4d " , high); sprintf (str2, "%4d " , game.targets); sprintf (scores, "%4d " , game.score); sprintf (str3, "%d" , game.tool_number); setcolor(EGERGB(255, 255, 255)); outtextxy(60, 55, str); outtextxy(150, 15, str1); outtextxy(260, 55, str2); outtextxy(205, 102, scores); setfont(25,0, "隶书" ); outtextxy(410,1,str3); } void show_map() { if (game.level_state==1) show_level(); show_word(); int i,j; for (i=0; i<MAPSIZE; i++) { for (j=0; j<MAPSIZE; j++) if (!state[i][j]) { if (map[i][j]==1) putimage(j*45,i*45+250,game.img[map[i][j]]); else if (map[i][j]==2) putimage(j*45,i*45+250,game.img[map[i][j]]); else if (map[i][j]==3) putimage(j*45,i*45+250,game.img[map[i][j]]); else if (map[i][j]==4) putimage(j*45,i*45+250,game.img[map[i][j]]); else if (map[i][j]==5) putimage(j*45,i*45+250,game.img[map[i][j]]); else if (map[i][j]==6) putimage(j*45,i*45+250,game.img[map[i][j]]); } } } //音频 void musicu() { mciSendString( "close mymusicu" ,NULL,0,NULL); mciSendString( "open game\\unbelievable.mp3 alias mymusicu" ,NULL,0,NULL); mciSendString( "play mymusicu" ,NULL,0,NULL); } void musicc() { mciSendString( "close mymusicc" ,NULL,0,NULL); mciSendString( "open game\\click.mp3 alias mymusicc" ,NULL,0,NULL); mciSendString( "play mymusicc" ,NULL,0,NULL); } void musicf() { mciSendString( "close mymusicf" ,NULL,0,NULL); mciSendString( "open game\\fail.mp3 alias mymusicf" ,NULL,0,NULL); mciSendString( "play mymusicf" ,NULL,0,NULL); } void musicd() { mciSendString( "close mymusicd" ,NULL,0,NULL); mciSendString( "open game\\delt.mp3 alias mymusicd" ,NULL,0,NULL); mciSendString( "play mymusicd" ,NULL,0,NULL); } void musicb() { mciSendString( "close mymusicb" ,NULL,0,NULL); mciSendString( "open game\\bomb.mp3 alias mymusicb" ,NULL,0,NULL); mciSendString( "play mymusicb" ,NULL,0,NULL); } void musicp() { mciSendString( "close mymusicp" ,NULL,0,NULL); mciSendString( "open game\\popwindows.mp3 alias mymusicp" ,NULL,0,NULL); mciSendString( "play mymusicp" ,NULL,0,NULL); } void musicsheng() { mciSendString( "close mymusicsheng" ,NULL,0,NULL); mciSendString( "open game\\成功.mp3 alias mymusicsheng" ,NULL,0,NULL); mciSendString( "play mymusicsheng" ,NULL,0,NULL); } //判断此位置是否符合 int avaliable( int x, int y) { if (x<0||x>=MAPSIZE||y<0||y>=MAPSIZE||state[y][x]) return 0; else return 1; } //搜索周围相同颜色 void search_map( int x, int y, int target) { if ((!avaliable(x,y))||map[y][x]!=target) return ; state[y][x]= true ; state1[y][x]= true ; game.counts++; search_map(x-1,y,target); search_map(x+1,y,target); search_map(x,y-1,target); search_map(x,y+1,target); } //搜索到相同颜色后调整位置 void adjust_map() { int i,j,r,k; int temp[10][10]; for (j=MAPSIZE-1; j>=0; j--) { int m=0; int flag=1; k=j; for (i=MAPSIZE-1; i>=0; i--) if (state[i][j]== false ) flag=0; if (flag==1) { if (k==MAPSIZE-1) continue ; while (1) { if (k==MAPSIZE-1) break ; for (i=MAPSIZE-1; i>=0; i--) { map[i][k]=map[i][k+1]; state[i][k]=state[i][k+1]; state[i][k+1]= true ; } k++; } } for (i=MAPSIZE-1; i>=0; i--) if (!state[i][j]) { temp[m][j]=map[i][j]; m++; } r=MAPSIZE-1; for (i=0; i<m; i++) { map[r][j]=temp[i][j]; state[r][j]= false ; r--; } for (; r>=0; r--) { state[r][j]= true ; } } } //判断是否通过本关 int end_map() { int i,j,target; int x; int y; int flag=1; for (i=0; i<MAPSIZE; i++) for (j=0; j<MAPSIZE; j++) if (!state[i][j]) { x=j; y=i; target=map[y][x]; if ((avaliable(x+1,y)&&map[y][x+1]==target)||(avaliable(x-1,y)&&map[y][x-1]==target) ||(avaliable(x,y-1)&&map[y-1][x]==target)||(avaliable(x,y+1)&&map[y+1][x]==target)) { flag=0; break ; } } return flag; } //弹出炸弹窗口,选择是否使用道具 int bomb_pop() { mouse_msg ms; while (1) { show_map(); putimage(88, 280, game.img[9]); while (mousemsg()) { ms=getmouse(); if (ms.is_left()&&ms.is_down()) { if (ms.x>105&&ms.x<230&&ms.y>380&&ms.y<425) { musicc(); return 0; } if (ms.x>240&&ms.x<370&&ms.y>380&&ms.y<420) { musicc(); return 1; } } } Sleep(500); } } //点击要炸裂的某一列 void bomb_map() { int i; int x,y; mouse_msg msg; while (1) { show_map(); while (mousemsg()) { msg=getmouse(); if (msg.is_left()&&msg.is_down()) { musicc(); x=msg.x/45; y=(msg.y-250)/45; if (!avaliable(x,y)) continue ; for (i=0; i<MAPSIZE; i++) if (state[i][x]== false ) { state[i][x]= true ; game.counts++; } return ; } } Sleep(500); } } //打印出要消除图形的碎片 void flash_map( int target) { int i,j; show_map(); for (i=0; i<MAPSIZE; i++) for (j=0; j<MAPSIZE; j++) if (state1[i][j]== true ) { musicd(); if (target==1) putimage(j*45,i*45+250,game.img[11]); else if (target==2) putimage(j*45,i*45+250,game.img[12]); else if (target==3) putimage(j*45,i*45+250,game.img[13]); else if (target==4) putimage(j*45,i*45+250,game.img[14]); else if (target==5) putimage(j*45,i*45+250,game.img[15]); else if (target==6) putimage(j*45,i*45+250,game.img[16]); } Sleep(200); } //碎片消除后,恢复以前状态 void state1_back() { int i,j; for (i=0; i<MAPSIZE; i++) for (j=0; j<MAPSIZE; j++) state1[i][j]= false ; } void play_game() { init_map(); int target; int x,y; int i,j; mouse_msg msg; if (game.state==0) //重新开始游戏时将现有分数置为0 game.score=0; else game.score=game.score; while (1) { show_map(); game.level_state=0; while (mousemsg()) { msg=getmouse(); if (msg.is_left()&&msg.is_down()) { musicc(); if (msg.x>400&&msg.x<450&&msg.y>=40&&msg.y<100&&game.tool_number>=5) //使用炸弹 { musicp(); if (bomb_pop()) { bomb_map(); musicb(); adjust_map(); game.score=game.score+game.counts*20; game.tool_number=game.tool_number-5; show_map(); game.counts=0; } } else { x=msg.x/45; y=(msg.y-250)/45; if (!avaliable(x,y)) continue ; target=map[y][x]; if ((avaliable(x+1,y)&&map[y][x+1]==target)||(avaliable(x-1,y)&&map[y][x-1]==target) ||(avaliable(x,y-1)&&map[y-1][x]==target)||(avaliable(x,y+1)&&map[y+1][x]==target)) { search_map(x,y,target); game.score=game.score+game.counts*20; if (game.counts>=6) { game.tool_number=game.tool_number+3; musicu(); } flash_map(target); adjust_map(); } game.counts=0; show_map(); Sleep(100); state1_back(); if (end_map()) { if (game.score>=game.targets) { musicsheng(); game.game_state=2; return ; } else if (game.score<game.targets) { musicf(); game.game_state=-1; return ; } } } } } } } //领取魔法鱼 void get_tool() { mouse_msg ms; while (1) { putimage(0,0,game.img[7]); putimage_transparent(NULL,game.img[17],88,280,0); while (mousemsg()) { ms=getmouse(); if (ms.is_left()&&ms.is_down()) { if (ms.x>170&&ms.x<300&&ms.y>380&&ms.y<420) { musicc(); game.tool_number=10; game.tool_state=0; return ; } } } Sleep(500); } } //游戏指南界面 void inter_game() { mouse_msg ms; while (1) { putimage(0,0,game.img[18]); while (mousemsg()) { ms=getmouse(); if (ms.is_left()&&ms.is_down()) { musicc(); game.game_state=0; return ; } } Sleep(500); } } void start_game() { mouse_msg ms; if (game.start_state==1) { putimage(0,0,game.img[19]); Sleep(500); putimage(0,0,game.img[20]); Sleep(500); game.start_state=0; } while (1) { putimage(0,0,game.img[7]); while (mousemsg()) { ms=getmouse(); if (ms.is_left()&&ms.is_down()) { musicc(); if (ms.x>140&&ms.x<310&&ms.y>370&&ms.y<410) { if (game.tool_state==1) { musicp(); get_tool(); } game.game_state=1; game.level_state=1; return ; } if (ms.x>140&&ms.x<310&&ms.y>420&&ms.y<460) { game.game_state=3; return ; } if (ms.x>140&&ms.x<310&&ms.y>470&&ms.y<500) { game.game_state=5; return ; } } } Sleep(500); } } void fail_again() { int i,j; int target; int x,y; int flag=0; mouse_msg msg; for (i=0; i<MAPSIZE; i++) for (j=0; j<MAPSIZE; j++) { state[i][j]= false ; map[i][j]=map2[i][j]; } while (1) { show_map(); while (mousemsg()) { msg=getmouse(); if (msg.is_left()&&msg.is_down()) { musicc(); if (msg.x>400&&msg.x<450&&msg.y>=40&&msg.y<100&&game.tool_number>=5) { musicp(); if (bomb_pop()) { bomb_map(); musicb(); adjust_map(); game.score=game.score+game.counts*20; game.tool_number=game.tool_number-5; show_map(); game.counts=0; } } else { x=msg.x/45; y=(msg.y-250)/45; if (!avaliable(x,y)) continue ; target=map[y][x]; if ((avaliable(x+1,y)&&map[y][x+1]==target)||(avaliable(x-1,y)&&map[y][x-1]==target) ||(avaliable(x,y-1)&&map[y-1][x]==target)||(avaliable(x,y+1)&&map[y+1][x]==target)) { search_map(x,y,target); game.score=game.score+game.counts*20; if (game.counts>=6) { game.tool_number=game.tool_number+3; musicu(); } flash_map(target); adjust_map(); } game.counts=0; show_map(); Sleep(100); state1_back(); if (end_map()) { if (game.score>=game.targets) { musicsheng(); game.game_state=2; return ; } else if (game.score<game.targets) { musicf(); game.game_state=-1; return ; } } } } } } } void end_game() { mouse_msg msg; while (1) { show_map(); putimage(100, 320, game.img[8]); setfont(30,0, "隶书" ); setcolor(EGERGB(255,255,255)); setbkmode(TRANSPARENT); outtextrect(120,330, 180,450, "闯关失败,是否重闯本关?" ); while (mousemsg()) { msg = getmouse(); if (msg.is_left()&&msg.is_down()) { if (msg.x>115&&msg.x<222&&msg.y>400&&msg.y<430) { musicc(); game.game_state=0; game.state=0; game.level=1; game.targets=1500; game.score=0; return ; } if (msg.x>230&&msg.x<336&&msg.y>400&&msg.y<430) { musicc(); if (game.level>1) game.score=game.score-1500; else if (game.level==1) game.score=0; game.game_state=4; return ; } } } Sleep(500); } } void win_game() { mouse_msg msg; while (1) { show_map(); putimage_transparent(NULL,game.img[10],130,300,0); while (mousemsg()) { msg = getmouse(); if (msg.is_left()&&msg.is_down()) { musicc(); game.level++; game.targets=game.targets+1600; game.state=1; game.game_state=1; game.level_state=1; game.tool_number=game.tool_number+2; return ; } } Sleep(500); } } void play() { while (1) { if (game.game_state==0) start_game(); else if (game.game_state==1) play_game(); else if (game.game_state==-1) end_game(); else if (game.game_state==2) win_game(); else if (game.game_state==4) fail_again(); else if (game.game_state==5) inter_game(); else if (game.game_state==3) return ; } } int main() { initgraph(450,700); game.game_state=0; game.counts=0; game.score=0; game.targets=1500; game.level=1; game.tool_number=0; game.tool_state=1; game.start_state=1; init_img(); mciSendString( "open game\\under.mp3 alias mymusicg" ,NULL,0,NULL); mciSendString( "play mymusicg repeat" ,NULL,0,NULL); read(&high); play(); closegraph(); mciSendString( "close mymusicg repeat" ,NULL,0,NULL); return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/li__ly/article/details/103662193