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.
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.
Pipe starts at X = 400
Just off the right edge of the screen
Each frame: pipe_x -= 4
Slides left by 4 pixels
If pipe_x < -50: reset to 400
Gone off-screen? Send it back to the start
pipe_x = 400pipe_gap_y = 250 # Inside game loop:pipe_x -= 4 if pipe_x < -50: pipe_x = 400 # Draw top pipe (from top of screen to gap)pygame.draw.rect(screen, (0, 200, 0), (pipe_x, 0, 50, pipe_gap_y - 75))# Draw bottom pipe (from below gap to bottom)pygame.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.