# Kathleen Prendergast
# 6/12/2022
# Eat as many squares as possible in 30 seconds

import random
import time
import pygame, sys
from pygame.locals import *

# Returns true if the player square is overlapping a food square,
# otherwise, it returns false
def collision(playerX, playerY, foodX, foodY):

    if (playerX + 25 > foodX and playerY + 25 > foodY and
        foodX + 25 > playerX and foodY +25 > playerY):
        return True

    return False


def main():

    # Setup
    pygame.init()
    DISPLAYSURF = pygame.display.set_mode((1000, 600))
    pygame.display.set_caption("Eat the Squares!")
    clock = pygame.time.Clock()

    # Color constants
    BLACK = pygame.Color(0,0,0)
    WHITE = pygame.Color(255,255,255)
    GREEN = pygame.Color(0,255,0)

    # Player variables
    x = 5
    y = 570

    DX = 0
    DY = 0

    # Food variables
    eatX = []
    eatY = []
    eaten = []

    for i in range(10):
        eatX.append(random.randint(0, 975))
        eatY.append(random.randint(0, 575))
        eaten.append(False)
        
    numFood = 10

    score = 0

    # Event loop
    while True:

        # Check for key presses
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            # Move player
            if event.type == pygame.KEYDOWN:
                # Move left 10 pixels
                if event.key == pygame.K_LEFT:
                    DX = -10
                # Move right 10 pixels
                if event.key == pygame.K_RIGHT:
                    DX = 10
                # Move up 10 pixels
                if event.key == pygame.K_UP:
                    DY = -10
                # Move down 10 pixels
                if event.key == pygame.K_DOWN:
                    DY = 10
            # Stop moving if user is not pressing a key
            if event.type == pygame.KEYUP:
                DX = 0
                DY = 0

        # Start drawing.
        DISPLAYSURF.fill(BLACK)

        # If the player ate all the food, generate 10 new food squares
        # in random locations
        if numFood == 0:
            for i in range(10):
                eatX[i] = random.randint(0, 975)
                eatY[i] = random.randint(0, 575)
                eaten[i] = False
            numFood = 10

        # Draw the player
        pygame.draw.rect(DISPLAYSURF, WHITE, (x, y, 25, 25), 0)

        # Check for eaten food:
        for i in range(10):
            if not eaten[i] and collision(x, y, eatX[i], eatY[i]):
                eaten[i] = True
                score += 1
                numFood -= 1

        # Draw the food squares that have not been eaten
        for i in range(10):
            if not eaten[i]:
                pygame.draw.rect(DISPLAYSURF, GREEN, (eatX[i], eatY[i], 25, 25), 0)

        # Move player in x direction, unless they are at the edge of the screen
        if x < 0:
            x = 0
        elif x > 975:
            x = 975
        else:
            x += DX

        # Move player in y direction, unless they are at the edge of the screen
        if y < 0:
            y = 0
        elif y > 575:
            y = 575
        else:
            y += DY
        
        # End game after 30 seconds
        if pygame.time.get_ticks() >= 30000:
            print("Your score is " + str(score))
            pygame.quit()
            sys.exit()

        pygame.display.update()

        clock.tick(30)
    
main()
