Flappy Bird
The flap-and-dodge game, written in Python with Pygame: gravity, pipes, collisions, sprites and sound.
- Platform
- Python game
- When
- 2021
- Role
- Sole developer
- Status
- Earlier work, on GitHub
if event.type == KEYDOWN and event.key in (K_SPACE, K_UP):
if playery > 0:
playerVelY = playerFlapAccv
playerFlapped = True
GAME_SOUNDS['wing'].play()
...
if playerVelY <playerMaxVelY and not playerFlapped:
playerVelY += playerAccY
if playerFlapped:
playerFlapped = False
playerHeight = GAME_SPRITES['player'].get_height()
playery = playery + min(playerVelY, GROUNDY - playery - playerHeight)The problem
Learn real-time input, motion, collision detection and drawing to the screen, in one small game.
What I built
Space or Up gives the bird an upward kick, and every frame after that gravity adds to its speed, up to a limit. Pairs of pipes slide left four pixels a frame, a new pair comes in as the first nears the edge, and pairs that leave are dropped. You score as you pass a pipe’s middle, and touching a pipe, the ground or the top of the screen ends the run, with wing, point and hit sounds as it happens. It all runs in a fixed window, 289 by 511 pixels.
Hard partPhysics that feel right at 32 frames a second, from a few lines of speed and gravity.
Under the hood
- 01InputSpace or Up
- 02PhysicsGravity, or a flap
- 03CollisionPipes, ground and sky
- 04Drawing32 frames a second
Decisions
- 01
A flap sets the speed, rather than adding to it
However fast the bird is falling, a flap always gives the same lift, so it’s predictable to play.
Trade-offThere’s no holding on to build up height.
Built with
PythonPygame