# Ian Binstock
# 6/3/2026
# Uses trig to get some spiral motion!

import pygame, sys, math
from pygame.locals import *

WINDOW_WIDTH = 1000
WINDOW_HEIGHT = 600

pygame.init()
DISPLAYSURF = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Part E - Spiral Movement")

clock = pygame.time.Clock()

CENTER_X = WINDOW_WIDTH // 2
CENTER_Y = WINDOW_HEIGHT // 2

ball_radius = 15
angle = 0.0
spiral_r = 0.0 # distance from center
speed_growth = 0.5
speed_rotation = 0.05

# 1 = outwards, -1 = inwards
direction = 1

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    # Update
    angle += speed_rotation
    spiral_r += speed_growth * direction

    # Calculate x, y
    x = CENTER_X + math.cos(angle) * spiral_r
    y = CENTER_Y + math.sin(angle) * spiral_r

    # Shrink condition
    if direction == 1:
        if (x >= WINDOW_WIDTH - ball_radius or x <= ball_radius or 
            y >= WINDOW_HEIGHT - ball_radius or y <= ball_radius):
            direction = -1

    # Expand condition
    elif direction == -1:
        if spiral_r <= 0:
            spiral_r = 0
            direction = 1

    # Draw
    DISPLAYSURF.fill(pygame.Color("black"))
    pygame.draw.circle(DISPLAYSURF, pygame.Color(147, 233, 190), (int(x), int(y)), ball_radius)
    
    pygame.display.update()
    clock.tick(60)
