Untitled
unknown
plain_text
2 years ago
2.1 kB
9
Indexable
import pygame
import random
import os
# Initialize Pygame
pygame.init()
# Set up some constants
WIDTH = 800
HEIGHT = 600
BIRD_SIZE = 40
PIPE_WIDTH = 100
PIPE_GAP = 200
PIPE_SPEED = 5
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Flappy Bird')
# Set up the clock
clock = pygame.time.Clock()
# Set up the bird
bird_surface = pygame.image.load('bird.png').convert()
bird_rect = bird_surface.get_rect(center=(100, 300))
# Set up the pipes
pipe_surface = pygame.image.load('pipe.png').convert()
pipe_list = []
# Set up the score
score = 0
font = pygame.font.Font(None, 36)
# Game loop
running = True
while running:
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird_rect.y -= 50
# Bird movement
bird_rect.y += 5
if bird_rect.y > HEIGHT - BIRD_SIZE:
bird_rect.y = HEIGHT - BIRD_SIZE
# Add new pipes
if len(pipe_list) < 5:
new_pipe = pygame.Rect(WIDTH, random.randint(0, HEIGHT-PIPE_GAP-PIPE_WIDTH), PIPE_WIDTH, PIPE_GAP)
pipe_list.append(new_pipe)
# Move and draw pipes
for pipe in pipe_list:
pipe.x -= PIPE_SPEED
if pipe.x < -PIPE_WIDTH:
pipe_list.remove(pipe)
pygame.draw.rect(screen, (0, 255, 0), pipe)
# Check for collisions
for pipe in pipe_list:
if bird_rect.colliderect(pipe):
running = False
# Check for score
for pipe in pipe_list:
if bird_rect.x > pipe.x and bird_rect.x < pipe.x + PIPE_WIDTH:
score += 1
# Draw the bird
screen.blit(bird_surface, bird_rect)
# Draw the score
score_surface = font.render(f'Score: {score}', True, (255, 255, 255))
score_rect = score_surface.get_rect(center=(WIDTH // 2, 50))
screen.blit(score_surface, score_rect)
# Update the display
pygame.display.flip()
clock.tick(60)
# Quit Pygame
pygame.quit()Editor is loading...
Leave a Comment