0% found this document useful (0 votes)
19 views6 pages

Flappy Bird

This document is a Python script that implements a Flappy Bird game using the Pygame library. It includes functionalities for loading assets, handling game physics, managing pipes, and detecting collisions, as well as displaying start and game over menus. The game runs in a loop, allowing the player to control a bird and score points by navigating through pipes.

Uploaded by

guptaajiibolte
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views6 pages

Flappy Bird

This document is a Python script that implements a Flappy Bird game using the Pygame library. It includes functionalities for loading assets, handling game physics, managing pipes, and detecting collisions, as well as displaying start and game over menus. The game runs in a loop, allowing the player to control a bird and score points by navigating through pipes.

Uploaded by

guptaajiibolte
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

import pygame

import random

# Initialize pygame

pygame.init()

# Set up display

width, height = 400, 600

screen = pygame.display.set_mode((width, height))

pygame.display.set_caption('Flappy Bird')

# Define colors

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

# Load assets

bird_img = pygame.image.load('bird.png') # Replace with your bird image path

bird_img = pygame.transform.scale(bird_img, (30, 30))

background_img = pygame.image.load('background.png') # Replace with your background image path

background_img = pygame.transform.scale(background_img, (width, height))

pipe_img = pygame.image.load('pipe.png') # Replace with your pipe image file path

pipe_img = pygame.transform.scale(pipe_img, (35, 400)) # Scale the pipe image

# Load background music

pygame.mixer.music.load('background.wav') # Replace with your music file path

pygame.mixer.music.set_volume(0.2) # Set the volume (0.0 to 1.0)

pygame.mixer.music.play(-1) # Play the music in a loop (-1 means loop indefinitely)

# Load crash sound

crash_sound = pygame.mixer.Sound('crash_sound.wav') # Replace with your crash sound file path

crash_sound.set_volume(0.5) # Set the volume for the crash sound

# Font

font = pygame.font.Font(None, 40)

small_font = pygame.font.Font(None, 30)


# Game variables

bird_x = 30

bird_y = height // 2

bird_vel = 0

gravity = 0.5

flap_power = -7

pipe_width = 35 # Define pipe width here

pipe_gap = 200

pipe_speed = 3

pipes = []

pipe_frequency = 1500 # milliseconds

last_pipe_time = pygame.time.get_ticks()

score = 0

game_over = False

# Function to create pipes

def create_pipe():

pipe_height = random.randint(100, 400)

pipe = {'x': width, 'y': pipe_height}

return pipe

# Function to move and draw pipes

def move_pipes(pipes):

for pipe in pipes:

pipe['x'] -= pipe_speed

def draw_pipes(pipes):

for pipe in pipes:


# Draw the top pipe (flipped upside down)

top_pipe_img = pygame.transform.flip(pipe_img, False, True)

screen.blit(top_pipe_img, (pipe['x'], pipe['y'] - pipe_gap // 2 - top_pipe_img.get_height()))

# Draw the bottom pipe (normal)

screen.blit(pipe_img, (pipe['x'], pipe['y'] + pipe_gap // 2))

# Function to check for collisions

def check_collision(bird_rect, pipes):

for pipe in pipes:

top_pipe_rect = pygame.Rect(pipe['x'], 0, pipe_width, pipe['y'] - pipe_gap // 2)

bottom_pipe_rect = pygame.Rect(pipe['x'], pipe['y'] + pipe_gap // 2, pipe_width, height)

if bird_rect.colliderect(top_pipe_rect) or bird_rect.colliderect(bottom_pipe_rect):

return True

if bird_rect.top <= 0 or bird_rect.bottom >= height:

return True

return False

# Function to display the start menu

def start_menu():

screen.blit(background_img, (0, 0))

title_text = font.render("Flappy Bird", True, BLACK)

play_text = small_font.render("Press SPACE to Play", True, BLACK)

screen.blit(title_text, (width // 2 - title_text.get_width() // 2, height // 2 - 100))

screen.blit(play_text, (width // 2 - play_text.get_width() // 2, height // 2))

pygame.display.update()

# Function to display the game over menu


def game_over_menu():

screen.blit(background_img, (0, 0))

game_over_text = font.render("Game Over", True, BLACK)

score_text = font.render(f"Score: {score}", True, BLACK)

play_again_text = small_font.render("Press SPACE to Play Again", True, BLACK)

screen.blit(game_over_text, (width // 2 - game_over_text.get_width() // 2, height // 2 - 100))

screen.blit(score_text, (width // 2 - score_text.get_width() // 2, height // 2))

screen.blit(play_again_text, (width // 2 - play_again_text.get_width() // 2, height // 2 + 50))

pygame.display.update()

# Game loop

running = True

in_game = False

while running:

if not in_game:

start_menu()

for event in pygame.event.get():

if event.type == pygame.QUIT:

running = False

if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:

if not in_game:

in_game = True

bird_y = height // 2

bird_vel = 0

pipes.clear()

score = 0

game_over = False

else:

bird_vel = flap_power
if in_game and not game_over:

screen.blit(background_img, (0, 0))

# Bird physics

bird_vel += gravity

bird_y += bird_vel

# Create new pipes

current_time = pygame.time.get_ticks()

if current_time - last_pipe_time > pipe_frequency:

pipes.append(create_pipe())

last_pipe_time = current_time

# Move and draw pipes

move_pipes(pipes)

draw_pipes(pipes)

# Remove pipes that are out of screen

pipes = [pipe for pipe in pipes if pipe['x'] > -pipe_width]

# Draw bird

bird_rect = bird_img.get_rect(center=(bird_x, bird_y))

screen.blit(bird_img, bird_rect)

# Check for collisions

if check_collision(bird_rect, pipes):

crash_sound.play() # Play the crash sound

game_over = True

# Update score
for pipe in pipes:

if pipe['x'] + pipe_width // 2 < bird_x and not pipe.get('scored', False):

score += 1

pipe['scored'] = True

# Display score

score_text = font.render(str(score), True, BLACK)

screen.blit(score_text, (width // 2, 50))

if game_over:

game_over_menu()

for event in pygame.event.get():

if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:

game_over = False

in_game = False

pygame.display.update()

pygame.time.Clock().tick(30)

pygame.quit()

You might also like