Skip to content

Flappy bird  #107

@hadeedjutt

Description

@hadeedjutt

import pygame
import random

pygame.init()

WIDTH, HEIGHT = 288, 512
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flappy Bird")

Game variables

FPS = 60
clock = pygame.time.Clock()

Colors

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

Bird properties

BIRD_X = 50
BIRD_Y = HEIGHT // 2
BIRD_RADIUS = 12
BIRD_VELOCITY = 0
GRAVITY = 0.5
JUMP_STRENGTH = -8

Pipe properties

PIPE_WIDTH = 50
PIPE_GAP = 100
PIPE_VELOCITY = 3
PIPES = []
PIPE_SPAWN_EVENT = pygame.USEREVENT + 1
pygame.time.set_timer(PIPE_SPAWN_EVENT, 1500) # Spawn a pipe every 1.5 seconds

Score

SCORE = 0
FONT = pygame.font.Font(None, 40)

Game state

GAME_OVER = False

def draw_bird(x, y):
pygame.draw.circle(SCREEN, BLUE, (x, y), BIRD_RADIUS)

def create_pipe():
pipe_height = random.randint(50, HEIGHT - PIPE_GAP - 50)
top_pipe = pygame.Rect(WIDTH, 0, PIPE_WIDTH, pipe_height)
bottom_pipe = pygame.Rect(WIDTH, pipe_height + PIPE_GAP, PIPE_WIDTH, HEIGHT - pipe_height - PIPE_GAP)
return top_pipe, bottom_pipe

def draw_pipes(pipes):
for pipe in pipes:
pygame.draw.rect(SCREEN, GREEN, pipe)

def move_pipes(pipes):
for pipe in pipes:
pipe.x -= PIPE_VELOCITY
return [pipe for pipe in pipes if pipe.right > 0]

def check_collision(bird_rect, pipes):
for pipe in pipes:
if bird_rect.colliderect(pipe):
return True
if bird_rect.top <= 0 or bird_rect.bottom >= HEIGHT:
return True
return False

def display_score():
score_text = FONT.render(f"Score: {SCORE}", True, WHITE)
SCREEN.blit(score_text, (10, 10))

def game_over_screen():
game_over_text = FONT.render("Game Over!", True, RED)
restart_text = FONT.render("Press R to Restart", True, WHITE)
SCREEN.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, HEIGHT // 2 - 50))
SCREEN.blit(restart_text, (WIDTH // 2 - restart_text.get_width() // 2, HEIGHT // 2 + 10))

running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and not GAME_OVER:
BIRD_VELOCITY = JUMP_STRENGTH
if event.key == pygame.K_r and GAME_OVER:
# Reset game
BIRD_Y = HEIGHT // 2
BIRD_VELOCITY = 0
PIPES = []
SCORE = 0
GAME_OVER = False
if event.type == PIPE_SPAWN_EVENT and not GAME_OVER:
PIPES.extend(create_pipe())

if not GAME_OVER:
    # Bird movement
    BIRD_VELOCITY += GRAVITY
    BIRD_Y += BIRD_VELOCITY

    bird_rect = pygame.Rect(BIRD_X - BIRD_RADIUS, BIRD_Y - BIRD_RADIUS, BIRD_RADIUS * 2, BIRD_RADIUS * 2)

    # Pipe movement and scoring
    PIPES = move_pipes(PIPES)
    for pipe in PIPES:
        if pipe.right < BIRD_X and pipe.left > BIRD_X - PIPE_VELOCITY and pipe.height > 0: # Only count top pipe
            SCORE += 0.5 # Each pipe is two rects, so count 0.5 for each

    # Collision detection
    if check_collision(bird_rect, PIPES):
        GAME_OVER = True

SCREEN.fill(BLACK)
draw_bird(BIRD_X, int(BIRD_Y))
draw_pipes(PIPES)
display_score()

if GAME_OVER:
    game_over_screen()

pygame.display.update()
clock.tick(FPS)

Pygame.quit()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions