Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Python to make a Windows minesweeping game

Shulou Source: shulou.com Published: 2022-06-02 21:33:53 10月03日 Update

How to use Python to do a Windows minesweeping game, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Minesweeping on Windows XP is the collective memory of countless post-80s / 90s. Today we will use Python to make a high-imitation version of the minesweeping game.

Forgive me for my broken hand, but mine sweeping basically never won. During the test, I secretly changed the number of mines from 99 to 50 to win.

Let's talk about my implementation logic.

First of all, how to represent mine and non-mine, the first idea is to establish a two-dimensional array to represent the whole region, 0 for non-mine and 1 for mine. Later, I thought it was wrong, and it was marked as a landmine, marked as a question mark, and a number indicating the number of mines around it. Many states, so let's just make a category:

Mine laying is very simple. Take 99 numbers at random and arrange them from top to bottom:

When we click on a grid, as long as according to the coordinates of the click, find the corresponding Mine, see what its value is, we will know whether it has stepped on the mine or not.

If you don't hit the mine, count how many mines are in the 8 surrounding positions in order to display the corresponding number.

If there is lightning around, then show the number, this is simple, but if there is no lightning around, it will show an area until there is thunder, as shown in the following picture, I only click in the middle, there is such a large area

In fact, this calculation is also easy, as long as the use of recursion, if the calculation of the surrounding mine number is 0, then recursively calculate the surrounding 8 locations around the mine number until the mine number is not zero.

Next, there is another trouble. We often press the left and right mouse buttons at the same time. If Lei is marked all at once, he will open all the boxes around him at once. If there is any wrong mark, then sorry, GAME OVER.

If it is not fully marked, there will be an effect showing a circle of unopened and marked squares around

That's all the main logic of minesweeping, and all that's left are miscellaneous incidents. Of course, these miscellaneous events are still enough for you to struggle with.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Marking minesweeping perimeter area number grid miscellaneous individual position event landmine that is time logic recursion help sorry wrong clear next bad. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux Apple Microsoft NVidia