Flappy Bird
Flappy Bird
import random
# Initialize pygame
pygame.init()
# Set up display
pygame.display.set_caption('Flappy Bird')
# Define colors
BLACK = (0, 0, 0)
# Load assets
# Font
bird_x = 30
bird_y = height // 2
bird_vel = 0
gravity = 0.5
flap_power = -7
pipe_gap = 200
pipe_speed = 3
pipes = []
last_pipe_time = pygame.time.get_ticks()
score = 0
game_over = False
def create_pipe():
return pipe
def move_pipes(pipes):
pipe['x'] -= pipe_speed
def draw_pipes(pipes):
if bird_rect.colliderect(top_pipe_rect) or bird_rect.colliderect(bottom_pipe_rect):
return True
return True
return False
def start_menu():
pygame.display.update()
pygame.display.update()
# Game loop
running = True
in_game = False
while running:
if not in_game:
start_menu()
if event.type == pygame.QUIT:
running = False
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:
# Bird physics
bird_vel += gravity
bird_y += bird_vel
current_time = pygame.time.get_ticks()
pipes.append(create_pipe())
last_pipe_time = current_time
move_pipes(pipes)
draw_pipes(pipes)
# Draw bird
screen.blit(bird_img, bird_rect)
if check_collision(bird_rect, pipes):
game_over = True
# Update score
for pipe in pipes:
score += 1
pipe['scored'] = True
# Display score
if game_over:
game_over_menu()
game_over = False
in_game = False
pygame.display.update()
pygame.time.Clock().tick(30)
pygame.quit()