The composition of Xiaoxiaole mainly includes three parts: the main body of the game, the scorer, and the timer. Let's take a look at the specific implementation.
Define some constants, such as: window width and height, number of grid rows and columns, etc. The code is as follows:
Then create a main window, the code is as follows:
Take a look at the effect:
Then draw an 8 x 8 grid in the window, the code is as follows:
Take a look at the effect:
Then randomly put various puzzle pieces in the grid, the code is as follows:
Take a look at the effect:
Interested students can follow the editor and apply for QQ group: 721195303 to receive this software.
Then add the scorer and timer, the code is as follows:
Take a look at the effect:
When the set game time runs out, we can generate some prompt messages, the code is as follows:
Take a look at the effect:
After talking about the related parts of the game graphical interface, let's look at the main processing logic of the game.
We use the mouse to manipulate the puzzle pieces, so the program needs to check whether the puzzle pieces are selected. The code is implemented as follows:
We need to exchange the positions of the puzzle pieces selected continuously by the mouse. The code is implemented as follows:
Every time we exchange puzzle pieces, we need to judge whether there are three or more puzzle pieces in a row. The code is implemented as follows:
When three or more puzzle pieces appear, these puzzle pieces need to be eliminated. The code is implemented as follows:
After eliminating the matching puzzle pieces, we also need to randomly generate new puzzle pieces. The code implementation is as follows:
Then repeat this process until the game time is exhausted and the game is over.
Finally, let's take a look at the game effect dynamically.
In this article, we use Python to implement a simple puzzle game. Those who are interested can further expand the game, such as adding levels.