0

Dodging Pipes

A game with no obstacles is just a screensaver. Time to add the famous green pipes. Here is the trick: the bird does not move forward. The pipes move toward the bird. It is like running on a treadmill.

The Treadmill Trick

In Flappy Bird, the bird stays at X=50 the whole time. The world moves around it.

The bird stays put. The pipes slide left. It looks like the bird is flying forward.
🎬

A movie trick

This is the same trick used in old movies: the actor stands still while the background scrolls behind them. We start each pipe at X=400 (the right edge) and subtract from X each frame to move it left.

In Flappy Bird, what actually moves?

Recycling Pipes

We do not create infinite pipes. When a pipe goes off the left edge, we teleport it back to the right.

1

Pipe starts at X = 400

Just off the right edge of the screen

2

Each frame: pipe_x -= 4

Slides left by 4 pixels

3

If pipe_x < -50: reset to 400

Gone off-screen? Send it back to the start

pipe movement
1pipe_x = 400
🏁 Start just off the right edge
2pipe_gap_y = 250
🕳️ Where the gap between pipes is (vertically)
3
4# Inside game loop:
5pipe_x -= 4
⬅️ Move left 4 pixels per frame
6
7if pipe_x < -50:
♻️ Off-screen? Recycle it back to the right
8 pipe_x = 400
9
10# Draw top pipe (from top of screen to gap)
🟩 Top pipe: from Y=0 down to the gap
11pygame.draw.rect(screen, (0, 200, 0), (pipe_x, 0, 50, pipe_gap_y - 75))
12# Draw bottom pipe (from below gap to bottom)
🟩 Bottom pipe: from below the gap to the bottom
13pygame.draw.rect(screen, (0, 200, 0), (pipe_x, pipe_gap_y + 75, 50, 600))

Think About It

The pipe starts at X=400 and moves left by 4 each frame. How many frames until it reaches X=0?

Your Turn: Move the Pipes

Fill in the blanks to make the pipe slide left and reset when it goes off-screen.

🎯
🎯

Level Goal

Move the pipe left by 4 pixels each frame and reset it to X=400 when it goes off-screen.

Logic Lab: python.py
0/3
12345678910
pipe_x = 400 # Inside the game loop: pipe_x 4 if pipe_x < -50: pipe_x = # Draw the bottom pipe pygame.draw.rect(screen, (0, 200, 0), (pipe_x, 400, , 200))

Choose for blank #1