# Maria Mauco
# 6/13/2024

# Import pygame
import pygame, sys
from pygame.locals import *

DISPLAYSURF = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()

# colors
white = 'white'
red = 'red'

# Paddle
xP = 50
yP = 600/2

dyP = 20
width = 20
length = 80

# Circle variables
xC = 700
yC = 600/2
dx = 2
dy = 2
radius = 15

# Function that checks if we hit the ball
def checkHit(xPaddle, yPaddle, xBall, yBall):
    hit = False
    if (yPaddle <= yBall and yPaddle + length >= yBall):
        if (xPaddle + radius + width >= xBall):
            hit = True

    return hit
    

# Game
while True:

    clock.tick(80)

    # Handle events
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_DOWN:
                yP += dyP
            elif event.key == K_UP:
                yP -= dyP

    DISPLAYSURF.fill(white)

    # Draw circle
    pygame.draw.circle(DISPLAYSURF, red, (xC,yC), radius)
    xC += dx
    yC += dy

    # Bounce ball
    if (xC >= 800 - radius):
        dx = -dx
    if (xC <= 0 + radius):
        print("You Lost :(")
        pygame.quit()
        sys.exit()

    if (yC >= 600 - radius or yC <= 0 + radius):
        dy= -dy
        
    if (checkHit(xP,yP,xC,yC)): # Bounce ball from paddle
        dx = dx * -1.5

    
    # Draw paddle
    pygame.draw.rect(DISPLAYSURF, 'black', (xP,yP, width, length))

    pygame.display.update()

    
    
