Here's a simplified Flappy Bird implementation using basic shapes (no external assets):
import pygame
import random
# Initialize Pygame
pygame.init()
# Game constants
WIDTH, HEIGHT = 400, 600
GRAVITY = 0.5
JUMP = -8
PIPE_SPEED = 3
PIPE_GAP = 150
PIPE_WIDTH = 50
# Colors
SKY_BLUE = (135, 206, 235)
BIRD_YELLOW = (255, 255, 0)
PIPE_GREEN = (0, 255, 0)
GROUND_BROWN = (139, 69, 19)
# Setup display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flappy Bird")
clock = pygame.time.Clock()
class Bird:
def __init__(self):
self.rect = pygame.Rect(50, HEIGHT//2, 30, 30)
self.velocity = 0
def update(self):
self.velocity += GRAVITY
self.rect.y += self.velocity
def jump(self):
self.velocity = JUMP
class Pipe:
def __init__(self, x):
self.x = x
self.height = random.randint(50, HEIGHT - PIPE_GAP - 50)
self.top_rect = pygame.Rect(x, 0, PIPE_WIDTH, self.height)
self.bottom_rect = pygame.Rect(x, self.height + PIPE_GAP, PIPE_WIDTH, HEIGHT - self.height - PIPE_GAP)
def update(self):
self.x -= PIPE_SPEED
def main():
bird = Bird()
pipes = [Pipe(WIDTH)]
score = 0
game_active = True
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and game_active:
bird.jump()
# Game logic
if game_active:
bird.update()
for pipe in pipes:
pipe.update()
if pipe.x < -PIPE_WIDTH:
pipes.remove(pipe)
if pipe.top_rect.colliderect(bird.rect) or pipe.bottom_rect.colliderect(bird.rect):
game_active = False
# Add new pipes
if pipes[-1].x < WIDTH - 200:
pipes.append(Pipe(WIDTH))
# Check ground collision
if bird.rect.bottom >= HEIGHT - 50:
game_active = False
# Draw everything
screen.fill(SKY_BLUE)
# Draw pipes
for pipe in pipes:
pygame.draw.rect(screen, PIPE_GREEN, pipe.top_rect)
pygame.draw.rect(screen, PIPE_GREEN, pipe.bottom_rect)
# Draw bird
pygame.draw.circle(screen, BIRD_YELLOW, (bird.rect.centerx, bird.rect.centery), 15)
# Draw ground
pygame.draw.rect(screen, GROUND_BROWN, (0, HEIGHT-50, WIDTH, 50))
pygame.display.update()
clock.tick(60)
if __name__ == "__main__":
main()
Key components:
-
Bird Mechanics
- Circular bird with gravity and jump mechanics[1][4]
Bird class handles vertical movement and collision detection[6]
-
Pipe Generation
- Randomly generated pipes with consistent gap size[8]
Pipe class manages position and collision rectangles[3][6]
-
Collision Detection
- Rectangle-based collision checking for pipes and ground[6][8]
- Game over when bird hits pipes or ground[4][6]
-
Simplified Graphics
- Basic shapes replace image assets
- Color-coded elements for visual clarity[2][7]
To play:
- Press SPACE to make the bird jump
- Avoid green pipes and ground
- Game ends on collision
This implementation focuses on core mechanics while maintaining simplicity. For a more complete version, you could add:
- Score tracking
- Sound effects
- Start/restart menus
- High score system[4][7]