make a flappy bird game in pygame
- User
make a flappy bird game in pygame
Assistant
I'll create a Flappy Bird game using pygame for you. Here's a complete implementation:
import pygame import random import sys # Initialize Pygame pygame.init() # Constants SCREEN_WIDTH = 400 SCREEN_HEIGHT = 600 BIRD_WIDTH = 34 BIRD_HEIGHT = 24 PIPE_WIDTH = 52 PIPE_GAP = 150 GRAVITY = 0.5 JUMP_STRENGTH = -8 PIPE_SPEED = 3 # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) GREEN = (0, 255, 0) BLUE = (135, 206, 235) YELLOW = (255, 255, 0) class Bird: def __init__(self): self.x = 50 self.y = SCREEN_HEIGHT // 2 self.velocity = 0 self.rect = pygame.Rect(self.x, self.y, BIRD_WIDTH, BIRD_HEIGHT) def jump(self): self.velocity = JUMP_STRENGTH def update(self): self.velocity += GRAVITY self.y += self.velocity self.rect.y = self.y def draw(self, screen): pygame.draw.ellipse(screen, YELLOW, self.rect) # Draw a simple eye pygame.draw.circle(screen, BLACK, (self.x + 20, self.y + 8), 3) class Pipe: def __init__(self, x): self.x = x self.height = random.randint(100, SCREEN_HEIGHT - PIPE_GAP - 100) self.top_rect = pygame.Rect(x, 0, PIPE_WIDTH, self.height) self.bottom_rect = pygame.Rect(x, self.height + PIPE_GAP, PIPE_WIDTH, SCREEN_HEIGHT - self.height - PIPE_GAP) self.passed = False def update(self): self.x -= PIPE_SPEED self.top_rect.x = self.x self.bottom_rect.x = self.x def draw(self, screen): pygame.draw.rect(screen, GREEN, self.top_rect) pygame.draw.rect(screen, GREEN, self.bottom_rect) # Draw pipe caps pygame.draw.rect(screen, GREEN, (self.x - 5, self.height - 20, PIPE_WIDTH + 10, 20)) pygame.draw.rect(screen, GREEN, (self.x - 5, self.height + PIPE_GAP, PIPE_WIDTH + 10, 20)) def collides_with(self, bird): return bird.rect.colliderect(self.top_rect) or bird.rect.colliderect(self.bottom_rect) class Game: def __init__(self): self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Flappy Bird") self.clock = pygame.time.Clock() self.font = pygame.font.Font(None, 36) self.reset_game() def reset_game(self): self.bird = Bird() self.pipes = [] self.score = 0 self.game_over = False self.game_started = False def spawn_pipe(self): if not self.pipes or self.pipes[-1].x < SCREEN_WIDTH - 200: self.pipes.append(Pipe(SCREEN_WIDTH)) def update(self): if not self.game_started or self.game_over: return self.bird.update() # Check if bird hits ground or ceiling if self.bird.y > SCREEN_HEIGHT - BIRD_HEIGHT or self.bird.y < 0: self.game_over = True # Update pipes for pipe in self.pipes[:]: pipe.update() # Remove pipes that are off screen if pipe.x < -PIPE_WIDTH: self.pipes.remove(pipe) # Check collision if pipe.collides_with(self.bird): self.game_over = True # Check if bird passed pipe if not pipe.passed and pipe.x + PIPE_WIDTH < self.bird.x: pipe.passed = True self.score += 1 # Spawn new pipes self.spawn_pipe() def draw(self): # Draw background self.screen.fill(BLUE) # Draw bird self.bird.draw(self.screen) # Draw pipes for pipe in self.pipes: pipe.draw(self.screen) # Draw score score_text = self.font.render(f"Score: {self.score}", True, WHITE) self.screen.blit(score_text, (10, 10)) # Draw instructions or game over if not self.game_started: start_text = self.font.render("Press SPACE to start", True, WHITE) text_rect = start_text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2)) self.screen.blit(start_text, text_rect) elif self.game_over: game_over_text = self.font.render("Game Over!", True, WHITE) restart_text = self.font.render("Press R to restart", True, WHITE) game_over_rect = game_over_text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2 - 20)) restart_rect = restart_text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2 + 20)) self.screen.blit(game_over_text, game_over_rect) self.screen.blit(restart_text, restart_rect) pygame.display.flip() def handle_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: return False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: if not self.game_started: self.game_started = True elif not self.game_over: self.bird.jump() elif event.key == pygame.K_r and self.game_over: self.reset_game() return True def run(self): running = True while running: running = self.handle_events() self.update() self.draw() self.clock.tick(60) pygame.quit() sys.exit() if __name__ == "__main__": game = Game() game.run()This Flappy Bird implementation includes:
Core Features:
- A yellow bird that falls due to gravity
- Green pipes with gaps that move from right to left
- Collision detection between bird and pipes
- Score tracking when passing through pipes
- Game over when hitting pipes, ground, or ceiling
Controls:
- Press SPACE to start the game
- Press SPACE to make the bird jump (flap)
- Press R to restart after game over
Game Mechanics:
- The bird continuously falls due to gravity
- Each space press gives the bird an upward boost
- Pipes spawn automatically with random gap positions
- Score increases each time you successfully pass through a pipe
- Game ends on collision or flying out of bounds
To run this game, you'll need to install pygame:
pip install pygameThe game uses simple geometric shapes for graphics, making it lightweight and easy to modify. You can customize colors, speeds, pipe spacing, or add features like high scores, sound effects, or sprite images.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.