Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Minesweeper Game with Easyx

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will introduce to you how Easyx implements the minesweeping game. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.

Code:

# include#include#include#include # include#pragma comment (lib, "winmm.lib") # define ROW 10 / / constant defining rows and rows # define COL 10 # define MineNum 10 / / number of mines # define ImgSize 40 / / Image size / / define picture resource IMAGE imgs [12]; void loadResource () {for (int I = 0; I)

< 12; i++) { char imgPath[50] = { 0 }; sprintf_s(imgPath, "./images/%d.jpg", i); loadimage(&imgs[i], imgPath, ImgSize, ImgSize); }} bool isfirst = true; //是不是第一次进来 //函数声明void show(int map[][COL]);void init(int map[][COL]);void draw(int map[][COL]);void mouseMsg(ExMessage* msg, int map[][COL]);void boomBlank(int map[][COL], int row, int col);int judge(int map[][COL], int row, int col);int main(){ //创建窗口 initgraph(400, 400/*,EW_SHOWCONSOLE*/); //播放开始音乐 mciSendString("open ./images/start.mp3 alias bgm", NULL, 0, NULL); mciSendString("play bgm", NULL, 0, NULL); //扫雷地图 int map[ROW][COL] = {0}; init(map); //游戏主循环 while (true) { //处理消息 ExMessage msg; while (peekmessage(&msg, EM_MOUSE)) { switch (msg.message) { case WM_LBUTTONDOWN: //鼠标左键和右键点击 case WM_RBUTTONDOWN: mouseMsg(&msg, map); int ret = judge(map,msg.y/ImgSize, msg.x / ImgSize); //点击之后判断 if (ret == -1) { draw(map); int select = MessageBox(GetHWnd(), "你这么牛,怎么输了呢?敢再来一把吗?", "low B!", MB_OKCANCEL); if (select == IDOK) //再来一把 { //重新初始化 init(map); } else //退出 { exit(0); } } else if(ret == 1) { } system("cls"); printf("judege:%d\n", ret); show(map); break; } } draw(map); } //show(map); getchar(); return 0;} void show(int map[][COL]){ for (int i = 0; i < ROW; i++) { for (int k = 0; k < COL; k++) { printf("%2d ", map[i][k]); } printf("\n"); }}//初始化数据void init(int map[][COL]){ loadResource(); //设置随机数种子 srand((unsigned)time(NULL)); //把map全部初始化为0 memset(map, 0, sizeof(int) * ROW * COL); //随机设置十个雷 用-1表示 for (int i = 0; i < MineNum; ) { //数组的有效下标 [0,9] int r = rand() % ROW; int c = rand() % COL; if (map[r][c] == 0) { map[r][c] = -1; //只有执行了这里的代码,才成功设置了雷 -1 i++; } } //把以雷为中心的九宫格数据都+1,雷除外 for (int i = 0; i < ROW; i++) { for (int k = 0; k < COL; k++) { //找到雷,并遍历雷所在的九宫格 if (map[i][k] == -1) { for (int r = i-1; r = 0 && c < COL) && map[r][c] != -1) { ++map[r][c]; } } } } } } //加密格子 for (int i = 0; i < ROW; i++) { for (int k = 0; k < COL; k++) { map[i][k] += 20; } }}//绘制void draw(int map[][COL]){ //贴图,根据map里面的数据,贴对应的图片 for (int i = 0; i < ROW; i++) { for (int k = 0; k < COL; k++) { if (map[i][k]>

= 0 & & map [I] [k] = 19 & & map [I] [k] = 39) /-1 + 20 + 20 {putimage (k * ImgSize, I * ImgSize, & imgs [11]) } / / Mouse operation data void mouseMsg (ExMessage* msg,int map [] [COL]) {/ / first calculate the subscript int r = msg- > y / ImgSize; int c = msg- > x / ImgSize of the corresponding array according to the coordinates of the mouse click. / / left-click to open the grid if (msg- > message = = WM_LBUTTONDOWN) {/ / when it can be opened, open if if not open (map [r] [c] > = 19 & & map [r] [c] message = = WM_RBUTTONDOWN) {PlaySound (". / images/rightClick.wav", NULL, SND_ASYNC | SND_FILENAME) / / can you tag: if it is not open, you can mark if (map [r] [c] > = 19 & & map [r] [c] = 39) {map [r] [c]-= 20 Click on the blank grid, all the blank cells around the chain burst and the number row col is the currently clicked grid void boomBlank (int map [] [COL], int row,int col) {/ / determine whether the row col position is a blank grid if (map [row] [col] = = 0) {for (int r = row-1; r)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report