“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

Review past

Python to achieve classic pac-man games

Python to achieve “little rabbit and Bun” game

Python eight notes to achieve small games

Python to achieve the puzzle game

Python to achieve ski games

Python for classic 90 tank wars

Python FlappyBird implementation of small games

Python real dinosaur jump a jump small game is

Python to achieve tower defense games

Python to achieve fruit and gold coins small games

Python simple version of the aircraft war games

Python tetris small game implementation

Python to achieve a small game push box

Preface:

Hello, long time no see. Vanishing Population online updates a monthly series of articles about making small games in Python. In this installment, we will make a small game to eliminate the joy, let’s have a happy start ~

Results show

The development tools

Python version: 3.6.4

Related modules:

Pygame module;

And some modules that come with Python.

Environment set up

Install Python and add it to the environment variables. PIP installs the required modules.

Introduction of the principle

Rules of the game:

Players exchange adjacent puzzles with the mouse. If there are three consecutive same puzzles in the horizontal/vertical direction after the exchange, these puzzles disappear and players score. At the same time, new puzzles are generated to supplement the missing parts.

Players need to score as many points as possible within the time limit.

Step by step:

Step1** : Initialize **

The game is initialized and the necessary game materials are loaded:

Step2: define the game Sprite class **

Collision detection between the mouse and the puzzle pieces is needed in the game to operate the puzzle pieces, so we define a puzzle piece game Sprite class to facilitate the implementation of these functions:

Step3: implement the main loop of the game

In fact, the logic is very simple, which is to constantly detect whether there is a mouse click event. If there is, it will determine whether the position of the mouse click is in the position area of a certain puzzle piece. If so, it will select the puzzle piece, otherwise it will not be selected.

When a second puzzle piece is selected, it determines whether the two puzzle pieces meet the condition of puzzle exchange. If so, the puzzle pieces are exchanged and the reward is obtained. Otherwise, the two puzzle pieces are not exchanged and the selected status of the two puzzle pieces is cancelled.

The source code is shown in the figure below:

Step4** : game end interface **

After the game countdown ends, enter the end of the game interface, the interface displays the user’s current score. At the same time, if the user types R to restart the game, and ESC to exit the game.

The source code is implemented as follows:

That’s the end of this article, thank you for watching Python24 mini-games series, next article will share 24 mini-games

To thank the readers, I’d like to share some of my recent programming gems to give back to each and every one of you.

Dry goods are mainly:

① More than 2000 Python ebooks (both mainstream and classic)

Python Standard Library (Chinese version)

(3) project source code (forty or fifty interesting and classic practice projects and source code)

④Python basic introduction, crawler, Web development, big data analysis videos (suitable for small white learning)

⑤Python Learning Roadmap

⑥ Two days of Python crawler boot camp live permissions

Don’t indulge in games, to combine work and rest

Have fun studying