Untitled
unknown
plain_text
9 months ago
3.7 kB
3
Indexable
import pygame
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Flappy Bird")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
# Clock
clock = pygame.time.Clock()
# Game variables
gravity = 0.25
bird_movement = 0
score = 0
high_score = 0
game_active = True
# Load images
bird_surface = pygame.image.load("bird.png").convert_alpha()
bird_rect = bird_surface.get_rect(center=(100, SCREEN_HEIGHT // 2))
pipe_surface = pygame.image.load("pipe.png").convert_alpha()
pipe_list = []
SPAWNPIPE = pygame.USEREVENT
pygame.time.set_timer(SPAWNPIPE, 1200)
# Font
font = pygame.font.Font(None, 50)
# Functions
def draw_floor():
screen.fill(GREEN)
pygame.draw.rect(screen, WHITE, (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 50))
def create_pipe():
random_pipe_pos = random.choice([200, 300, 400])
bottom_pipe = pipe_surface.get_rect(midtop=(500, random_pipe_pos))
top_pipe = pipe_surface.get_rect(midbottom=(500, random_pipe_pos - 150))
return bottom_pipe, top_pipe
def move_pipes(pipes):
for pipe in pipes:
pipe.centerx -= 3
return pipes
def draw_pipes(pipes):
for pipe in pipes:
if pipe.bottom >= SCREEN_HEIGHT:
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 >= SCREEN_HEIGHT - 50:
return False
return True
def update_score(score, high_score):
if score > high_score:
high_score = score
return high_score
def display_score(game_active):
if game_active:
score_surface = font.render(f"Score: {int(score)}", True, BLACK)
score_rect = score_surface.get_rect(center=(SCREEN_WIDTH // 2, 50))
screen.blit(score_surface, score_rect)
else:
score_surface = font.render(f"Score: {int(score)}", True, BLACK)
score_rect = score_surface.get_rect(center=(SCREEN_WIDTH // 2, 50))
screen.blit(score_surface, score_rect)
high_score_surface = font.render(f"High Score: {int(high_score)}", True, BLACK)
high_score_rect = high_score_surface.get_rect(center=(SCREEN_WIDTH // 2, 100))
screen.blit(high_score_surface, high_score_rect)
# Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and game_active:
bird_movement = -6
if event.key == pygame.K_SPACE and not game_active:
game_active = True
pipe_list.clear()
bird_rect.center = (100, SCREEN_HEIGHT // 2)
bird_movement = 0
score = 0
if event.type == SPAWNPIPE:
pipe_list.extend(create_pipe())
# Draw background
draw_floor()
if game_active:
# Bird movement
bird_movement += gravity
bird_rect.centery += bird_movement
screen.blit(bird_surface, bird_rect)
# Pipes
pipe_list = move_pipes(pipe_list)
draw_pipes(pipe_list)
# Collision
game_active = check_collision(pipe_list)
# Score
score += 0.01
display_score(game_active)
else:
high_score = update_score(score, high_score)
display_score(game_active)
# Update display
pygame.display.update()
clock.tick(120)Editor is loading...
Leave a Comment