[C/C++游戏编码教程]:扫雷游戏

1.效果图

[C/C++游戏编码教程]:扫雷游戏

2.主要代码

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<time.h>

HWND hwnd;
int win;

//函数声明
void drawMap(int map[][12], IMAGE *img);
int mouseDown(int map[][12]);
void swap(int map[][12], int mi, int mj);

//初始化游戏
void initGame()
{
	initgraph(500, 500);

	srand((unsigned int)time(NULL));

	//定义二维数组
	int map[12][12] = { 0 };

	int m, n, i, j;
	//埋雷
	for (n = 0; n < 10;)
	{
		i = rand() % 10 + 1;
		j = rand() % 10 + 1;
		if (map[i][j] == 0)
		{
			map[i][j] = -1;	//-1表示雷
			n++;
		}
	}
	//埋数字
	for (i = 1; i <= 10; i++)
	{
		for (j = 1; j <= 10; j++)
		{
			//不是雷才埋数字
			if (map[i][j] != -1)
			{
				for (m = i - 1; m <= i + 1; m++)
				{
					for (n = j - 1; n <= j + 1; n++)
					{
						//当前数字周围有几个雷,该位置就是几
						if (map[m][n] == -1)
						{
							map[i][j]++;
						}
					}
				}
			}
		}
	}

	//贴图技术
	IMAGE img[12];

	loadimage(&img[0], "0.jpg", 50, 50);
	loadimage(&img[1], "1.jpg", 50, 50);
	loadimage(&img[2], "2.jpg", 50, 50);
	loadimage(&img[3], "3.jpg", 50, 50);
	loadimage(&img[4], "4.jpg", 50, 50);
	loadimage(&img[5], "5.jpg", 50, 50);
	loadimage(&img[6], "6.jpg", 50, 50);
	loadimage(&img[7], "7.jpg", 50, 50);
	loadimage(&img[8], "8.jpg", 50, 50);
	loadimage(&img[9], "雷.jpg", 50, 50);
	loadimage(&img[10], "地图.jpg", 50, 50);
	loadimage(&img[11], "标记.jpg", 50, 50);

	hwnd = GetHWnd();	//窗口前置
	win = 0;
	while (1)
	{
		drawMap(map, img);
		if (mouseDown(map) == 9)
		{
			drawMap(map, img);
			MessageBox(hwnd, "炸弹来咯!", "BOOM", MB_OK);
			return;
		}
		if (win == 90)
		{
			drawMap(map, img);
			MessageBox(hwnd, "你赢了!", "YOU WIN", MB_OK);
			break;
		}
	}
}

//贴图
void drawMap(int map[][12],IMAGE *img)
{
	int i, j;
	for (i = 1; i <= 10; i++)
	{
		for (j = 1; j <= 10; j++)
		{
			int x = 50 * (j - 1);
			int y = 50 * (i - 1);
			//只要鼠标右键点击+30
			if (map[i][j] > 20)
			{
				putimage(x, y, &img[11]);
				continue;
			}

			//+10,把数字和雷盖起来
			switch (map[i][j])
			{
			case 9:
				putimage(x, y, &img[9]);
				break;
			case 10:
				putimage(x, y, &img[0]);
				break;
			case 11:
				putimage(x, y, &img[1]);
				break;
			case 12:
				putimage(x, y, &img[2]);
				break;
			case 13:
				putimage(x, y, &img[3]);
				break;
			case 14:
				putimage(x, y, &img[4]);
				break;
			case 15:
				putimage(x, y, &img[5]);
				break;
			case 16:
				putimage(x, y, &img[6]);
				break;
			case 17:
				putimage(x, y, &img[7]);
				break;
			case 18:
				putimage(x, y, &img[8]);
				break;
			default:
				putimage(x, y, &img[10]);
				break;
			}
		}
	}
}

//鼠标点击
int mouseDown(int map[][12])
{
	MOUSEMSG m;
	while (1)
	{
		m = GetMouseMsg();

		//鼠标点击位置的坐标转换成数组下标
		int mi = m.y / 50 + 1;
		int mj = m.x / 50 + 1;

		//当前鼠标消息:左键点击 or 右键点击
		switch (m.uMsg)
		{
		case WM_LBUTTONDOWN://左键按下
			//被点开的不能重复点开
			if (map[mi][mj] > 9)
			{
				continue;
			}
			//翻到0就翻开一片
			if (map[mi][mj] == 0)
			{
				//递归算法:
				swap(map, mi, mj);
			}
			else
			{
				//其他情况:直接翻开
				map[mi][mj] += 10;
				win++;
			}
			return map[mi][mj];
			break;
		case WM_RBUTTONDOWN:
			//标记
			if (map[mi][mj] < 9)
			{
				map[mi][mj] += 30;
			}
			//取消标记
			else if (map[mi][mj]>20)
			{
				map[mi][mj] -= 30;
			}
			else
			{
				continue;
			}
			return 0;
			break;
		}
	}
}

//递归:把0以及0周围的数字翻开

void swap(int map[][12], int mi, int mj)
{
	map[mi][mj] = 10;
	win++;
	for (int i = mi - 1; i <= mi + 1; i++)
	{
		for (int j = mj - 1; j <= mj + 1; j++)
		{
			//不能越界
			if (i >= 1 && i <= 10 && j >= 1 && j <= 10)
			{
				//翻开的是数字 <9
				if (map[i][j] < 9)
				{
					//如果还是0就递归
					if (map[i][j] == 0)
					{
						swap(map, i, j);
					}
					//否则,就翻开数字
					else
					{
						map[i][j] += 10;
						win++;
					}
				}
			}
		}
	}
}

int main()
{
	

	while (1)
	{
		initGame();
		if (MessageBox(hwnd, "再玩一局?", "游戏结束", MB_YESNO) == IDNO)
			break;
	}

	//_getch();
	closegraph();
	return 0;
}

 3.完整源码素材获取,C/C++学习交流:https://jq.qq.com/?_wv=1027&k=eWsAqn2h[C/C++游戏编码教程]:扫雷游戏https://jq.qq.com/?_wv=1027&k=eWsAqn2h

 

上一篇:LeetCode #169. Majority Element 数组 摩尔投票法


下一篇:python自学第7天(MJ培根)