Review past

Python implements the “bunnies and Bun” game

Python implements eight-note mini-games

Python implements jigsaw puzzles

Skiing mini-games in Python

Python implements the classic 90 Tank Battle

Preface:

In this issue we will make a mini game that mimics “FlappyBird”.

Let’s have a good time

rendering

Environment set up

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

The principle is introduced

###FlappyBird

Players control a bird through the space bar, so that it across the obstacles made up of various lengths of water pipes, when the bird hits the obstacles or falls to the bottom of the screen, the game ends.

Step by step:

Step1: define Sprite classes

In order to facilitate the detection of collisions between birds and water pipes, we first define some sprites, including:

(1) the little bird

Over time, the bird should have the ability to update its position so that when the player presses the space bar, the bird jumps up, or falls down. In addition, to make the scene more realistic, birds should adjust their body angles before jumping up or falling down. The specific code is as follows:

(2) the piping class

Pipe is divided into pipe body and pipe head. Pipe head:

The pipe body:

It can be briefly defined as follows:

Obviously, a pipe head and several pipe bodies form a pipe obstacle. The pipe obstacles are in two rows with a certain space between them for birds to pass through, like this:

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

Among them, the function of updating the pipe is to achieve the effect of bird moving right continuously by moving the pipe left.

** Step2: implement the main loop **

Initialize, load images, music, fonts, etc., and define some necessary constants:

Now you can define the main loop of your game! The logic of the main loop of the game is very simple, showing the background of the game first, and then updating the position of the bird according to the player’s operation, automatically updating the position of the pipe, and judging whether the game is over through collision detection and the vertical coordinate of the bird, if the game is over, the game is over screen. Of course, you also need to update the player’s current score in real time based on the number of pipes the player has passed through. This step should be written at the end, otherwise the score will be covered by pipes, which is obviously not reasonable.

That’s the end of this article. Thank you for watching. Python is a series of 24 mini-games

To thank you readers, I’d like to share some of my recent programming favorites to give back to each and every one of you in the hope that they can help you.

Dry goods mainly include:

① Over 2000 Python ebooks (both mainstream and classic books should be available)

②Python Standard Library (Most Complete Chinese version)

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

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

⑤ A Roadmap for Learning Python

⑥ Two days of Python crawler boot camp live access

All done~ share not easy to remember to wave like + comment support ~ see personal profile or private letter to obtain the complete source code.