Yuvaraj Vodiboina
All work
(15)Case studyPython game

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
flappy.py
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)
(01)

The problem

Learn real-time input, motion, collision detection and drawing to the screen, in one small game.

(02)

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.

(03)

Under the hood

  1. 01InputSpace or Up
  2. 02PhysicsGravity, or a flap
  3. 03CollisionPipes, ground and sky
  4. 04Drawing32 frames a second
(04)

Decisions

  1. 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.

(05)

Built with

PythonPygame

Next16 of 16Android, in Java IdentiFlash My first Android app: a flashlight, in Java. One toggle turns the torch on and off, and it turns itself off when you leave.Read the case study