Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

python makes mini-games (III)


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

Lead

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


Related documents

Baidu web download link: https://pan.baidu.com/s/1q9VPkOYvI12LjqfQVRCF1Q

Password: 7wfg


Develop tools

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 principle

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:

 python makes mini-games (III)1

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:

 python makes mini-games (III)2

Here's how it works:

 python makes mini-games (III)3

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:

 python makes mini-games (III)4

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:

 python makes mini-games (III)5

Initialization of the game's main interface:

 python makes mini-games (III)6

Finally, the main interface display refresh and event response functions:

 python makes mini-games (III)7

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:

 python makes mini-games (III)8

OK, it's done!!!


The effect of the game

Run the Game3.py file in the cmd window.

The effect is as follows:


more

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.