flappy bird.py
2 years ago in Python
import pygame
import sys
import random
def draw_floor():
screen.blit(floor_surface, (floor_x_pos, 450))
screen.blit(floor_surface, (floor_x_pos + 288, 450))
def create_pipe():
random_pipe_pos = random.choice(pipe_height)
bottom_pipe = pipe_surface.get_rect(midtop = (350, random_pipe_pos))
top_pipe = pipe_surface.get_rect(midbottom = (350, random_pipe_pos - 150))
return bottom_pipe, top_pipe
def move_pipes(pipes):
for pipe in pipes:
pipe.centerx -= 2
return pipes
def draw_pipes(pipes):
for pipe in pipes:
if pipe.bottom >= 512:
screen.blit(pipe_surface, pipe)
else:
flip_pipe = pygame.transform.flip(pipe_surface, False, True)
screen.blit(flip_pipe, pipe)
def check_collision(pipes):
for pipe in pipes:
if bird_rect.colliderect(pipe):
return False
if bird_rect.top <= -50 or bird_rect.bottom >= 450:
return False
return True
def rotate_bird(bird):
new_bird = pygame.transform.rotozoom(bird, -bird_movement * 3, 1)
return new_bird
def bird_animation():
new_bird = bird_frames[bird_index]
new_bird_rect = new_bird.get_rect(center = (50, bird_rect.centery))
return new_bird, new_bird_rect
def score_display(game_state):
if game_state == 'main_game':
score_surface = game_font.render(str(int(score)), True, (255, 255, 255))
score_rect = score_surface.get_rect(center = (144, 50))
screen.blit(score_surface, score_rect)
if game_state == 'game_over':
score_surface = game_font.render(f'Score: {int(score)}', True, (255, 255, 255))
score_rect = score_surface.get_rect(center = (144, 50))
screen.blit(score_surface, score_rect)
high_score_surface = game_font.render(f'High Score: {int(high_score)}', True, (255, 255, 255))
high_score_rect = high_score_surface.get_rect(center = (144, 425))
screen.blit(high_score_surface, high_score_rect)
def update_high_score(score, high_score):
if score > high_score:
high_score = score
return high_score
pygame.init()
screen = pygame.display.set_mode((288, 512))
clock = pygame.time.Clock()
game_font = pygame.font.Font('04B_19.ttf', 40)
# Game Variables
gravity = 0.25
bird_movement = 0
game_active = True
score = 0
high_score = 0
bg_surface = pygame.image.load('assets/background-day.png').convert()
bg_surface = pygame.transform.scale2x(bg_surface)
floor_surface = pygame.image.load('assets/base.png').convert()
floor_surface = pygame.transform.scale2x(floor_surface)
floor_x_pos = 0
bird_downflap = pygame.transform.scale2x(pygame.image.load('assets/yellowbird-downflap.png').convert_alpha())
bird_midflap = pygame.transform.scale2x(pygame.image.load('assets/yellowbird-midflap.png').convert_alpha())
bird_upflap = pygame.transform.scale2x(pygame.image.load('assets/yellowbird-upflap.png').convert_alpha())
bird_frames = [bird_downflap,