Flap to Fly
The bird falls… and falls… and there is nothing we can do about it. Until now. Let’s give the player control. Press SPACE and the bird flaps upward.
Listening for Input
Pygame keeps a list of everything the player does: key presses, mouse clicks, closing the window. These are called events.
The event loop
Each frame, we loop through all events with
pygame.event.get().
We check: is this event a key press? If yes, which key? That is how we know the player wants to flap.
The Flap Trick
Remember: positive velocity = falling down. So to go UP, we set velocity to a negative number.
velocity = +5
Bird moves down (Y increases)
velocity = -8
Bird shoots up (Y decreases)
Why -8?
The exact number controls how strong the flap feels. Too small (-2) and the bird barely moves. Too big (-20) and it rockets off screen. -8 feels just right, a satisfying hop.
What is the best way to make the bird go up?
Reading the Code
Here is how we detect a spacebar press and make the bird jump.
for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: bird_velocity = -8Your Turn: Add the Flap
Fill in the blanks to make the bird jump when SPACE is pressed.
Level Goal
Detect the SPACE key press and set velocity to -8 to make the bird jump.