Jun 01, 2021 Article blog
This article was reproduced to Know ID: Charles (Bai Lu) knows his personal column
Download the W3Cschool Mobile App, 0 Foundation Anytime, Anywhere Learning Programming >> Poke this to learn
In this issue we will make a jigsaw puzzle game.
Well, maybe it's a little childish.
But anyway, let's get off to a happy start
Baidu web download link:
https://pan.baidu.com/s/1q9VPkOYvI12LjqfQVRCF1Q
Password: 7wfg
Python version: 3.6.4
Related modules:
pygame module;
and some Python's own modules
Environment construction
Install Python and add it to the environment variable, and pip installs the relevant modules that are required.
Introduction to the game:
After dividing the image into m× n rectangular blocks and replacing the rectangular blocks in the lower right corner of the image with blank blocks, the rectangular blocks are randomly placed in the shape of the original image. The goal of the game is to restore the randomly placed image to its original image by moving non-blank blocks, and to specify that the movement operation only exists when non-blank blocks are moved to blank blocks.
As shown in the following image:
Step-by-step:
Step1: The game's initial interface
Since it's a game, you have to have an initial interface, right?
OK, let's write a game initial interface:
Here's how it works:
Depending on the player's own level, you can choose puzzles of varying difficulty.
Step2: Define the move operation
The purpose of defining a move operation is to move the puzzle (as if it were nonsense T_T) and it's easy to implement:
Step3: The main interface of the game
OK, with the front paving, we can start implementing our main game interface.
First of all, we need to mess up the puzzle, but random messing up is likely to lead to the puzzle is inexplicable, so we use random movement of the puzzle to achieve the effect of messing up the puzzle, which is the main reason why we first define the movement of the puzzle:
Initialization of the game's main interface:
Finally, the main interface display refresh and event response functions:
Step4: Game end interface
When the player completes the puzzle, the end-of-game interface needs to be displayed, similar to the initial interface of the game, which is simple to implement:
OK, it's done!!!
Run the Game3.py file in the cmd window.
The effect is as follows:
Code as of 2018-06-26 test is correct.
Similarly, interested partners can be optimized on this basis, such as adding timing, integration, and so on.