o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
write me a flappy bird in python using pygame
- User
write me a flappy bird in python using pygame
- Assistant
write me a flappy bird in python using pygame
Here's a simple implementation of Flappy Bird in Python using Pygame. This code creates a basic version of the game where you control a bird trying to fly between pipes without hitting them. Press the spacebar to make the bird flap and avoid obstacles.
import pygame
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH = 400
HEIGHT = 600
# Create the screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Flappy Bird')
# Clock for controlling frame rate
clock = pygame.time.Clock()
# Load images (For simplicity, we'll use plain surfaces here)
bird_image = pygame.Surface((34, 24)) # Bird size
bird_image.fill((255, 255, 0)) # Yellow bird
pipe_image = pygame.Surface((52, 320)) # Pipe size
pipe_image.fill((0, 255, 0)) # Green pipe
background_image = pygame.Surface((WIDTH, HEIGHT))
background_image.fill((135, 206, 235)) # Sky blue background
# Bird class
class Bird:
def __init__(self):
self.x = 50
self.y = HEIGHT // 2
self.velocity = 0
self.image = bird_image
def update(self):
self.velocity += 0.5 # Apply gravity
self.y += self.velocity
def flap(self):
self.velocity = -10 # Move up on flap
def draw(self, screen):
screen.blit(self.image, (self.x, self.y))
def get_rect(self):
return pygame.Rect(
self.x, self.y, self.image.get_width(), self.image.get_height()
)
# Pipe class
class Pipe:
GAP = 150 # Gap between top and bottom pipes
VELOCITY = 3 # Pipe movement speed
def __init__(self):
self.x = WIDTH
# Random height for the top pipe
self.height = random.randint(50, HEIGHT - self.GAP - 50)
self.top = self.height - pipe_image.get_height()
self.bottom = self.height + self.GAP
self.pipe_top = pipe_image
self.pipe_bottom = pipe_image
self.passed = False # For scoring
def update(self):
self.x -= self.VELOCITY
def draw(self, screen):
# Draw the top pipe
screen.blit(self.pipe_top, (self.x, self.top))
# Draw the bottom pipe
screen.blit(self.pipe_bottom, (self.x, self.bottom))
def collide(self, bird_rect):
top_rect = pygame.Rect(
self.x, self.top, self.pipe_top.get_width(), self.pipe_top.get_height()
)
bottom_rect = pygame.Rect(
self.x, self.bottom, self.pipe_bottom.get_width(), self.pipe_bottom.get_height()
)
return bird_rect.colliderect(top_rect) or bird_rect.colliderect(bottom_rect)
def main():
bird = Bird()
pipes = [Pipe()]
score = 0
font = pygame.font.SysFont(None, 48) # Font for displaying score
run = True
while run:
clock.tick(30) # Set FPS to 30
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False # Exit game
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird.flap() # Bird flaps on spacebar
# Update bird
bird.update()
# Update pipes
remove_pipes = []
add_pipe = False
for pipe in pipes:
pipe.update()
# Check for collision
if pipe.collide(bird.get_rect()):
run = False # End game on collision
if pipe.x + pipe.pipe_top.get_width() < 0:
remove_pipes.append(pipe)
# Check if bird has passed the pipe for scoring
if not pipe.passed and pipe.x < bird.x:
pipe.passed = True
add_pipe = True
score += 1 # Increment score
if add_pipe:
pipes.append(Pipe()) # Add new pipe
for r in remove_pipes:
pipes.remove(r)
# Check if bird hits the top or bottom of the screen
if bird.y > HEIGHT or bird.y < 0:
run = False # End game if bird goes off screen
# Draw background
screen.blit(background_image, (0, 0))
# Draw pipes
for pipe in pipes:
pipe.draw(screen)
# Draw bird
bird.draw(screen)
# Draw score
score_text = font.render(str(score), True, (255, 255, 255))
screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, 10))
# Update the display
pygame.display.flip()
pygame.quit()
if __name__ == '__main__':
main()
Instructions to Run the Game:
Install Pygame: If you haven't installed Pygame, you can install it using pip:
pip install pygame
Save the Code: Copy the code into a file named flappy_bird.py.
Run the Game: Open a terminal or command prompt in the directory containing flappy_bird.py and run:
python flappy_bird.py
Controls: Press the spacebar to make the bird flap its wings and navigate through the pipes.
Notes:
Enjoy the game and have fun coding!
Not yet included in a published release.