import pygame import random # Initialize pygame pygame.init() # Constants WIDTH, HEIGHT = 1920, 600 BIRD_X, BIRD_Y = 50, 300 GRAVITY = 0.6 JUMP_STRENGTH = -7 PIPE_WIDTH = 70 PIPE_GAP = 150 PIPE_SPEED = 3 # Colors WHITE = (255, 255, 255) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # Setup display screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Flappy Bird") # Load bird image bird = pygame.Rect(BIRD_X, BIRD_Y, 30, 30) # Pipe list pipes = [] def add_pipe(): y = random.randint(150, 400) pipes.append(pygame.Rect(WIDTH, y, PIPE_WIDTH, HEIGHT - y)) pipes.append(pygame.Rect(WIDTH, 0, PIPE_WIDTH, y - PIPE_GAP)) # Game variables velocity = 0 score = 0 running = True clock = pygame.time.Clock() frames_since_pipe = 0 while running: screen.fill(WHITE) # 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: velocity = JUMP_STRENGTH # Gravity effect velocity += GRAVITY bird.y += velocity # Pipe movement for pipe in pipes: pipe.x -= PIPE_SPEED # Remove off-screen pipes pipes = [pipe for pipe in pipes if pipe.x > -PIPE_WIDTH] # Add new pipes frames_since_pipe += 1 if frames_since_pipe > 90: add_pipe() frames_since_pipe = 0 # Collision detection for pipe in pipes: if bird.colliderect(pipe): running = False if bird.y > HEIGHT or bird.y < 0: running = False # Drawing pygame.draw.rect(screen, BLUE, bird) for pipe in pipes: pygame.draw.rect(screen, GREEN, pipe) pygame.display.update() clock.tick(60) pygame.quit()