How to Build a Flappy Bird Clone with Python and Pygame
This article walks you through creating a Flappy Bird‑style game using Python 3 and the pygame library, covering installation, running the program, visual demos, and a full source‑code listing that includes bird, pipe, stone, and bullet mechanics with level progression and sound effects.
1. Demo
Previously I played Flappy Bird on my phone, so I wrote a version using Python 3 and the pygame module. The game runs smoothly, includes background music, and surprises you with its fluid animation.
Download the game and install pygame with pip install pygame. Then run the .py file to see the first screen.
Click the "Play" button in the image below to start the game.
The game plays music during play; be sure to download the accompanying assets so the sound works.
2. Full source code
The following code implements the Flappy Bird clone, defining classes for Bird, Pipe, Stone, Bullet, and various helper functions for loading images, handling collisions, scoring, level changes, and a settings menu.
import math
import os
import time
from random import randint, uniform
import pygame
from pygame.locals import *
from collections import deque
FPS = 60
BK_WIDTH = 900 # background width
BK_HEIGHT = 650 # background height
PIPE_WIDTH = 80
PIPE_HEIGHT = 10
PIPE_HEAD_HEIGHT = 32
# ... (additional constants and global variables) ...
class Bullet(pygame.sprite.Sprite):
speed = BULLET_SPEED
width = BULLET_WIETH
height = BULLET_HEIGHT
def __init__(self, bird, images):
super(Bullet, self).__init__()
self.x, self.y = bird.x, bird.y
self.bullet = images
self.mask_bullet = pygame.mask.from_surface(self.bullet)
def update(self):
self.x = self.x + self.speed * frames_to_msec(1)
@property
def image(self):
return self.bullet
@property
def mask(self):
return self.mask_bullet
@property
def rect(self):
return Rect(self.x, self.y, Bullet.width, Bullet.height)
def visible(self):
return 0 < self.x < BK_WIDTH + Bullet.width
class Bird(pygame.sprite.Sprite):
width = BIRD_WIDTH
height = BIRD_HEIGHT
sink_gravity = 0.001
def __init__(self, x, y, level, images):
super(Bird, self).__init__()
self.x, self.y = x, y
self.wing_up, self.wing_mid, self.wing_down = images
self.mask_wing_up = pygame.mask.from_surface(self.wing_up)
self.mask_wing_mid = pygame.mask.from_surface(self.wing_mid)
self.mask_wing_down = pygame.mask.from_surface(self.wing_down)
self.inital_speed = 0
self.level = level
self.score = 0
def update(self, t):
y_ = self.inital_speed * t + 0.5 * self.sink_gravity * t * t
if self.inital_speed <= 0.3:
self.inital_speed += self.sink_gravity * t
self.y += y_
@property
def image(self):
if pygame.time.get_ticks() % 400 >= 120:
return self.wing_up
elif pygame.time.get_ticks() % 400 >= 280:
return self.wing_mid
else:
return self.wing_down
@property
def mask(self):
if pygame.time.get_ticks() % 400 >= 120:
return self.mask_wing_up
elif pygame.time.get_ticks() % 400 >= 280:
return self.mask_wing_mid
else:
return self.mask_wing_down
@property
def rect(self):
return Rect(self.x, self.y, Bird.width, Bird.height)
# Additional classes: Pipe, Stone, utility functions (load_image, load_images, frames_to_msec, msec_to_frames, game_loop, setting, buttons, main, etc.)
if __name__ == "__main__":
main()Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
