C语言代码实现简单的扫雷小游戏
程序员文章站
2022-04-05 16:58:48
c语言+easyx实现扫雷,供大家参考,具体内容如下主要思路就是通过一个二维数组存储不同的数来代表0到8等具体的图片,再配合鼠标的位置和点击情况,来改变数组某一项的值,而显示不同的图片。水平有限,有些...
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; } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。