#Trisha
#6/14/2023
#eating game

#constants
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 600

#character constants
chara_x = 300
chara_y = 300

radius = 20

dy = 30
dx = 30

#enemy constants
enemy_radius = 15
enemy_x = 400
enemy_y = 400
speed = 5
sin_y = 200
sin_height = 50

collided = False

#init
import pygame, sys, math, random
from pygame.locals import *
pygame.init()

#window settings
DISPLAYSURF = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("eating game")

def checkCollide(cY, cX, r, eY, eX): #returns true if collide
    #circle formula: (x - h)^2 + (y - k)^2 = r2

    distSq = (cX - eX)**2 + (cY - eY)**2

    return distSq <= r**2 # check if the enemy point is within the circle

#game loop
while True:

    #event handler
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN: #checks if key is pressed

            #checks for key
            if event.key == K_w:
                chara_y += -dy
                
            if event.key == K_s:
                chara_y += dy
                
            if event.key == K_a:
                chara_x += -dx
                
            if event.key == K_d:
                chara_x += dx
                
            if event.key == K_SPACE:#respawn our enemy
                enemy_x = random.randint(15, 585)
                collided = False
        
    DISPLAYSURF.fill("black")

    #character
    pygame.draw.circle(DISPLAYSURF, "magenta", (chara_x, chara_y), radius)

    #ENEMY
    t = pygame.time.get_ticks() / speed 
    enemy_y = math.sin(t/50.0) * sin_height + sin_y

    if not collided: 
        pygame.draw.circle(DISPLAYSURF, "blue", (enemy_x, enemy_y), enemy_radius)

    #collisions
    if checkCollide(chara_y, chara_x, radius, enemy_y, enemy_x):
        #print("collided")
        collided = True
        
        #grow chracter
        radius += enemy_radius/2 + (.15*radius)

        #update enemy
        enemy_x = 700 #outside screen
    
    pygame.display.update()
