#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;
}
- 其中资源文件是放在这个目录下的:
- 最后运行完的结果是这个样子的(还有音乐哦):