import pygame
import sys
import math

# Simple pinball game skeleton using Pygame
def main():
    pygame.init()
    WIDTH, HEIGHT = 600, 800
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    clock = pygame.time.Clock()

    # Ball properties
    ball_pos = [WIDTH // 2, HEIGHT // 4]
    ball_vel = [0, 0]
    gravity = 0.5
    radius = 10

    # Flipper properties
    left_flipper_angle = -30
    right_flipper_angle = 30
    flipper_length = 80
    flipper_speed = 8

    def draw_flipper(center, angle):
        x1, y1 = center
        x2 = x1 + flipper_length * math.cos(math.radians(angle))
        y2 = y1 + flipper_length * math.sin(math.radians(angle))
        pygame.draw.line(screen, (255, 255, 255), (x1, y1), (x2, y2), 8)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            left_flipper_angle = max(-60, left_flipper_angle - flipper_speed)
        else:
            left_flipper_angle = min(-30, left_flipper_angle + flipper_speed)
        if keys[pygame.K_RIGHT]:
            right_flipper_angle = min(60, right_flipper_angle + flipper_speed)
        else:
            right_flipper_angle = max(30, right_flipper_angle - flipper_speed)

        # Update ball physics
        ball_vel[1] += gravity
        ball_pos[0] += ball_vel[0]
        ball_pos[1] += ball_vel[1]

        # Funnel walls
        if ball_pos[1] > HEIGHT - 200:
            if ball_pos[0] < WIDTH // 2:
                ball_vel[0] += 0.2
            else:
                ball_vel[0] -= 0.2

        # Boundary collision
        if ball_pos[0] < radius or ball_pos[0] > WIDTH - radius:
            ball_vel[0] *= -1
        if ball_pos[1] < radius:
            ball_vel[1] *= -1

        screen.fill((0, 0, 0))
        # Draw funnel walls
        pygame.draw.line(screen, (255, 255, 255), (0, HEIGHT - 200), (WIDTH//2, HEIGHT), 4)
        pygame.draw.line(screen, (255, 255, 255), (WIDTH, HEIGHT - 200), (WIDTH//2, HEIGHT), 4)

        # Draw flippers
        draw_flipper((WIDTH//2 - 100, HEIGHT - 100), left_flipper_angle)
        draw_flipper((WIDTH//2 + 100, HEIGHT - 100), 180-right_flipper_angle)

        # Draw ball
        pygame.draw.circle(screen, (255, 255, 255), (int(ball_pos[0]), int(ball_pos[1])), radius)

        pygame.display.flip()
        clock.tick(60)

if __name__ == "__main__":
    main()
