![Flappy Bird Game in Python](https://thepythonassignmenthelp.com/uploads/blog/2023-12-14-09-55-10nwczbhuegx.png)
- 14th Dec 2023
- 20:25 pm
Question - Create a clone of the popular Flappy Bird game involves concepts like handling gravity, user input for bird control, collision detection with pipes, and scoring mechanisms.
Here's a basic implementation of a Flappy Bird clone in Python using the Pygame library.
To run this code, you'll need to have Pygame installed. You can install it via pip:
pip install pygame
Here's the code:
import pygame
import random
import sys
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 288, 512
FPS = 60
gravity = 0.25
bird_movement = 0
game_active = True
score = 0
high_score = 0
# Colors
white = (255, 255, 255)
black = (0, 0, 0)
yellow = (255, 255, 0)
# Game window setup
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flappy Bird")
clock = pygame.time.Clock()
# Load images
background = pygame.image.load('background.png').convert()
bird_img = pygame.image.load('bird.png').convert_alpha()
pipe_img = pygame.image.load('pipe.png').convert_alpha()
# Scale the bird image
bird = pygame.transform.scale2x(bird_img)
# Define bird and pipe attributes
bird_rect = bird.get_rect(center=(50, HEIGHT // 2))
pipe_list = []
SPAWNPIPE = pygame.USEREVENT
pygame.time.set_timer(SPAWNPIPE, 1200)
pipe_heights = [200, 300, 400]
# Functions to display score and game over text
font = pygame.font.Font('freesansbold.ttf', 32)
def display_score(game_state):
if game_state == 'current_game':
score_surface = font.render(f'Score: {int(score)}', True, white)
window.blit(score_surface, (10, 10))
if game_state == 'game_over':
score_surface = font.render(f'Score: {int(score)}', True, white)
high_score_surface = font.render(f'High Score: {int(high_score)}', True, white)
window.blit(score_surface, (50, 200))
window.blit(high_score_surface, (50, 250))
def create_pipe():
random_pipe_pos = random.choice(pipe_heights)
bottom_pipe = pipe_img.get_rect(midtop=(WIDTH + 50, random_pipe_pos))
top_pipe = pipe_img.get_rect(midbottom=(WIDTH + 50, random_pipe_pos - 150))
return bottom_pipe, top_pipe
def move_pipes(pipes):
for pipe in pipes:
pipe.centerx -= 5
return pipes
def draw_pipes(pipes):
for pipe in pipes:
if pipe.bottom >= HEIGHT:
window.blit(pipe_img, pipe)
else:
flip_pipe = pygame.transform.flip(pipe_img, False, True)
window.blit(flip_pipe, pipe)
def check_collision(pipes):
for pipe in pipes:
if bird_rect.colliderect(pipe):
return False
if bird_rect.top <= -100 or bird_rect.bottom >= 450:
return False
return True
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and game_active:
bird_movement = 0
bird_movement -= 8
if event.key == pygame.K_SPACE and not game_active:
game_active = True
pipe_list.clear()
bird_rect.center = (50, HEIGHT // 2)
bird_movement = 0
score = 0
if event.type == SPAWNPIPE:
pipe_list.extend(create_pipe())
window.blit(background, (0, 0))
if game_active:
# Bird
bird_movement += gravity
rotated_bird = pygame.transform.rotozoom(bird, -bird_movement * 3, 1)
bird_rect.centery += bird_movement
window.blit(rotated_bird, bird_rect)
game_active = check_collision(pipe_list)
# Pipes
pipe_list = move_pipes(pipe_list)
draw_pipes(pipe_list)
# Scoring
for pipe in pipe_list:
if pipe.centerx == 50:
score += 0.5
high_score = max(high_score, score)
display_score('current_game')
else:
display_score('game_over')
pygame.display.update()
clock.tick(FPS)
This code creates a basic version of the Flappy Bird game using Pygame. It includes gravity, user input for bird control (using the spacebar), collision detection with pipes, and scoring mechanisms. The game window is 288x512 pixels.
Make sure to replace 'background.png'
, 'bird.png'
, and 'pipe.png'
with your own image files for the game background, bird, and pipes, respectively.
About The Author - Maya Sharma
Maya Sharma, a passionate game developer and Python enthusiast, shares insights into creating the "Flappy Bird Game in Python." With a creative approach and years of programming experience, Maya guides readers through the process of developing this iconic game. Her commitment to making coding accessible and fun is evident as she unveils the mechanics and intricacies of game development using Python.