Preface:

In this episode, we’re going to make a little game that imitates “Flappybird”.

Let’s get started happily

rendering

Environment set up

Install Python and add it to the environment variable, and the PIP will install the appropriate module.

The principle is introduced

Flappybird Game Introduction:

The player controls a bird through the space bar to make it jump over an obstacle made up of various lengths of water pipes. When the bird hits the obstacle or falls to the bottom of the screen, the game ends.

Step by step:

Step1: Define the Sprite class

To facilitate the detection of collisions between birds and pipes, we first define some Sprite classes, including:

(1) the little bird

The bird should have the ability to update its position over time, jumping up when the player presses the space bar, or falling down. In addition, to make the game more realistic, the bird should adjust its body Angle before jumping up or falling down. The specific code is as follows:

(2) the piping class

Pipes are divided into pipe body and pipe head. Pipe head:

The pipe body:

It can be simply defined as follows:

Apparently, a pipe head and a number of pipe bodies form a pipe obstacle, which is placed in two rows with a certain space between the two for the birds to cross, as follows:

Therefore, we define a large pipe class to build a complete pipe obstacle, which is implemented as follows:

Among them, the function of updating the pipeline is to realize the effect of the bird moving to the right continuously through the left movement of the pipeline.

Step2: Implement the main game loop

First initialize, load images, music, fonts, and other files, and define some necessary constants:

Now it’s time to define the game’s main loop! The logic of the main loop of the game is very simple, first display the game background, and then update the position of the bird according to the player’s operation, automatically update the position of the pipeline, and judge whether the game is over through the collision detection and the vertical coordinate of the bird, if the game is over, the end of the game screen will be displayed. Of course, you also need to update the player’s current score in real time based on the number of channels the player has gone through. This step should be written at the end, otherwise the score will be covered by the channel, which is obviously not reasonable. Here’s how to do it:

This is the end of the article, thank you for watching, Python24 small games series, the next article imitation Google small dinosaur

To thank you readers, I’d like to share with you some of my recent collection of dry programming goodies and give something back to every reader in the hope of helping you out.

Dry goods mainly include:

① More than 2000 Python e-books (mainstream and classic books are available)

(2) The Python Standard Library (Chinese version)

③ project source code (40 or 50 interesting and classic hands-on project and source code)

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

⑤Python Learning Roadmap (Goodbye to Slow Learning)

⑥ Two days of Python crawler training camp live access

All done~ share not easy to remember to give wave thumb up + comments under support ~ see profile or private message for complete source code.