Creating a complete Flappy Bird game is quite extensive for this platform, but I can provide you with a basic outline and a simple implementation using the Pygame library. You can install Pygame using pip:
pip install pygame
Here's a simple Flappy Bird game implementation in Python 3 using Pygame:
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 400, 600
# Colors
WHITE = (255, 255, 255)
BLUE = (100, 149, 237)
GREEN = (34, 139, 34)
# Game settings
GRAVITY = 0.25
FLAP_STRENGTH = 7
BIRD_X = 100
PIPE_GAP = 200
PIPE_DISTANCE = 200
PIPE_SPEED = 2
# Create the screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flappy Bird")
# Load bird image
bird_img = pygame.image.load("bird.png").convert_alpha()
bird_rect = bird_img.get_rect(center=(BIRD_X, HEIGHT // 2))
# Load font
font = pygame.font.Font(None, 36)
def create_pipe():
pipe_height = random.randint(100, 400)
bottom_pipe = pygame.Rect(WIDTH, HEIGHT - pipe_height, 100, pipe_height)
top_pipe = pygame.Rect(WIDTH, 0, 100, HEIGHT - pipe_height - PIPE_GAP)
return bottom_pipe, top_pipe
def move_pipes(pipes):
for bottom_pipe, top_pipe in pipes:
bottom_pipe.x -= PIPE_SPEED
top_pipe.x -= PIPE_SPEED
return pipes
def draw_pipes(pipes):
for bottom_pipe, top_pipe in pipes:
pygame.draw.rect(screen, GREEN, bottom_pipe)
pygame.draw.rect(screen, GREEN, top_pipe)
def check_collision(bird_rect, pipes):
for bottom_pipe, top_pipe in pipes:
if bird_rect.colliderect(bottom_pipe) or bird_rect.colliderect(top_pipe):
return True
if bird_rect.top <= 0 or bird_rect.bottom >= HEIGHT:
return True
return False
def game_over_screen(score):
score_text = font.render(f"Score: {score}", True, WHITE)
retry_text = font.render("Retry", True, WHITE)
exit_text = font.render("Exit", True, WHITE)
retry_rect = retry_text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
exit_rect = exit_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 50))
while True:
screen.fill(BLUE)
screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, HEIGHT // 4))
screen.blit(retry_text, retry_rect)
screen.blit(exit_text, exit_rect)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
if retry_rect.collidepoint(mouse_pos):
return True # added this line
if exit_rect.collidepoint(mouse_pos):
pygame.quit()
sys.exit()
def game_loop():
bird_rect = bird_img.get_rect(center=(BIRD_X, HEIGHT // 2))
pipes = [create_pipe()]
bird_y_speed = 0
score = 0
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:
bird_y_speed = -FLAP_STRENGTH
# Update bird position
bird_y_speed += GRAVITY
bird_rect.centery += bird_y_speed
# Update pipes
pipes = move_pipes(pipes)
if pipes[0][0].x + 100 < BIRD_X and not pipes[0][0].collidepoint(BIRD_X, 0):
pipes.append(create_pipe())
pipes.pop(0)
score += 1
# Draw the bird and pipes
screen.fill(BLUE)
screen.blit(bird_img, bird_rect)
draw_pipes(pipes)
# Draw the score
score_text = font.render(str(score), True, WHITE)
screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, 50))
# Check for collisions
if check_collision(bird_rect, pipes):
if game_over_screen(score): # added this line
# Reset game state
bird_rect = bird_img.get_rect(center=(BIRD_X, HEIGHT // 2))
pipes = [create_pipe()]
bird_y_speed = 0
score = 0
continue # continue the game loop
# Update the display
pygame.display.update()
# Cap the frame rate
pygame.time.Clock().tick(60)
game_loop()
This code provides a basic yet functional Flappy Bird game that allows the player to control a bird using the space bar that can flap and collide with the top and bottom edges of the game screen. It includes randomly generated pipes that the player must navigate through to score points. The game also includes a scoring system that displays the current score on the screen. If the player collides with a pipe or the top/bottom edges of the screen, a game over screen will appear that allows the player to retry or exit the game. You'll need to add your own bird image (named "bird.png") in the same directory as the script.