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

Python Makes Small Games (Sat)


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're going to make a mini-game like "FlappyBird."

Let's get off to a pleasant start


Related documents

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

Password: zhm6

Images and audio footage originated on the internet and were deleted.


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.

Take a quick look


Introduction to the principle

Introduction to The FlappyBird game:

The player controls a bird with a space bar to cross obstacles made up of various lengths of water pipes, and the game ends when the bird hits the barrier or falls to the bottom of the screen.

Step-by-step:

Step1: Define the elf class

To facilitate the detection of collisions between birds and water pipes, let's first define some genie classes, including:

(1) Small birds

 Python Makes Small Games (Sat)1

As game time progresses, the bird should have the ability to update its position, i.e. when the player presses the space bar, the bird jumps up or the bird falls down. I n addition, in order to make the game scene more realistic, the bird should adjust the body angle before jumping up or down. The code implementation is as follows:

 Python Makes Small Games (Sat)2

(2) Pipeline class

Pipes are divided into pipe bodies and pipe heads. Pipe head:

 Python Makes Small Games (Sat)3

Pipe body:

 Python Makes Small Games (Sat)4

It can be simply defined as follows:

 Python Makes Small Games (Sat)5

Obviously, a pipe head and several pipe bodies form a pipe obstacle, pipe obstacles in two columns, there is a certain space between the two for birds to pass through, like this:

 Python Makes Small Games (Sat)6

So we define a large pipeline class to build a complete pipeline barrier, and the code implements the following:

 Python Makes Small Games (Sat)7

Among them, the role of updating the pipe is to achieve the effect of the bird constantly moving right through the left movement of the pipe.

Step2: Implement the main loop of the game

Initialize, load pictures, music, fonts, and more, and define some of the necessary constants:

 Python Makes Small Games (Sat)8

Now you're ready to define the game's main loop! T he logic of the main loop of the game is very simple, first show the game background, and then update the bird position according to the player's operation, automatically update the pipe position, and through collision detection and the bird's ordinate to determine whether the game is over, if the game is over, then show the end of the game screen. Of course, you also need to update the player's current score in real time based on the number of pipes the player passes through, this step needs to be written at the end, otherwise the score will be obscured by the pipeline, which is obviously unreasonable, the specific implementation is as follows:

 Python Makes Small Games (Sat)9

All Doneļ¼


more

Code as of 2018-08-05 test correct.

Packaged versions are available in the relevant documents of this series of articles, so you can run and play without the need for environment building