# [Python] Make a Flappy bird game with Pygame

[Python] Make a Flappy bird game with Pygame (3)

Friends who need game materials and programs can reply to "FPB" in the background on my public number [thumb notes]

# 0. Final results This section describes how to add randomly generated pipes to a game.Let's sort it out next.

Friends who have played Flappy bird know that this game randomly generates upstream and downstream pipes of varying length. There is a certain distance between the upstream and downstream pipes, and new pipes are generated at regular intervals.The length of the pipe material is of course the same, so we randomly generate the coordinate y of the pipe to generate a random length of pipe.Let's figure out how to calculate the distance between two pipes.

## 1.1 Upper and lower tube coordinate relationship

The height of the whole window is 512 pixels.We set the distance between the two pipes to 50 pixels.The height of the pipe image is 320 pixels.

Since the y-value coordinate corresponds to the upper end of the image, the relationship between the upper and lower tubes is: the y-coordinate of the lower tube = the y-coordinate of the upper tube + 320 + 50

The pipe_y ranges from -320 to 0, and the more negative the pipe, the shorter the length of the upper tube.To ensure proper length, I set the pipe_y range to -270 to -20 ## 1.2 Random tube generation

Here we use randint(-270,-10) in the random module to generate a range of random integers and assign the whole integer to pipe_y.This allows random pipes of different heights but with the same spacing to be generated.

```import random
pipe_y = random.randint(-270,-20)
```

To make the effect more obvious, I added spaces to control random tube generation.

The results are as follows: ## 1.3 Move the pipe

In the game, the pipes and green bricks move at the same speed, so we use the same speed, and the moving distance per frame still depends on the time of each frame.The left boundary of the pipe corresponds to an x-value, and the pipe itself is 52 pixels wide.So the x value should range from -52 to 288.

```pipe_x = 288

#Enter a cycle:
time_passed = clock.tick()
time_passed_seconds = time_passed / 1000
#Calculate the time of each frame
distance_moved = time_passed_seconds * speed
pipe_x -=distance_moved
if(pipe_x<0):
pipe_x+=288
pipe_y = random.randint(-270,-20)
```

The results are as follows. To make the effect more obvious, I accelerated the movement of the tube. Now let's integrate these codes into the previous programs according to what we just thought.The result is a scene where birds, pipes and green bricks move together.

First, initialization. Variables to initialize are pipe_y and pipe_x

```pipe_y = random.randint(-270,-20)
pipe_x = 288
```

Then the pipe_y (upper pipe coordinate) is used to calculate the value divided by the pipe_dy (lower pipe coordinate).

```def calculation(pipe_y):
pipe_dy = pipe_y+370
return pipe_dy
```

Then modify the start_updatexy() function of the game interface to add the updated portion of the pipeline.

```def start_updatexy(time_passed_seconds,base_x,base_y,dirx):
distance_moved = time_passed_seconds * speed
bird_distance = time_passed_seconds * bird_speed
base_y = base_y + dirx*bird_distance
base_x -= distance_moved
pipe_x -=distance_moved

if(pipe_x<0):
pipe_x+=288
pipe_y = random.randint(-270,-20)

if base_x<-40:
base_x += 40
if 490>base_y >20:
dirx = 2
elif base_y >490:
dirx = 0
elif base_y <0:
base_y = 0
return base_x,base_y,dirx
```

Finally, modify the display to draw the pipe to the window.

```def start():
screen.blit(background,(0,0))

screen.blit(top,(pipe_x,pipe_y))
pipe_dy = calculation(pipe_y)
screen.blit(bottom,(pipe_x,pipe_dy))

screen.blit(green_base,(base_x,400))
```

The most important thing to add pipelines is to grasp the distance between two groups of pipelines.First, let's assume that the total length of the pipe movement is 600, the window (288 pixels) is in the middle, and the first pipe start coordinate is 444, and the end coordinate is -156; but the second pipe can't appear at the beginning, so the first pipe cycle start position is 744, but the end position is -156.

Program implementation:

```#Initialize start position first
pipe_x = 444
pipe_x1 = 744

#Change the start() function

def start():
screen.blit(background,(0,0))

screen.blit(top,(pipe_x,pipe_y))
pipe_dy = calculation(pipe_y)
screen.blit(bottom,(pipe_x,pipe_dy))

screen.blit(top,(pipe_x1,pipe_y1))
pipe_dy1 = calculation(pipe_y1)
screen.blit(bottom,(pipe_x1,pipe_dy1))

screen.blit(green_base,(base_x,400))

#Change the start_updatexy() function
pipe_x -=distance_moved
pipe_x1 -=distance_moved

if(pipe_x1<-156):
pipe_x1+=600
pipe_y1 = random.randint(-270,-20)
if(pipe_x<-156):
pipe_x+=600
pipe_y = random.randint(-270,-20)

```

The end result:   18 original articles published, 18 praised, 2571 visits

Tags: Python

Posted on Sun, 15 Mar 2020 21:11:42 -0400 by shahab03