How to build Flappy Bird in Python

Welcome back! Flappy Bird was one of the most popular mobile games ever created, so let’s go ahead and build this out. First off, we’re going to be using this specific Github project, so make sure to give them all of the credit, I am simply explaining the code out in detail, here is full project:

Within this project you will see a couple of different files, a background image, base image, the images for the bird, the actual Python file (flappy.py) a pipe image and a readme file:

Awesome, we will need this folder to essentially run this whole project, make sure to keep all of these assets in the same folder. Now, we will need to install the pygame module, in order to do this run the following command:

pip install pygame
#For Python3 Use The Following
pip3 install pygame

Awesome! You could technically run the Python file right now if you wanted to by going to your terminal / command prompt and using this command:

python /path/to/the/folder/on/your/computer/flappy.py
#OR
python3 /path/to/the/folder/on/your/computer/flappy.py

Make sure to change the above file path to the specific one for your folder, you will essentially see the game actually output:

But before we run it, let’s walk through the code since we may need to change some things. Starting off, we’re going to import our project and set some universal characteristics of our game, these include the screen height, width, speed and many other things as well:

import pygame, random
from pygame.locals import *

SCREEN_WIDTH = 400
SCREEN_HEIGHT = 800
SPEED = 10
GRAVITY = 1
GAME_SPEED = 5

GROUND_WIDTH = 2 * SCREEN_WIDTH
GROUND_HEIGHT = 100

PIPE_WIDTH = 80
PIPE_HEIGHT = 500

PIPE_GAP = 200

You can change these parameters as needed but don’t delete any, they are going to be called in various points in this script. You can also change alot of the characteristics of this game, let’s say you wanted to make the game faster, you could increase the GAME_SPEED value to 10. Next up we need to create our sprite & animate it, this is where we define our class and change the path of the images to your specific computer:

class Bird(pygame.sprite.Sprite):

def __init__(self):
pygame.sprite.Sprite.__init__(self)

self.images = [pygame.image.load('/Users/users/Downloads/python-flappybird-master/bluebird-upflap.png').convert_alpha(),
pygame.image.load('/Users/users/Downloads/python-flappybird-master/bluebird-midflap.png').convert_alpha(),
pygame.image.load('/Users/users/Downloads/python-flappybird-master/bluebird-downflap.png').convert_alpha()]

self.speed = SPEED

self.current_image = 0

self.image = pygame.image.load('/Users/users/Downloads/python-flappybird-master/bluebird-upflap.png').convert_alpha()
self.mask = pygame.mask.from_surface(self.image)

self.rect = self.image.get_rect()
self.rect[0] = SCREEN_WIDTH / 2
self.rect[1] = SCREEN_HEIGHT / 2

def update(self):
self.current_image = (self.current_image + 1) % 3
self.image = self.images[ self.current_image ]

self.speed += GRAVITY

# Update height
self.rect[1] += self.speed

def bump(self):
self.speed = -SPEED

This code essentially cycles through the 3 images to “animate” the bird, it will also allow us to call the bird and move it up by clicking the space bar. We will now essentially do the same thing for the pipe and ground, remember to change the images to your specific file path:

class Pipe(pygame.sprite.Sprite):

def __init__(self, inverted, xpos, ysize):
pygame.sprite.Sprite.__init__(self)

self.image = pygame.image.load('/Users/users/Downloads/python-flappybird-master/pipe-red.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (PIPE_WIDTH,PIPE_HEIGHT))

self.rect = self.image.get_rect()
self.rect[0] = xpos

if inverted:
self.image = pygame.transform.flip(self.image, False, True)
self.rect[1] = - (self.rect[3] - ysize)
else:
self.rect[1] = SCREEN_HEIGHT - ysize

self.mask = pygame.mask.from_surface(self.image)

def update(self):
self.rect[0] -= GAME_SPEED

class Ground(pygame.sprite.Sprite):

def __init__(self, xpos):
pygame.sprite.Sprite.__init__(self)

self.image = pygame.image.load('/Users/users/Downloads/python-flappybird-master/base.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (GROUND_WIDTH, GROUND_HEIGHT))

self.mask = pygame.mask.from_surface(self.image)

self.rect = self.image.get_rect()
self.rect[0] = xpos
self.rect[1] = SCREEN_HEIGHT - GROUND_HEIGHT

def update(self):
self.rect[0] -= GAME_SPEED

def is_off_screen(sprite):
return sprite.rect[0] < -(sprite.rect[2])

def get_random_pipes(xpos):
size = random.randint(100, 300)
pipe = Pipe(False, xpos, size)
pipe_inverted = Pipe(True, xpos, SCREEN_HEIGHT - size - PIPE_GAP)
return (pipe, pipe_inverted)

Finally we will initialize our Pygame environment, add in the assets to our screen and end the game once the bird hits the pipe, this is essentially how you would code that out:

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

BACKGROUND = pygame.image.load('/Users/users/Downloads/python-flappybird-master/background-day.png')
BACKGROUND = pygame.transform.scale(BACKGROUND, (SCREEN_WIDTH, SCREEN_HEIGHT))

bird_group = pygame.sprite.Group()
bird = Bird()
bird_group.add(bird)

ground_group = pygame.sprite.Group()
for i in range(2):
ground = Ground(GROUND_WIDTH * i)
ground_group.add(ground)

pipe_group = pygame.sprite.Group()
for i in range(2):
pipes = get_random_pipes(SCREEN_WIDTH * i + 800)
pipe_group.add(pipes[0])
pipe_group.add(pipes[1])


clock = pygame.time.Clock()

while True:
clock.tick(30)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()

if event.type == KEYDOWN:
if event.key == K_SPACE:
bird.bump()

screen.blit(BACKGROUND, (0, 0))

if is_off_screen(ground_group.sprites()[0]):
ground_group.remove(ground_group.sprites()[0])

new_ground = Ground(GROUND_WIDTH - 20)
ground_group.add(new_ground)

if is_off_screen(pipe_group.sprites()[0]):
pipe_group.remove(pipe_group.sprites()[0])
pipe_group.remove(pipe_group.sprites()[0])

pipes = get_random_pipes(SCREEN_WIDTH * 2)

pipe_group.add(pipes[0])
pipe_group.add(pipes[1])

bird_group.update()
ground_group.update()
pipe_group.update()

bird_group.draw(screen)
pipe_group.draw(screen)
ground_group.draw(screen)

pygame.display.update()

if (pygame.sprite.groupcollide(bird_group, ground_group, False, False, pygame.sprite.collide_mask) or
pygame.sprite.groupcollide(bird_group, pipe_group, False, False, pygame.sprite.collide_mask)):
# Game over
input()
break

As you can see this is a very brief walk through of the code, but the code readability is fairly high on this project. In order to run this code you will have to run this in your terminal / command prompt:

python /path/to/the/folder/on/your/computer/flappy.py
#OR
python3 /path/to/the/folder/on/your/computer/flappy.py

Make sure to change the file path to your specific project, you will then see a window open up on your display which showcases the Flappy Bird game!

There you have it, again please give credit to the original creator of the code:

He did a great job coding out this project, all I did was simply change a few parameters of this project.

As Always

if you have any suggestions, thoughts or just want to connect, feel free to contact / follow me on Twitter! Also, below is a link to some of my favorite resources for learning programming, Python, R, Data Science, etc.

Thanks so much for reading!

Data Scientist / Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store