# Leo Salazar
# 6/5/2024
# Example of player input in a bad version of the game Agar.io

# Edited 6/7/2024 to implement sound effects and background music
# Also used the distance formula to detect collisions for accuracy

import pygame
import random

# pygame setup
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True
dt = 0
circles = []
player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)
p1 = pygame.Rect(player_pos.x, player_pos.y, 30, 30)

# making a sound object with the sound file
eating = pygame.mixer.Sound("munchSound.mp3")

# loading a music file for playback
pygame.mixer.music.load("wiiShopBossaNova.mp3")

# Checks if the player has collided with the enemy circle
# increases player size and removes enemy circle if so
def checkCol():

    '''
    for x in circles:
        if p1.colliderect(x[0]):
            p1.width += 5
            p1.height += 5
            circles.remove(x)
            # plays the sound of eating whenever the player collides with a circle
            pygame.mixer.Sound.play(eating)
    '''

    # distance formula calculation for collision
    for circle in circles:
        if ((p1.center[0] - circle[0].center[0])**2 + (p1.center[1] - circle[0].center[1])**2)**.5 < p1.width/2 + circle[0].width/2:
            p1.width += 5
            p1.height += 5
            circles.remove(circle)

            # play eating sound every time player collides with a circle
            pygame.mixer.Sound.play(eating)

# Makes n circles in random locations with random colors
# Stores their information in the circles list for future reference
def make_circles(n):
    for i in range(n):
        color = pygame.Color(random.randint(0,255), random.randint(0,255), random.randint(0,255))
        x = random.randint(0, screen.get_width()-10)
        y = random.randint(0, screen.get_height()-10)
        rectangle = pygame.Rect(x, y, 10, 10)
        circles.append([rectangle, color])

# Draws the circles in the circles list on the screen
def draw_circles():
    for x in circles:
        pygame.draw.ellipse(screen, x[1], x[0])

# Makes sure the player stays confined within the walls of the screen
def checkBorders():
    if p1.left < 0:
        p1.left = 0
    if p1.right > screen.get_width():
        p1.right = screen.get_width()
    if p1.top < 0:
        p1.top = 0
    if p1.bottom> screen.get_height():
        p1.bottom = screen.get_height()

make_circles(100)

# plays the loaded song loop number of times at the start of the game (-1 = infinite)
pygame.mixer.music.play(-1)

while running:
    # poll for events
    # pygame.QUIT event means the user clicked X to close your window
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # fill the screen with a color to wipe away anything from last frame
    screen.fill("purple")

    # draws the player
    pygame.draw.ellipse(screen, "red", p1)

    # draws all the enemy circles
    draw_circles()

    # reads keyboard input and performs appropriate movement
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
        p1.y -= 300 * dt
    if keys[pygame.K_s]:
        p1.y += 300 * dt
    if keys[pygame.K_a]:
        p1.x -= 300 * dt
    if keys[pygame.K_d]:
        p1.x += 300 * dt

    # makes sure the player doesn't leave the screen
    checkBorders()
    # checks if player has consumed the enemy
    checkCol()

    # flip() the display to put your work on screen
    pygame.display.flip()

    # limits FPS to 60
    # dt is delta time in seconds since last frame, used for framerate-
    # independent physics.
    dt = clock.tick(60) / 1000

pygame.quit()