C语言+EASYX实现扫雷,供大家参考,具体内容如下
主要思路就是通过一个二维数组存储不同的数来代表0到8等具体的图片,再配合鼠标的位置和点击情况,来改变数组某一项的值,而显示不同的图片。
水平有限,有些地方的代码过于复杂和繁琐,有待优化;有些功能的实现也不是很完整和合理,敬请指正。
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
|
#include <stdio.h> //标准的输入输出头文件。 #include <graphics.h> //EasyX图形界面。 #include <time.h> #include <stdlib.h> #include<mmsystem.h> #pragma comment(lib,"winmm.lib")//导入静态库 int f2=0; //标志信号 int flag=0; //标志信号 int ROW = 0; //棋盘行数 int COL = 0; //棋盘列数 int NUM = 0; //雷的个数 int size; //图片尺寸 HWND hwnd1; int count = 0; //已掀开的数量; void BGMPLAY( void ) //播放音乐函数 { mciSendString(L "open ./image/XXX.mp3 alias bgm" ,0,0,0); //XX可替换为自己的歌曲 mciSendString(L "play bgm repeat" ,0,0,0); } struct rayxy //地雷位置 { int x; int y; }xy[200]; //地雷位置 IMAGE img[21]; //存储图像 void welcome() { initgraph( 640, 480); loadimage(&img[9], L "./image/开始界面1.jpg" , 640, 480); //开始界面 loadimage(&img[15], L "./image/菜单界面.jpg" , 640, 480); //菜单选择界面 if (!flag) { putimage(0, 0, &img[9]); Sleep(1000); } cleardevice(); putimage(0, 0, &img[15]); //模式选择 MOUSEMSG msg = { 0 }; const int x1=9,y1=8, x2=290,y2=8,x3=9,y3=330,x4=290,y4=330, h=306, w=265; int sx = 0, sy = 0; //模式选择 while (1) { HWND hwnd; msg = GetMouseMsg(); if (msg.uMsg == WM_LBUTTONDOWN) { sx = msg.x; sy = msg.y; break ; } } if ((sx >= x1 && sy >= y1)&& (sx <= x1+w && sy <= y1+h)) { ROW = 9; COL = 9; NUM = 10; size = 70; } if ((sx >= x2 && sy >= y2) && (sx <= x2 + w && sy <= y2 + h)) { ROW = 16; COL = 30; NUM = 99; size = 43; } if ((sx >= x3 && sy >= y3) && (sx <= x3 + w && sy <= y3 + h)) { ROW = 16; COL = 16; NUM = 40; size = 48; } if ((sx >= x4 && sy >= y4) && (sx <= x4 + w && sy <= y4 + h)) { MessageBox(hwnd1, L "敬请期待!!!" , L "提示" , MB_OK); f2 = 1; flag = 1; Sleep(1000); } } //欢迎界面 int map[30][30]; //棋盘地图 void imgplay( void ) { loadimage(&img[0], L "./image/空白.jpg" ,size, size); //空白 loadimage(&img[1], L "./image/1.jpg" , size, size); loadimage(&img[2], L "./image/2.jpg" , size, size); loadimage(&img[3], L "./image/3.jpg" , size, size); loadimage(&img[4], L "./image/4.jpg" , size, size); loadimage(&img[5], L "./image/5.jpg" , size, size); loadimage(&img[6], L "./image/6.jpg" , size, size); loadimage(&img[7], L "./image/7.jpg" , size, size); loadimage(&img[8], L "./image/8.jpg" , size, size); //8 loadimage(&img[16], L "./image/问号.jpg" , size, size); //问号 loadimage(&img[10], L "./image/覆盖.jpg" , size, size); //覆盖 loadimage(&img[11], L "./image/标记.jpg" , size, size); //标记 loadimage(&img[12], L "./image/地雷.jpg" , size, size); //地雷 loadimage(&img[13], L "./image/胜利.jpg" , size, size); //胜利 loadimage(&img[14], L "./image/问号.jpg" , size, size); //问号 loadimage(&img[16], L "./image/9.jpg" , size, size); //9 loadimage(&img[17], L "./image/0.jpg" , size, size); //0 loadimage(&img[18], L "./image/冒号.jpg" , size, size); //冒号 loadimage(&img[20], L "./image/标记错误.jpg" , size, size);/标记错误 } void gameinit() { int i, j; srand ((unsigned int ) time (NULL)); //为随机布雷提供随机数 for (i = 0; i < ROW + 2; i++) { for (j = 0; j < COL + 2; j++) { map[i][j] = 0; } } int r, c, n = 0; while (n < NUM) { r = rand () % ROW + 1; c = rand () % COL + 1; if (map[r][c] != -1) { map[r][c] = -1; xy[n].x = r; xy[n].y = c; n++; } else { continue ; } } for (i = 1; i <= ROW; i++) { for (j = 1; j <= COL; j++) { if (map[i][j] != -1) { for (r = i - 1; r <= i + 1; r++) for (c = j - 1; c <= j + 1; c++) { if (map[r][c] == -1) { map[i][j]++; } } } } } for (i = 1; i <= ROW; i++) { for (j = 1; j <= COL; j++) { map[i][j] += 20; } } } void drawgraph() { int i, j; for (i = 1; i <= ROW; i++) { for (j = 1; j <= COL; j++) { if (map[i][j] == -1) { putimage((j - 1) * size, (i - 1) * size, &img[12]); //地雷 } else if (map[i][j] >= 0 && map[i][j] <= 8) { putimage((j - 1) * size, (i - 1) * size, &img[map[i][j]]); //显示数字和空白 } else if (map[i][j] >= 19 && map[i][j] <= 28) { putimage((j - 1) * size, (i - 1) * size, &img[10]); //覆盖 } else if (map[i][j] >= 29&&map[i][j]<=38) { putimage((j - 1) * size, (i - 1) * size, &img[11]); //标记 } else if (map[i][j]>=39&&map[i][j]<=48) { putimage((j - 1) * size, (i - 1) * size, &img[14]); //问号 } if (map[i][j] == -2) { putimage((j - 1) * size, (i - 1) * size, &img[13]); //胜利 } } } } void draw( int i, int j) { if (map[i][j] == -1) { putimage((j - 1) * size, (i - 1) * size, &img[12]); //地雷 } else if (map[i][j] >= 0 && map[i][j] <= 8) { putimage((j - 1) * size, (i - 1) * size, &img[map[i][j]]); //显示数字和空白 } else if (map[i][j] >= 19 && map[i][j] <= 28) { putimage((j - 1) * size, (i - 1) * size, &img[10]); //覆盖 } else if (map[i][j] >= 29&&map[i][j]<=38) { putimage((j - 1) * size, (i - 1) * size, &img[11]); //标记 } else { putimage((j - 1) * size, (i - 1) * size, &img[14]); //问号 } } void blankopen( int r, int c) { int a, b; if (map[r][c] == 20) { count++; map[r][c] -= 20; } for (a = r - 1; a <= r + 1; a++) { for (b = c - 1; b <= c + 1; b++) { if (a >= 1 && a <= ROW && b >= 1 && b <= COL) { if (map[a][b] >= 20 && map[a][b] <= 28) { map[a][b] -= 20; count++; } } } } } int playgame() { MOUSEMSG msg = { 0 }; int r, c; //while (1) //{ msg = GetMouseMsg(); switch (msg.uMsg) { case WM_LBUTTONDOWN: c = msg.x / size + 1; r = msg.y / size + 1; if (map[r][c] >= 29&&map[r][c]<=38) { map[r][c] -= 10; } if (map[r][c] >= 39 && map[r][c] <= 48) { map[r][c] -= 20; } if (map[r][c] >= 19 && map[r][c] <= 28) { if (map[r][c] == 20) { blankopen(r, c); } else { if (map[r][c] != 19) count++; map[r][c] -= 20; } } if (map[r][c] == 0) blankopen(r, c); draw(r, c); return map[r][c]; //break; case WM_RBUTTONDOWN: c = msg.x / size + 1; r = msg.y / size + 1; if (map[r][c] >= 19 && map[r][c] <= 28) { map[r][c] += 10; } else if (map[r][c] >= 29&&map[r][c]<=38) { map[r][c] += 10; } else { map[r][c] -= 20; } draw(r, c); return map[r][c]; //break; } //} } int timerec( void ) { time_t timep; struct tm p; time (&timep); gmtime_s(&p, &timep); int t = p.tm_min * 60 + p.tm_sec; return t; } void timeplay( int t) { int min=0 ,sec=0; printf ( "%d\n" , t); min = t / 60; putimage((COL - 3) * size, (ROW)*size, &img[18]); switch (min / 10) { case 0:putimage((COL - 5) * size, (ROW ) * size, &img[17]); break ; case 1:putimage((COL - 5) * size, (ROW ) * size, &img[1]); break ; case 2:putimage((COL - 5) * size, (ROW ) * size, &img[2]); break ; case 3:putimage((COL - 5) * size, (ROW ) * size, &img[3]); break ; case 4:putimage((COL - 5) * size, (ROW ) * size, &img[4]); break ; case 5:putimage((COL - 5) * size, (ROW ) * size, &img[5]); break ; case 6:putimage((COL - 5) * size, (ROW ) * size, &img[6]); break ; case 7:putimage((COL - 5) * size, (ROW ) * size, &img[7]); break ; case 8:putimage((COL - 5) * size, (ROW ) * size, &img[8]); break ; case 9:putimage((COL - 5) * size, (ROW ) * size, &img[16]); break ; default :putimage((COL - 5) * size, (ROW ) * size, &img[10]); break ; } switch (min % 10) { case 0:putimage((COL - 4) * size, (ROW ) * size, &img[17]); break ; case 1:putimage((COL - 4) * size, (ROW ) * size, &img[1]); break ; case 2:putimage((COL - 4) * size, (ROW ) * size, &img[2]); break ; case 3:putimage((COL - 4) * size, (ROW ) * size, &img[3]); break ; case 4:putimage((COL - 4) * size, (ROW ) * size, &img[4]); break ; case 5:putimage((COL - 4) * size, (ROW ) * size, &img[5]); break ; case 6:putimage((COL - 4) * size, (ROW ) * size, &img[6]); break ; case 7:putimage((COL - 4) * size, (ROW ) * size, &img[7]); break ; case 8:putimage((COL - 4) * size, (ROW ) * size, &img[8]); break ; case 9:putimage((COL - 4) * size, (ROW ) * size, &img[16]); break ; default :putimage((COL - 4) * size, (ROW ) * size, &img[10]); break ; } sec = t % 60; switch (sec / 10) { case 0:putimage((COL - 2) * size, (ROW ) * size, &img[17]); break ; case 1:putimage((COL - 2) * size, (ROW ) * size, &img[1]); break ; case 2:putimage((COL - 2) * size, (ROW ) * size, &img[2]); break ; case 3:putimage((COL - 2) * size, (ROW ) * size, &img[3]); break ; case 4:putimage((COL - 2) * size, (ROW ) * size, &img[4]); break ; case 5:putimage((COL - 2) * size, (ROW ) * size, &img[5]); break ; case 6:putimage((COL - 2) * size, (ROW ) * size, &img[6]); break ; case 7:putimage((COL - 2) * size, (ROW ) * size, &img[7]); break ; case 8:putimage((COL - 2) * size, (ROW ) * size, &img[8]); break ; case 9:putimage((COL - 2) * size, (ROW ) * size, &img[16]); break ; default :putimage((COL - 2) * size, (ROW ) * size, &img[10]); break ; } switch (sec % 10) { case 0:putimage((COL - 1) * size, (ROW ) * size, &img[17]); break ; case 1:putimage((COL - 1) * size, (ROW ) * size, &img[1]); break ; case 2:putimage((COL - 1) * size, (ROW ) * size, &img[2]); break ; case 3:putimage((COL - 1) * size, (ROW ) * size, &img[3]); break ; case 4:putimage((COL - 1) * size, (ROW ) * size, &img[4]); break ; case 5:putimage((COL - 1) * size, (ROW ) * size, &img[5]); break ; case 6:putimage((COL - 1) * size, (ROW ) * size, &img[6]); break ; case 7:putimage((COL - 1) * size, (ROW ) * size, &img[7]); break ; case 8:putimage((COL - 1) * size, (ROW ) * size, &img[8]); break ; case 9:putimage((COL - 1) * size, (ROW ) * size, &img[16]); break ; default :putimage((COL - 1) * size, (ROW ) * size, &img[10]); break ; } for ( int i = 0; i < COL-5; i++) { putimage((i) * size, (ROW)*size, &img[20]); } } int main() { BGMPLAY(); //播放初始音乐 int i = 0, j = 0, MAX = 0; srand ((unsigned int ) time (NULL)); loop: loop1: f2 = 0; welcome(); if (f2) goto loop1; imgplay(); count = 0; hwnd1 = initgraph(COL * size, (ROW+1) * size); gameinit(); MAX = ROW * COL - NUM; time_t timep; struct tm p; time (&timep); gmtime_s(&p,&timep); int t1 = timerec(); while (1) { drawgraph(); timeplay(timerec() - t1); if (playgame() == -1) { for (i = 0; i < NUM; i++) { map[xy[i].x][xy[i].y] = -1; } drawgraph(); Sleep(1000); MessageBox(hwnd1, L "很遗憾!" , L "提示" , MB_OK); int x = MessageBox(hwnd1, L "回到主菜单" , L "Minesweeper" , MB_OKCANCEL); if (x == 1) { flag = 1; goto loop; } break ; } if (count == MAX) { for (i = 0; i < NUM; i++) { map[xy[i].x][xy[i].y] = -2; } drawgraph(); Sleep(1000); MessageBox(hwnd1, L "Very good!!!" , L "提示" , MB_OK); int y = MessageBox(hwnd1, L "回到主菜单" , L "Minesweeper" , MB_OKCANCEL); if (y) { flag = 1; goto loop; } else { break ; } } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/Joker15517/article/details/114947461