欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C+EasyX制作《扫雷》小游戏!让你在学习的过程中扫除掉所有的不开心!

程序员文章站 2022-03-06 14:26:45
...

游戏的实现比较简单,点击一个格子时,判断是否点击到雷区,如果没有的话就判断四周有多少颗雷,然后将雷的个数显示出来,我们需要的素材如下所示:
C+EasyX制作《扫雷》小游戏!让你在学习的过程中扫除掉所有的不开心!
如果有需要素材的小伙伴可以私信,我会发给你的 ^ _ ^

游戏的效果如下所示:

小游戏制作扫雷


代码如下所示:

#include <iostream>
#include <graphics.h>
#include <ctime>	
#include <cstdlib>
#include <mmsystem.h>
#pragma comment (lib,"winmm")


class Saolei
{
private:
	IMAGE black_bomb, red_bomb, red_flag, img[9];
	MOUSEMSG msg;

	const int Size = 675;
	int block_size;
	int flag;		//用来标记难度
	int red_num;

public:

	Saolei() { block_size = flag = 0; }
	~Saolei() {}

	void InitDraw();						//初始	画
	void Load_material();					//加载素材
	void InitDrawLineSimpleness();			//初始画
	void RunGameSimpleness();				//简单
	void InitDrawLineOrdinary();
	void RunGameOrdinary();					//一般
	void InitDrawLineDifficulty();
	void RunGameDifficulty();				//困难
	void Game();
};

int main()
{
	srand((unsigned)time(0));

	Saolei* saolei = new Saolei();

	saolei->InitDraw();
	saolei->Load_material();

	saolei->Game();

	MessageBox(nullptr, "        游 戏 结 束", "扫雷", MB_OK);

	delete saolei;

	return 0;
}


void Saolei::InitDraw()
{
	initgraph(Size + 200, Size, SHOWCONSOLE);
	loadimage(nullptr, "bj.jpg", Size, Size + 60);


	mciSendString("open bj.mp3", 0, 0, 0);
	mciSendString("play bj.mp3 repeat", 0, 0, 0);

	settextcolor(RED);
	setbkmode(TRANSPARENT);
	settextstyle(80, 35, "微软雅黑");
	outtextxy(230, 80, "扫   雷");
	settextstyle(40, 24, "楷体");
	outtextxy(200, Size - 300, "开 始 游 戏");

	MOUSEMSG msg;

	while (1)
	{
		msg = GetMouseMsg();

		if (msg.x - 200 < 260 && msg.x - 200 > 0 && msg.y - (Size - 300) < 40 && msg.y - (Size - 300) > 0)
		{
			settextcolor(RGB(0, 255, 255));
			outtextxy(200, Size - 300, "开 始 游 戏");
			if (msg.mkLButton)
				break;
		}

		else
		{
			settextcolor(RED);
			outtextxy(200, Size - 300, "开 始 游 戏");
		}
	}


	cleardevice();
	loadimage(nullptr, "bj.jpg", Size, Size + 60);

	settextcolor(RED);
	outtextxy(100, 100, "游戏难度:");
	outtextxy(300, 180, "简单");
	outtextxy(300, 300, "一般");
	outtextxy(300, 420, "困难");

	while (1)
	{
		msg = GetMouseMsg();

		if (msg.x - 300 < 95 && msg.x - 300 > 0 && msg.y - 180 < 40 && msg.y - 180 > 0)
		{

			block_size = Size / 9;
			flag = 0;

			settextcolor(RGB(0, 255, 255));
			outtextxy(300, 180, "简单");
			if (msg.mkLButton)
				break;
		}
		else if (msg.x - 300 < 95 && msg.x - 300 > 0 && msg.y - 300 < 40 && msg.y - 300 > 0)
		{

			block_size = Size / 15;
			flag = 1;

			settextcolor(RGB(0, 255, 255));
			outtextxy(300, 300, "一般");
			if (msg.mkLButton)
				break;
		}
		else if (msg.x - 300 < 95 && msg.x - 300 > 0 && msg.y - 420 < 40 && msg.y - 420 > 0)
		{

			block_size = Size / 25;
			flag = 2;

			settextcolor(RGB(0, 255, 255));
			outtextxy(300, 420, "困难");
			if (msg.mkLButton)
				break;
		}
		else
		{
			settextcolor(RED);
			outtextxy(300, 180, "简单");
			outtextxy(300, 300, "一般");
			outtextxy(300, 420, "困难");
		}
	}

	setbkcolor(RGB(128, 128, 128));
	cleardevice();

	switch (flag)
	{
	case 0: InitDrawLineSimpleness();
		break;
	case 1:	InitDrawLineOrdinary();
		break;
	case 2:	InitDrawLineDifficulty();
		break;
	}

	settextcolor(RED);

}

void Saolei::Load_material()
{
	loadimage(&black_bomb, "black_bomb.jpg", block_size - 2, block_size - 2);
	loadimage(&red_bomb, "red_bomb.jpg", block_size - 2, block_size - 2);
	loadimage(&red_flag, "red_flag.jpg", block_size - 2, block_size - 2);

	char buf[8] = { 0 };
	for (int i = 0; i < 9; i++)
	{
		memset(buf, 0, sizeof(buf));
		sprintf(buf, "%d.jpg", i);
		loadimage(&img[i], buf, block_size - 2, block_size - 2);
	}


	putimage(Size + 30, 100, &red_flag);

}

void Saolei::InitDrawLineSimpleness()
{

	for (int i = 0; i < 9; i++)
	{
		line(0, block_size * (i + 1), Size, block_size * (i + 1));
		line(block_size * (i + 1), 0, block_size * (i + 1), Size);
	}

	red_num = 20;
}

void Saolei::InitDrawLineOrdinary()
{
	for (int i = 0; i < 15; i++)
	{
		line(0, block_size * (i + 1), Size, block_size * (i + 1));
		line(block_size * (i + 1), 0, block_size * (i + 1), Size);
	}

	red_num = 60;
}

void Saolei::InitDrawLineDifficulty()
{
	for (int i = 0; i < 25; i++)
	{
		line(0, block_size * (i + 1), Size, block_size * (i + 1));
		line(block_size * (i + 1), 0, block_size * (i + 1), Size);
	}

	red_num = 100;
}


void Saolei::RunGameSimpleness()
{
	int arr[9][9]{ 0 };
	int flag_arr[9][9]{ 0 };

	for (int i = 0; i < red_num; i++)
	{

		int a = rand() % 9;
		int b = rand() % 9;

		arr[a][b] = 1;		//1 为雷	黑色的
	}

	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			std::cout << arr[i][j] << "  ";
		}
		std::cout << '\n';
	}

	char buf[64];
	while (1)
	{

		settextstyle(30, 12, "楷体");

		clearrectangle(Size + 100, 125, Size + 200, 150);
		memset(buf, 0, sizeof(buf));
		sprintf(buf, "剩余 %d", red_num);

		outtextxy(Size + 100, 125, buf);


		msg = GetMouseMsg();

		int x = msg.x / 75;
		int y = msg.y / 75;

		if (msg.mkLButton && msg.x < Size)
		{
			if (arr[y][x] == 1)
			{
				arr[y][x] = 2;
				putimage(x * 75 + 1, y * 75 + 1, &red_bomb);

				for (int i = 0; i < 9; i++)
				{
					for (int j = 0; j < 9; j++)
					{
						if (flag_arr[y][x] == 0 && arr[i][j] == 1)
						{
							putimage(j * 75 + 1, i * 75 + 1, &black_bomb);
							Sleep(100);
						}
					}
				}


				break;
			}

			else if (flag_arr[y][x] == 0 && arr[y][x] == 0)
			{
				int count = 0;
				for (int i = y - 1; i <= y + 1; i++)
				{
					for (int j = x - 1; j <= x + 1; j++)
					{
						if (i < 0 || i > 8)
							break;
						else if (j < 0 || j > 8)
							continue;
						else if (arr[i][j] == 1)
							count++;
					}
				}
				putimage(x * 75 + 1, y * 75 + 1, &img[count]);
				flag_arr[y][x] = 1;
			}

		}
		else if (msg.mkRButton && red_num > 0 && flag_arr[y][x] == 0 && msg.x < Size)
		{
			putimage(x * 75 + 1, y * 75 + 1, &red_flag);
			red_num--;
			flag_arr[y][x] = 1;
		}




	}
}





void Saolei::RunGameOrdinary()
{
	int arr[15][15]{ 0 };
	int flag_arr[15][15]{ 0 };

	for (int i = 0; i < red_num; i++)
	{

		int a = rand() % 15;
		int b = rand() % 15;

		arr[a][b] = 1;		//1 为雷	黑色的
	}

	for (int i = 0; i < 15; i++)
	{
		for (int j = 0; j < 15; j++)
		{
			std::cout << arr[i][j] << "  ";
		}
		std::cout << '\n';
	}

	char buf[64];
	while (1)
	{

		settextstyle(30, 12, "楷体");

		clearrectangle(Size + 100, 125, Size + 200, 150);
		memset(buf, 0, sizeof(buf));
		sprintf(buf, "剩余 %d", red_num);

		outtextxy(Size + 100, 125, buf);


		msg = GetMouseMsg();

		int x = msg.x / 45;
		int y = msg.y / 45;

		if (msg.mkLButton && msg.x < Size)
		{
			if (arr[y][x] == 1)
			{
				arr[y][x] = 2;
				putimage(x * 45 + 1, y * 45 + 1, &red_bomb);

				for (int i = 0; i < 15; i++)
				{
					for (int j = 0; j < 15; j++)
					{
						if (flag_arr[y][x] == 0 && arr[i][j] == 1)
						{
							putimage(j * 45 + 1, i * 45 + 1, &black_bomb);
							Sleep(100);
						}
					}
				}


				break;
			}

			else if (flag_arr[y][x] == 0 && arr[y][x] == 0)
			{
				int count = 0;
				for (int i = y - 1; i <= y + 1; i++)
				{
					for (int j = x - 1; j <= x + 1; j++)
					{
						if (i < 0 || i > 14)
							break;
						else if (j < 0 || j > 14)
							continue;
						else if (arr[i][j] == 1)
							count++;
					}
				}
				putimage(x * 45 + 1, y * 45 + 1, &img[count]);
				flag_arr[y][x] = 1;
			}

		}
		else if (msg.mkRButton && red_num > 0 && flag_arr[y][x] == 0 && msg.x < Size)
		{
			putimage(x * 45 + 1, y * 45 + 1, &red_flag);
			red_num--;
			flag_arr[y][x] = 1;
		}
	}
}

void Saolei::RunGameDifficulty()
{
	int arr[25][25]{ 0 };
	int flag_arr[25][25]{ 0 };

	for (int i = 0; i < red_num; i++)
	{

		int a = rand() % 25;
		int b = rand() % 25;

		arr[a][b] = 1;		//1 为雷	黑色的
	}

	for (int i = 0; i < 25; i++)
	{
		for (int j = 0; j < 25; j++)
		{
			std::cout << arr[i][j] << "  ";
		}
		std::cout << '\n';
	}

	char buf[64];
	while (1)
	{

		settextstyle(30, 12, "楷体");

		clearrectangle(Size + 100, 125, Size + 200, 150);
		memset(buf, 0, sizeof(buf));
		sprintf(buf, "剩余 %d", red_num);

		outtextxy(Size + 100, 125, buf);


		msg = GetMouseMsg();

		int x = msg.x / 27;
		int y = msg.y / 27;

		if (msg.mkLButton && msg.x < Size)
		{
			if (arr[y][x] == 1)
			{
				arr[y][x] = 2;
				putimage(x * 27 + 1, y * 27 + 1, &red_bomb);

				for (int i = 0; i < 25; i++)
				{
					for (int j = 0; j < 25; j++)
					{
						if (flag_arr[y][x] == 0 && arr[i][j] == 1)
						{
							putimage(j * 27 + 1, i * 27 + 1, &black_bomb);
							Sleep(100);
						}
					}
				}


				break;
			}

			else if (flag_arr[y][x] == 0 && arr[y][x] == 0)
			{
				int count = 0;
				for (int i = y - 1; i <= y + 1; i++)
				{
					for (int j = x - 1; j <= x + 1; j++)
					{
						if (i < 0 || i > 24)
							break;
						else if (j < 0 || j > 24)
							continue;
						else if (arr[i][j] == 1)
							count++;
					}
				}
				putimage(x * 27 + 1, y * 27 + 1, &img[count]);
				flag_arr[y][x] = 1;
			}

		}
		else if (msg.mkRButton && red_num > 0 && flag_arr[y][x] == 0 && msg.x < Size)
		{
			putimage(x * 27 + 1, y * 27 + 1, &red_flag);
			red_num--;
			flag_arr[y][x] = 1;
		}
	}
}

void Saolei::Game()
{
	switch (flag)
	{
	case 0: RunGameSimpleness();
		break;
	case 1:	RunGameOrdinary();
		break;
	case 2:	RunGameDifficulty();
		break;
	}
}