Untitled
unknown
plain_text
a year ago
3.9 kB
7
Indexable
import pygame
import sys
import random
# Initialize pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 400, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flappy Bird Clone with Levels")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (135, 206, 250)
GREEN = (34, 139, 34)
# Clock
clock = pygame.time.Clock()
FPS = 60
# Game variables
gravity = 0.5
bird_movement = 0
game_active = True
score = 0
level = 1
# Bird
bird = pygame.Rect(50, HEIGHT // 2, 30, 30)
# Pipes
pipe_width = 70
pipe_height = 400
pipe_gap = 150
pipe_speed = 3
pipe_list = []
# Add new pipe function
def create_pipe():
random_height = random.randint(150, HEIGHT - pipe_gap - 50)
top_pipe = pygame.Rect(WIDTH, random_height - pipe_gap - pipe_height, pipe_width, pipe_height)
bottom_pipe = pygame.Rect(WIDTH, random_height, pipe_width, pipe_height)
return top_pipe, bottom_pipe
# Move pipes
def move_pipes(pipes):
for pipe in pipes:
pipe.x -= pipe_speed
return [pipe for pipe in pipes if pipe.x > -pipe_width]
# Draw pipes
def draw_pipes(pipes):
for pipe in pipes:
if pipe.y < 0: # Top pipe
pygame.draw.rect(screen, GREEN, pipe)
else: # Bottom pipe
pygame.draw.rect(screen, GREEN, pipe)
# Check collisions
def check_collision(pipes):
for pipe in pipes:
if bird.colliderect(pipe):
return False
if bird.top <= 0 or bird.bottom >= HEIGHT:
return False
return True
# Display score and level
font = pygame.font.Font(None, 40)
def display_score_and_level(score, level):
score_surface = font.render(f"Score: {score}", True, BLACK)
level_surface = font.render(f"Level: {level}", True, BLACK)
screen.blit(score_surface, (10, 10))
screen.blit(level_surface, (10, 50))
# Update difficulty based on level
def update_level(score):
global level, pipe_speed, pipe_gap
if score < 10:
level = 1
pipe_speed = 3
pipe_gap = 150
elif score < 20:
level = 2
pipe_speed = 4
pipe_gap = 130
elif score < 30:
level = 3
pipe_speed = 5
pipe_gap = 110
elif score < 50:
level = 4
pipe_speed = 6
pipe_gap = 90
else:
level = 5
pipe_speed = 7
pipe_gap = 80
# Main game loop
pipe_timer = pygame.USEREVENT
pygame.time.set_timer(pipe_timer, 1500)
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 and game_active:
bird_movement = 0
bird_movement -= 10
if event.key == pygame.K_SPACE and not game_active:
game_active = True
pipe_list.clear()
bird.center = (50, HEIGHT // 2)
bird_movement = 0
score = 0
level = 1
if event.type == pipe_timer and game_active:
pipe_list.extend(create_pipe())
# Background
screen.fill(BLUE)
if game_active:
# Bird
bird_movement += gravity
bird.y += bird_movement
pygame.draw.ellipse(screen, BLACK, bird)
# Pipes
pipe_list = move_pipes(pipe_list)
draw_pipes(pipe_list)
# Collision
game_active = check_collision(pipe_list)
# Scoring
for pipe in pipe_list:
if pipe.centerx == bird.centerx:
score += 1
# Update level
update_level(score)
# Display score and level
display_score_and_level(score, level)
else:
game_over_surface = font.render("Game Over", True, BLACK)
screen.blit(game_over_surface, (WIDTH // 2 - 80, HEIGHT // 2 - 20))
pygame.display.update()
clock.tick(FPS)Editor is loading...
Leave a Comment