#Bryan Medina
#6/21/2017
#mouseEvent.py
#A look at how mouse events work in pygame.

# Slightly edited by Arup Guha for COP 2930 on 3/25/2020.

#Basic stuff that we always have to do
import pygame, sys
import random
from pygame.locals import *

pygame.init()
DISPLAYSURF = pygame.display.set_mode((1000, 600))
pygame.display.set_caption("Click on the screen and balls will appear")

colors = [pygame.Color(0, 128, 0), pygame.Color(255, 0, 0), pygame.Color(192, 192, 192), pygame.Color(128, 0, 128), pygame.Color(0  ,  0,255)]

#Create a list of circles
circles = []

def clickedOnCircle(mousePosition, circle):
    """A function that takes in the position of the mouse and circle as lists and uses the distance formula to determine if the mouse was clicked inside the circle.

    Parameters
    ----------
    mousePosition: A list/tuple that contains the x and y coordinate of the mouse
    circle: A list that contains the x, y, radius, and color of the circle

    Returns
    -------
    Boolean: true if the mouse was clicked within the radius of the ball.

    Things to note
    --------------
    circle[2] is the radius (not diameter) of the ball
    circle[1] is the y coordinate of the ball
    circle[0] is the x coordinate of the ball
    mousePosition[1] is the y coordinate of the mouse
    mousePosition[0] is the x coordinate of the mouse

    """
    
    return (((mousePosition[0] - (circle[0]+circle[2]))**2 + (mousePosition[1] - (circle[1]+circle[2]))**2)**(0.5)) <= circle[2]
    
while True:

    # Event loop for each iteration
    for event in pygame.event.get():

        # To handle exiting the game.     
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        # Click 
        if event.type == MOUSEBUTTONDOWN:
            # Just look at left mouse button.
            if pygame.mouse.get_pressed()[0]:
                #If the left mouse is pressed, then a new ball with the mouse's x, y, and a random radius and color will appear
                radius = random.randint(1, 20)
                circles.append([event.pos[0] - radius, event.pos[1] - radius, radius, colors[random.randint(0, 4)]])
            # Right mouse button
            if pygame.mouse.get_pressed()[2]:
                for circle in circles:
                    #If you click on a specific circle, then the radius of that circle will increase
                    if(clickedOnCircle(event.pos, circle)):
                        circle[2] += 5

            # Arup Edit -tests the middle mouse button moves circle 100 units to the right looping around
            # to the left end of the screen if necessary.
            if pygame.mouse.get_pressed()[1]:
                for circle in circles:
                    if (clickedOnCircle(event.pos, circle)):
                        circle[0] = (circle[0]+100)%1000
    
    #Fill the background with black
    DISPLAYSURF.fill(pygame.Color(0, 0, 0))
                               
    if(len(circles) != 0): #If there are no circles in the list, then there are no circles to display
        for circle in circles:
            pygame.draw.ellipse(DISPLAYSURF, circle[3], (circle[0], circle[1], circle[2] * 2, circle[2] * 2), 0)

    pygame.display.update()
                               
                
