Make a python code for Flappy Bird
- User
Make a python code for Flappy Bird
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Make a python code for Flappy Bird
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is a compact, fully-working “Flappy Bird” clone written with nothing but Python + Pygame (no external sprites required—everything’s drawn with plain rectangles & circles). Copy-paste into flappy.py, run it, and flap away! 🐤
"""
Minimal Flappy Bird clone
---------------------------------
controls : SPACE / ↑ -> flap / restart
quit : ESC / window close
requirements : pygame 2.x
pip install pygame
"""
import pygame, sys, random
pygame.init()
# --- CONFIG --------------------------------------------------------------
WIDTH, HEIGHT = 400, 600
FPS = 60
GRAVITY = 0.35
JUMP_STRENGTH = -7.5
PIPE_SPEED = -3
PIPE_GAP = 170
PIPE_DISTANCE = 260 # horizontal space between pipes
BIRD_X = 80
BG_COLOR = (135, 206, 235) # sky-blue
PIPE_COLOR = (34, 139, 34)
BIRD_COLOR = (255, 255, 0)
FONT_COLOR = (255, 255, 255)
FONT = pygame.font.SysFont("consolas", 32, bold=True)
# --- INIT ----------------------------------------------------------------
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
pygame.display.set_caption("Flappy Bird (pure pygame)")
# --- GAME OBJECTS --------------------------------------------------------
class Bird:
RADIUS = 16
def __init__(self):
self.reset()
def reset(self):
self.x = BIRD_X
self.y = HEIGHT // 2
self.vel = 0
self.alive = True
def update(self):
self.vel += GRAVITY
self.y += self.vel
if self.y + self.RADIUS >= HEIGHT: # ground hit
self.alive = False
def flap(self):
self.vel = JUMP_STRENGTH
@property
def rect(self):
return pygame.Rect(self.x - self.RADIUS,
self.y - self.RADIUS,
self.RADIUS * 2,
self.RADIUS * 2)
def draw(self, surf):
pygame.draw.circle(surf, BIRD_COLOR, (int(self.x), int(self.y)), self.RADIUS)
class PipePair:
WIDTH = 60
def __init__(self, x):
self.x = x
self.gap_y = random.randrange(120, HEIGHT - 120)
self.scored = False # for score counting
def update(self):
self.x += PIPE_SPEED
def offscreen(self):
return self.x + self.WIDTH < 0
def collision(self, bird_rect):
top_rect = pygame.Rect(self.x, 0,
self.WIDTH, self.gap_y - PIPE_GAP // 2)
bottom_rect = pygame.Rect(self.x, self.gap_y + PIPE_GAP // 2,
self.WIDTH, HEIGHT)
return bird_rect.colliderect(top_rect) or bird_rect.colliderect(bottom_rect)
def draw(self, surf):
# top pipe
pygame.draw.rect(surf, PIPE_COLOR,
(self.x, 0, self.WIDTH, self.gap_y - PIPE_GAP // 2))
# bottom pipe
pygame.draw.rect(surf, PIPE_COLOR,
(self.x, self.gap_y + PIPE_GAP // 2, self.WIDTH, HEIGHT))
# --- HELPER --------------------------------------------------------------
def draw_text(s, size=32, center=(0,0)):
surf = FONT.render(s, True, FONT_COLOR)
rect = surf.get_rect(center=center)
screen.blit(surf, rect)
# --- GAME LOOP -----------------------------------------------------------
def main():
bird = Bird()
pipes = []
frame = 0
score = 0
running = True
game_active = True
while running:
dt = clock.tick(FPS) / 1000 # not used, but could help for future tweaks
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
elif e.type == pygame.KEYDOWN:
if e.key in (pygame.K_ESCAPE,):
running = False
elif e.key in (pygame.K_SPACE, pygame.K_UP):
if game_active:
bird.flap()
else:
# restart
bird.reset()
pipes.clear()
frame = 0
score = 0
game_active = True
if game_active:
# Update bird
bird.update()
if bird.y - bird.RADIUS <= 0: # hit ceiling
bird.alive = False
# Spawn pipes
if frame % (PIPE_DISTANCE // abs(PIPE_SPEED)) == 0:
pipes.append(PipePair(WIDTH + 10))
frame += 1
# Update pipes
for p in pipes:
p.update()
# scoring
if not p.scored and p.x + p.WIDTH < bird.x:
score += 1
p.scored = True
# Remove off-screen pipes
pipes = [p for p in pipes if not p.offscreen()]
# Collision
if any(p.collision(bird.rect) for p in pipes):
bird.alive = False
if not bird.alive:
game_active = False
# --- DRAW --------------------------------------------------------
screen.fill(BG_COLOR)
bird.draw(screen)
for p in pipes:
p.draw(screen)
draw_text(str(score), size=48, center=(WIDTH//2, 60))
if not game_active:
draw_text("GAME OVER", center=(WIDTH//2, HEIGHT//2 - 40))
draw_text("Press SPACE to restart", size=24,
center=(WIDTH//2, HEIGHT//2 + 10))
pygame.display.flip()
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
How it works (quick tour)
• Bird physics: each frame vel += gravity, y += vel.
• Flap resets vel to JUMP_STRENGTH (negative → upward).
• Pipes are spawned at fixed horizontal intervals, move left, and delete themselves once off-screen.
• Collision is simple rectangle intersection between the bird and both pipe rectangles (top & bottom).
• Score increments the first time the bird passes the center of a pipe pair.
• When the bird hits ground, ceiling, or a pipe, the game pauses; SPACE/↑ restarts.
Want extras? (sound, sprite images, parallax backgrounds, menu, leaderboard) — just let me know and we’ll jazz it up!