C语言+EASYX实现扫雷,供大家参考,具体内容如下
主要思路就是通过一个二维数组存储不同的数来代表0到8等具体的图片,再配合鼠标的位置和点击情况,来改变数组某一项的值,而显示不同的图片。
水平有限,有些地方的代码过于复杂和繁琐,有待优化;有些功能的实现也不是很完整和合理,敬请指正。
#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; } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。