#Bryan Medina
#6/21/2017
#circleCollision.py
#A look at how to verify if a collision has occured with circles.

#This will work almost identically to squareCollision.py

# Edited on 3/20/20 in COP 2930 to get rid of the flashing color for an
# extended collision. Idea here is to use a flag that determines if it's
# okay to change the color or not. Right after a color is changed, this
# flag is set to false, so no further color changes occur. Only when there
# is no collision again is this flag set back to true to allow for a new
# color change with a new collision.

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


def checkCollision(circle1, circle2):
    """A function that takes in two lists (each containing the properties of a circles) and checking if the two circles  have collided. 

    Parameters
    ----------
    circle1: A list. The list should contain 4 numeric types in it. 
    circle2: A list. The list shoudl contain 4 numeric types in it. 

    Returns
    -------
    collided: A boolean variable. Returns True if the circles have collided and False otherwise.
    """

    center1X = circle1[0] + circle1[4]/2
    center1Y = circle1[1] + circle1[5]/2
    center2X = circle2[0] + circle2[4]/2
    center2Y = circle2[1] + circle2[5]/2
    
    return ((center2X - center1X)**2 + (center2Y - center1Y)**2)**(0.5) <= circle1[4]/2 + circle2[4]/2


pygame.init()
DISPLAYSURF = pygame.display.set_mode((1000, 600))
pygame.display.set_caption("Circle Collision")

#variables to keep track of the width and height of the screen
width  = 1000
height = 600

clock = pygame.time.Clock()

#Keep track of a list of some colors
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)]


#We're going to need two circles.

#We're going to store of all circle properties in a list of lists.
#Each list is going to contain an x position, y position, x speed, y speed, width, height and a color
#(in that order).

circle1 = [600 , 275, 0, 0, 50, 50, pygame.Color(255,  0,  0)]
circle2 = [300 , 275, 0, 0, 50, 50, pygame.Color(0  ,  0,255)]

# Will allow us to prevent flashing colors.
changeColor = True

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()

        #Handle key being pressed
        if event.type == KEYDOWN:
            if event.key == K_DOWN:
                circle1[3] = 20
            if event.key == K_UP:
                circle1[3] = -20
            if event.key == K_LEFT:
                circle1[2] = -20
            if event.key == K_RIGHT:
                circle1[2] = 20

        #Handle key being released
        if event.type == KEYUP:
            circle1[3] = 0
            circle1[2] = 0

                
    circle1[1] += circle1[3]
    circle1[0] += circle1[2]

    # First we check for the collision.
    if(checkCollision(circle1, circle2)):

        # We will only change colors if our flag says to.
        if changeColor:
            circle1[6] = colors[random.randint(0, 4)]
            circle2[6] = colors[random.randint(0, 4)]

            # Next time, if we are still colliding no change will occur.
            changeColor = False

    # If there is no collision, we want this flag to be true again.
    else:
        changeColor = True
    
    # Display everything for this iteration.
    DISPLAYSURF.fill(pygame.Color(0, 0, 0))
    pygame.draw.ellipse(DISPLAYSURF, circle1[6], (circle1[0], circle1[1], circle1[4], circle1[5]), 0)
    pygame.draw.ellipse(DISPLAYSURF, circle2[6], (circle2[0], circle2[1], circle2[4], circle2[5]), 0)

    
    #Change the framerate
    clock.tick(100)
    
    pygame.display.update()
