# Arup Guha
# 6/2/2022
# Framework for SI@UCF pyGame Program: Balloon Pop

import pygame, sys
from pygame.locals import *
from datetime import datetime

# Returns true iff the point (pX, pY) is in the circle with center (cX, cY)
# with radius r.
def ptInCircle(cX, cY, r, pX, pY):

    # Distance from point to center of the circle, squared.
    distSq = (cX-pX)*(cX-pX) + (cY-pY)*(cY-pY)

    #print(distSq, cX, cY, pX, pY)

    # This means we're within the circle.
    return distSq <= r*r


# Returns true if the ballon with id number idnum (must be 0, 1 or 2), is hit
# by an arrow with top left corner (arrowX, arrowY) with width arrowW.
# Returns false otherwise.
def hitBalloon(idnum, arrowX, arrowY, arrowW):

    # Where the balloon center is.
    bX = idnum*400 + 100
    bY = 50

    # Due to how the arrow always goes, if either of these two points are in the circle, there
    # is an intersection. In real circle intersection code, there's more cases.
    return ptInCircle(bX, bY, 50, arrowX, arrowY) or ptInCircle(bX, bY, 50, arrowX+arrowW, arrowY)
    
def main():

    pygame.init()
    DISPLAYSURF = pygame.display.set_mode((1000, 600))
    pygame.display.set_caption("Pop the Balloons!")

    # Color "constants" we will use.
    ''' ADD ANY OTHER COLORS YOU WANT HERE'''
    black = pygame.Color(0,0,0)
    white = pygame.Color(255,255,255)
    red = pygame.Color(255,0,0)
    purple = pygame.Color(160,32,240)
    blue = pygame.Color(0,0,255)
    green = pygame.Color(0,255,0)

    # To slow down display
    clock = pygame.time.Clock()
    startT = pygame.time.get_ticks()

    # MOVEMENT constant for character.
    DX = 50

    # Variables for me.
    x = 500
    y = 550

    # Variables for "arrow" - initially off the screen.
    xArrow = 500
    yArrow = 700
    DY = 0

    # How many have popped.
    popped = 0

    # Which ones are popped
    poppedLeft = False
    poppedMid = False
    poppedRight = False

    # Event loop.
    while True:

        # Here is where we'll look for key presses.
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            ''' ADD CODE IN THIS IF TO PROCESS 3 KEY EVENTS:
                (1) left key, (2) right key, (3) space bar.
                The first two move you left or right. The last shoots
                an arrow. You can't shoot again until the old one clears the
                screen.
            '''
            if event.type == KEYDOWN:

                ''' CODE IN HERE '''

                
        # Start drawing.
        DISPLAYSURF.fill(white)

        # We always draw ourselves.
        pygame.draw.rect(DISPLAYSURF, black, (x, y, 50, 50), 0)

        # This is sort of a hack, but we always draw the arrow also.
        pygame.draw.rect(DISPLAYSURF, green, (xArrow, yArrow, 3, 50), 0)

        ''' Fill in code inside of this if to process a balloon being hit'''
        for item in range(3):
            if hitBalloon(item, xArrow, yArrow, 3):

                ''' Steps: (1) see which balloon was hit and set its popped
                           (2) set its flag to True
                           (3) Update the # of popped balloons
                           (4) Move the arrow back to its default position
                           (5) Change the arrow velocity back to 0.
                '''

        ''' Fill in code to draw balloons. ONLY draw each one if it's hasn't
            been popped yet.
        '''
    
        ''' If all three balloons have been popped, end the game. Calculate
            the end time and then display on the regular screen the time
            it took the user to pop all 3 balloons. Then quit pygame and exit.
        '''
            
        # We always "move" the arrow.
        yArrow += DY
        
        # Update the display so changes can be seen.
        pygame.display.update()

        # To slow stuff down.
        clock.tick(50)

# Get it started!
main()
