程序员

利用vs 2019和easyx图形库完成简易扫雷小游戏

作者:admin 2021-07-28 我要评论

利用vs 2019和easyx图形库完成扫雷小游戏 需要的工具 win 10 vs 2019 easyx C 源码 : # include iostream # include graphics.h # include time.h using namespa...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

利用vs 2019和easyx图形库完成扫雷小游戏

需要的工具:

  1. win 10
  2. vs 2019
  3. easyx
C++源码:
#include<iostream>
#include<graphics.h>
#include<time.h>
using namespace std;

int map[15][15];
int Temp_map[15][15];
int Boom_x[15];
int Boom_y[15];
int Boomnum = 15;
IMAGE image;
void Initmap()
{
	for (size_t i = 0; i < 15; i++)
	{
		for (size_t j = 0; j < 15; j++)
		{
			map[i][j] = 0;

			Temp_map[i][j] = 0;

		}
	}
}
void InitBoom()
{
	int x = 0;
	int y = 0;

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

	for (size_t i = 0; i < 15; i++)
	{
		x = rand() % 15;
		y = rand() % 15;
		if (10 == map[x][y])
		{
			x = rand() % 15;
			y = rand() % 15;
		}
		Boom_x[i] = x;
		Boom_y[i] = y;
		map[x][y] = 10;
	}

	for (size_t i = 0; i < 15; i++)
	{
		for (size_t j = 0; j < 15; j++)
		{
			if (10 == map[i][j])
			{
				continue;
			}
			if (i != 0 && j != 0 && 10 == map[i - 1][j - 1])
			{
				map[i][j] += 1;
			}
			if (i != 0 && 10 == map[i - 1][j])
			{
				map[i][j] += 1;
			}
			if (j != 0 && 10 == map[i][j - 1])
			{
				map[i][j] += 1;
			}
			if (i < 14 && 10 == map[i + 1][j])
			{
				map[i][j] += 1;
			}
			if (j < 14 && 10 == map[i][j + 1])
			{
				map[i][j] += 1;
			}
			if (i < 14 && j < 14 && 10 == map[i + 1][j + 1])
			{
				map[i][j] += 1;
			}
			if (i != 0 && j < 14 && 10 == map[i - 1][j + 1])
			{
				map[i][j] += 1;
			}
			if (i < 14 && j != 0 && 10 == map[i + 1][j - 1])
			{
				map[i][j] += 1;
			}
		}
	}
}
void Coutmap()
{
	for (size_t i = 0; i < 15; i++)
	{
		for (size_t j = 0; j < 15; j++)
		{
			cout << map[i][j] << " ";
		}
		cout << endl;
	}
}
void IniteasyxMap()
{
	loadimage(&image, "back.png", 600, 600);
	putimage(0, 0, &image);

	for (size_t i = 0; i < 15; i++)
	{
		for (size_t j = 0; j < 15; j++)
		{
			outtextxy(j * 40 + 11, i * 40 + 11, "*");
		}
	}
}
void MouseEvent()
{
	MOUSEMSG msg = GetMouseMsg();
	setfillcolor(BLACK);
	int L_y = msg.x / 40;
	int L_x = msg.y / 40;

	if (msg.uMsg == WM_LBUTTONDOWN)
	{
		if (map[L_x][L_y] == 9)
		{
			return;
		}

		if (map[L_x][L_y] == 10)
		{
			HWND hwnd = GetHWnd();

			MessageBox(hwnd, "You lose", "Over", 0);
		}
		else
		{
			if (0 == map[L_x][L_y])
			{
				fillrectangle(L_y * 40, L_x * 40, (L_y + 1) * 40, (L_x + 1) * 40);
				outtextxy(L_y * 40 + 11, L_x * 40 + 11, "0");
				map[L_x][L_y] = 11;
			}
			if (1 == map[L_x][L_y])
			{
				fillrectangle(L_y * 40, L_x * 40, (L_y + 1) * 40, (L_x + 1) * 40);
				outtextxy(L_y * 40 + 11, L_x * 40 + 11, "1");
				map[L_x][L_y] = 11;
			}
			if (2 == map[L_x][L_y])
			{
				fillrectangle(L_y * 40, L_x * 40, (L_y + 1) * 40, (L_x + 1) * 40);
				outtextxy(L_y * 40 + 11, L_x * 40 + 11, "2");
				map[L_x][L_y] = 11;
			}
			if (3 == map[L_x][L_y])
			{
				fillrectangle(L_y * 40, L_x * 40, (L_y + 1) * 40, (L_x + 1) * 40);
				outtextxy(L_y * 40 + 11, L_x * 40 + 11, "3");
				map[L_x][L_y] = 11;
			}
			if (4 == map[L_x][L_y])
			{
				fillrectangle(L_y * 40, L_x * 40, (L_y + 1) * 40, (L_x + 1) * 40);
				outtextxy(L_y * 40 + 11, L_x * 40 + 11, "4");
				map[L_x][L_y] = 11;
			}
			if (5 == map[L_x][L_y])
			{
				fillrectangle(L_y * 40, L_x * 40, (L_y + 1) * 40, (L_x + 1) * 40);
				outtextxy(L_y * 40 + 11, L_x * 40 + 11, "5");
				map[L_x][L_y] = 11;
			}
			if (6 == map[L_x][L_y])
			{
				fillrectangle(L_y * 40, L_x * 40, (L_y + 1) * 40, (L_x + 1) * 40);
				outtextxy(L_y * 40 + 11, L_x * 40 + 11, "6");
				map[L_x][L_y] = 11;
			}
			if (7 == map[L_x][L_y])
			{
				fillrectangle(L_y * 40, L_x * 40, (L_y + 1) * 40, (L_x + 1) * 40);
				outtextxy(L_y * 40 + 11, L_x * 40 + 11, "7");
				map[L_x][L_y] = 11;
			}
			if (8 == map[L_x][L_y])
			{
				fillrectangle(L_y * 40, L_x * 40, (L_y + 1) * 40, (L_x + 1) * 40);
				outtextxy(L_y * 40 + 11, L_x * 40 + 11, "8");
				map[L_x][L_y] = 11;
			}
		}
	}

	if (msg.uMsg == WM_RBUTTONDOWN)
	{
		if (map[L_x][L_y] == 11)
		{
			return;
		}
		if (map[L_x][L_y] == 10) 
		{
			Boomnum -= 1;
		}
		if (map[L_x][L_y] == 9)
		{
			if (Temp_map[L_x][L_y] == 10) 
			{
				Boomnum += 1;
			}
			setfillcolor(RGB(192, 192, 192));

			map[L_x][L_y] = Temp_map[L_x][L_y];

			fillrectangle(L_y * 40, L_x * 40, (L_y + 1) * 40, (L_x + 1) * 40);

			outtextxy(L_y * 40 + 11, L_x * 40 + 11, "*");

			return;
		}

		Temp_map[L_x][L_y] = map[L_x][L_y];

		map[L_x][L_y] = 9;

		fillrectangle(L_y * 40, L_x * 40, (L_y + 1) * 40, (L_x + 1) * 40);

		outtextxy(L_y * 40 + 11, L_x * 40 + 11, "!!!");
	}
}
bool JudgeOver()
{
	for (size_t i = 0; i < 15; i++)
	{
		if (map[Boom_x[i]][Boom_y[i]] == 9)
		{
			Boomnum -= 1;
		}
	}

	if (Boomnum == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
void StartGame()
{
	initgraph(600, 600, 1);
	Initmap();
	InitBoom();
	Coutmap();
	IniteasyxMap();

	while (1)
	{
		MouseEvent();

		if (Boomnum == 0) 
		{
			HWND  hwnd = GetHWnd();

			MessageBox(hwnd,"You win","Over",MB_OK);

			return;
		}
	}
}

int main()
{
	StartGame();

	system("pause");
	return 0;
}
;原文链接:https://blog.csdn.net/qq_47750142/article/details/115800744

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 利用vs 2019和easyx图形库完成简易扫雷

    利用vs 2019和easyx图形库完成简易扫雷

  • 最短路径(迪杰斯特拉算法)

    最短路径(迪杰斯特拉算法)

  • unity+Vuforia实现用虚拟按钮进行交互

    unity+Vuforia实现用虚拟按钮进行交互

  • 牛客昆明站K.Parallel Sort

    牛客昆明站K.Parallel Sort

腾讯云代理商
海外云服务器