Let's make 16 games in C++(六):Minesweeper

1、准备游戏需要的背景

2、将游戏内容渲染出来

#include<SFML/Graphics.hpp>
#include<time.h>

using namespace sf;

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

	RenderWindow app(VideoMode(400, 400), "Minesweeper!");

	int w = 32;
	int grid[12][12];
	int sgrid[12][12];

	Texture t;
	t.loadFromFile("./Resources/images/tiles.jpg");
	Sprite s(t);

	for (int i = 1; i <= 10; i++)
	{
		for (int j = 1; j <= 10; j++)
		{
			sgrid[i][j] = 10;
			if (rand() % 5 == 0)
			{
				grid[i][j] = 9;
			}
			else
			{
				grid[i][j] = 0;
			}
		}
	}

	for (int i = 1; i <= 10; i++)
	{
		for (int j = 1; j <= 10; j++)
		{
			int n = 0;
			if (grid[i][j] == 9)
			{
				continue;
			}
			if (grid[i+1][j] == 9)
			{
				n++;
			}
			if (grid[i][j+1] == 9)
			{
				n++;
			}
			if (grid[i-1][j] == 9)
			{
				n++;
			}
			if (grid[i][j-1] == 9)
			{
				n++;
			}
			if (grid[i+1][j+1] == 9)
			{
				n++;
			}
			if (grid[i-1][j-1] == 9)
			{
				n++;
			}
			if (grid[i-1][j+1] == 9)
			{
				n++;
			}
			if (grid[i+1][j-1] == 9)
			{
				n++;
			}
			grid[i][j] = n;
		}
	}



	while (app.isOpen())
	{
		Event e;
		while (app.pollEvent(e))
		{
			if (e.type == Event::Closed)
			{
				app.close();
			}
		}

		app.clear(Color::White);
		for (int i = 1; i <= 10; i++)
		{
			for (int j = 1; j <= 10; j++)
			{
				sgrid[i][j] = grid[i][j];
				s.setTextureRect(IntRect(sgrid[i][j] * w, 0, w, w));
				s.setPosition(i * w, j * w);
				app.draw(s);
			}
		}
		app.display();

	}

	return 0;

}


3、添加鼠标按键

#include<SFML/Graphics.hpp>
#include<time.h>

using namespace sf;

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

	RenderWindow app(VideoMode(400, 400), "Minesweeper!");

	int w = 32;
	int grid[12][12];
	int sgrid[12][12];

	Texture t;
	t.loadFromFile("./Resources/images/tiles.jpg");
	Sprite s(t);

	for (int i = 1; i <= 10; i++)
	{
		for (int j = 1; j <= 10; j++)
		{
			sgrid[i][j] = 10;
			if (rand() % 5 == 0)
			{
				grid[i][j] = 9;
			}
			else
			{
				grid[i][j] = 0;
			}
		}
	}

	for (int i = 1; i <= 10; i++)
	{
		for (int j = 1; j <= 10; j++)
		{
			int n = 0;
			if (grid[i][j] == 9)
			{
				continue;
			}
			if (grid[i+1][j] == 9)
			{
				n++;
			}
			if (grid[i][j+1] == 9)
			{
				n++;
			}
			if (grid[i-1][j] == 9)
			{
				n++;
			}
			if (grid[i][j-1] == 9)
			{
				n++;
			}
			if (grid[i+1][j+1] == 9)
			{
				n++;
			}
			if (grid[i-1][j-1] == 9)
			{
				n++;
			}
			if (grid[i-1][j+1] == 9)
			{
				n++;
			}
			if (grid[i+1][j-1] == 9)
			{
				n++;
			}
			grid[i][j] = n;
		}
	}



	while (app.isOpen())
	{

		Vector2i pos = Mouse::getPosition(app);
		int x = pos.x / w;
		int y = pos.y / w;

		Event e;
		while (app.pollEvent(e))
		{
			if (e.type == Event::Closed)
			{
				app.close();
			}

			if (e.type == Event::MouseButtonPressed)
			{
				if (e.key.code == Mouse::Left)
				{
					sgrid[x][y] = grid[x][y];
				}
				else if(e.key.code == Mouse::Right)
				{
					sgrid[x][y] == 11;
				}
			}

		}

		app.clear(Color::White);
		for (int i = 1; i <= 10; i++)
		{
			for (int j = 1; j <= 10; j++)
			{
				if (sgrid[x][y] == 9)
				{
					sgrid[i][j] = grid[i][j];
				}
				s.setTextureRect(IntRect(sgrid[i][j] * w, 0, w, w));
				s.setPosition(i * w, j * w);
				app.draw(s);
			}
		}
		app.display();

	}

	return 0;

}


结果图:

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值