使用easy-x可视化窗口实现扫雷小游戏

  • easy-x插件需要自己在官网下载(附上链接)
    https://easyx.cn/
  • 安装好插件后用vs2019打开
  • 然后话不多说,附上扫雷源码
#include <graphics.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
using namespace std;
class Sweep_Mines {
public:
    int row;
    int col;
    int num;
    int sweep_num;
    int swag;
    int map[100][100] = { 0 };
    char str[30][10];
    IMAGE img[4];
    Sweep_Mines(int col, int row, int num) {
        initgraph(col * 30 + 150, row * 30);
        mciSendString("open ./source/back_misic.mp3", 0, 0, 0);
        mciSendString("play ./source/back_music.mp3", 0, 0, 0);
        setbkcolor(WHITE);
        cleardevice();

        loadimage(&img[0], "./source/-1.png");
        loadimage(&img[1], "./source/0.png");
        loadimage(&img[2], "./source/1.png");
        loadimage(&img[3], "./source/2.png");
        this->col = col;
        this->row = row;
        this->num = num;
        sweep_num = 0;
        swag = 0;

        initial_map();
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                map[i][j] += 31;
            }
        }
        Draw_map();

    }
    void Draw_map() {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                switch (map[i][j]) {

                case -1:
                    putimage(j * 30, i * 30, &img[0]);
                    break;
                case 0:
                    putimage(j * 30, i * 30, &img[2]);
                    break;

                }
                if (map[i][j] >= 30 && map[i][j] < 60) {
                    putimage(j * 30, i * 30, &img[1]);
                }
                if (map[i][j] >= 60) {
                    putimage(j * 30, i * 30, &img[3]);
                }
                if (map[i][j] > 0 && map[i][j] < 30) {
                    putimage(j * 30, i * 30, &img[2]);
                    sprintf_s(str[map[i][j]], "%d", map[i][j]);
                    settextstyle(20, 20, "楷体");
                    settextcolor(RGB(rand() % 256, rand() % 256, rand() % 256));

                    outtextxy(j * 30 + 5, i * 30 + 5, str[map[i][j]]);
                }
            }
        }

    }
    void initial_map() {
        char str1[20] = "";
        sprintf_s(str1, "雷:%d", num);
        settextstyle(20, 15, "黑体");
        settextcolor(RED);
        outtextxy(col * 30 + 30, row * 30 / 2, str1);
        srand((unsigned)time(NULL));
        for (int i = 0; i < num; i++) {
            int x = rand() % row;
            int y = rand() % col;
            while (map[x][y] == -1) {
                x = rand() % row;
                y = rand() % col;
            }
            map[x][y] = -1;


        }
        for (int x = 0; x < row; x++)
            for (int y = 0; y < col; y++) {
                if (map[x][y] == -1) {
                    if (x - 1 >= 0) {
                        if (map[x - 1][y] != -1) {
                            map[x - 1][y]++;
                        }

                    }
                    if (x - 1 >= 0 && y - 1 >= 0) {
                        if (map[x - 1][y - 1] != -1) {
                            map[x - 1][y - 1]++;
                        }
                    }
                    if (x - 1 >= 0 && y + 1 < col) {
                        if (map[x - 1][y + 1] != -1) {
                            map[x - 1][y + 1]++;
                        }
                    }
                    if (x + 1 < row) {
                        if (map[x + 1][y] != -1) {
                            map[x + 1][y]++;
                        }
                    }
                    if (x + 1 < row && y - 1 >= 0) {
                        if (map[x + 1][y - 1] != -1) {
                            map[x + 1][y - 1]++;
                        }
                    }
                    if (x + 1 < row && y + 1 < col) {
                        if (map[x + 1][y + 1] != -1) {
                            map[x + 1][y + 1]++;
                        }
                    }
                    if (y - 1 >= 0) {
                        if (map[x][y - 1] != -1) {
                            map[x][y - 1]++;
                        }
                    }
                    if (y + 1 < col) {
                        if (map[x][y + 1] != -1) {
                            map[x][y + 1]++;
                        }
                    }
                }
            }

    }
    int victory() {
        if (num == swag && num == sweep_num) {
            return 1;
        }
        return 0;
    }
    void Play_Game() {
        MOUSEMSG msg;
        while (1) {
            msg = GetMouseMsg();
            int x, y;
            switch (msg.uMsg) {
            case WM_LBUTTONDOWN:
                x = (int)msg.x / 30;
                y = (int)msg.y / 30;
                if (map[y][x] >= 30 && map[y][x] < 60) {
                    int c = rand() % col / 4;
                    int r = rand() % row / 4;
                    map[y][x] -= 31;
                    for (int i = x; i >= 0 && i >= x - c; i--) {
                        for (int j = y; j >= 0 && j >= y - r; j--) {
                            if (map[j][i] > 30 && map[j][i] < 60) {
                                map[j][i] -= 31;
                            }
                        }
                        for (int j = y + 1; j < row && j <= y + r; j++) {
                            if (map[j][i] > 30 && map[j][i] < 60) {
                                map[j][i] -= 31;
                            }
                        }
                    }
                    for (int i = x + 1; i <= x + c && i < col; i++) {
                        for (int j = y; j >= 0 && j >= y - r; j--) {
                            if (map[j][i] > 30 && map[j][i] < 60) {
                                map[j][i] -= 31;
                            }
                        }
                        for (int j = y + 1; j < row && j <= y + r; j++) {
                            if (map[j][i] > 30 && map[j][i] < 60) {
                                map[j][i] -= 31;
                            }
                        }
                    }
                    if (map[y][x] == -1) {
                        for (int i = 0; i < row; i++) {
                            for (int j = 0; j < col; j++) {
                                if (map[j][i] >= 30 && map[j][i] < 60) {
                                    map[j][i] -= 31;
                                }
                                if (map[j][i] >= 60) {
                                    map[j][i] -= 61;
                                }
                            }

                        }
                        Draw_map();
                        MessageBox(NULL, "GAME OVER", "消息", MB_OK);

                        return;
                    }
                }
                break;
            case WM_RBUTTONDOWN:
                x = (int)msg.x / 30;
                y = (int)msg.y / 30;
                if (map[y][x] >= 30 && map[y][x] < 60) {
                    map[y][x] += 30;
                    swag++;
                    if (map[y][x] == 60) {
                        sweep_num++;
                    }
                    break;
                }
                if (map[y][x] >= 60) {
                    map[y][x] -= 30;
                    swag--;
                    break;
                }

            }

            Draw_map();
            if (victory()) {
                MessageBox(NULL, "Vitory!", "消息", MB_OK);
                return;
            }
        }
    }
};
int main()
{
    Sweep_Mines game1(10, 10, 5);
    game1.Play_Game();
    closegraph;
}
  • 其中资源文件是放在这个目录下的:
    在这里插入图片描述
  • 最后运行完的结果是这个样子的(还有音乐哦):
    在这里插入图片描述
    在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值