hello , Long time no see . Vanishing population online updates a wave that will come out every month python Make small game series articles . In this issue, we will make a Xiaole game , Let's start happily ~
Python edition ： 3.6.4
Related modules ：
pygame modular ;
As well as some Python Built in modules .
install Python And add to environment variable ,pip Install the relevant modules required .
Introduction of the principle
The rules of the game ：
Players exchange adjacent puzzles with the mouse , If the level after exchange / There are three consecutive identical puzzles in the vertical direction , Then these puzzles disappear , Players score , At the same time, a new puzzle is generated to supplement the missing part , otherwise , The exchange failed , Players don't score .
Players need to score as high as possible within the specified time .
Step by step ：
Game initialization , And load the necessary game materials ：
Step2****： Define the game sprite class
Collision detection between mouse and puzzle block is needed in the game , So as to operate the puzzle block to move , Therefore, we define a puzzle game wizard class to facilitate the implementation of these functions ：
Step3****： Realize the main cycle of the game
Logic is very simple , Is to constantly detect whether a mouse click event occurs , If there is , Then judge whether the position when the mouse clicks is within the position area of a puzzle block , If in , Then select the puzzle block , Otherwise, don't select .
When a second puzzle piece is selected , Then judge whether the two puzzle blocks meet the conditions of puzzle exchange , If meet , Then exchange puzzle pieces , And get a reward , Otherwise, do not swap and deselect the selected state of the two puzzle blocks .
The source code is shown in the figure below ：
Step4****： Game ending screen
After the game countdown , Enter the game end interface , The interface displays the user's current score . meanwhile , If the user types R Key to restart the game , type ESC Key to exit the game .
The source code implementation is as follows ：
This is the end of the article , Thanks for watching ,Python24 A series of small games , Share the next article 24 A little game