# Arup Guha
# Dot Game
# 6/14/2024
# This is the dot game functionality in one file called from dotmain.py

import random
import math
import time
import dotconsts
import dotfunctions
import pygame, sys
from dotfile import dot
from pygame.locals import *

def main_game(color):

    # Basic Set Up
    pygame.init()
    DISPLAYSURF = pygame.display.set_mode((dotconsts.SCREEN_W, dotconsts.SCREEN_H))
    pygame.display.set_caption("Dot Game!")
    clock = pygame.time.Clock()

    font = pygame.font.SysFont(None, 50)

    #clock = time.time()

    # Enemy dots will be stored here.
    dots = []

    # My initial position.
    me = dot(dotconsts.SCREEN_W//2,dotconsts.SCREEN_H//2,0,0,20,color)

    #curT = time.clock()
    score = 0
    dropped = 0
    step = 0
    alive = True
    timeEnd = -1

    # Main game loop starts here.
    while True:
        
        for event in pygame.event.get():

            # X out at the top right.
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            # Just looking for key presses to change the dot movement.
            if event.type == KEYDOWN:

                # Using the arrow keys for their natural meaning.
                if event.key == K_DOWN:
                    me.changeVelocity(0,dotconsts.DELTA_ME)
                elif event.key == K_UP:
                    me.changeVelocity(0,-dotconsts.DELTA_ME)
                elif event.key == K_RIGHT:
                    me.changeVelocity(dotconsts.DELTA_ME,0)
                elif event.key == K_LEFT:
                    me.changeVelocity(-dotconsts.DELTA_ME,0)
                 
        # I add dots once every 20 frames.
        if step%20 == 0:
            x = random.randint(1, dotconsts.SCREEN_W)
            y = random.randint(1, dotconsts.SCREEN_H)
            dx = random.randint(-dotconsts.DELTA+1, dotconsts.DELTA-1)
            dy = random.randint(-dotconsts.DELTA+1, dotconsts.DELTA-1)
            diameter = 2*random.randint(5,50)
            dots.append(dot(x,y,dx,dy,diameter,dotconsts.PINK))

        # Move stuff
        dotfunctions.move(dots)
        me.move()
        
        # See if I am eating a dot!
        for item in dots:
            if me.hit(item):

                # I die :(
                if not me.isBigger(item):
                    alive = False

                # I grow - my new area is your area plus mine!
                else :
                    me.eat(item)  
                    dots.remove(item)

        # So my list doesn't grow endlessly!
        dotfunctions.removeUseless(dots)

        # Only draw if alive.
        if alive:
        
            DISPLAYSURF.fill(dotconsts.WHITE)

            # Draw all the dots.
            for item in dots:
                item.draw(DISPLAYSURF)

            # Draw me.
            me.draw(DISPLAYSURF)


        # Conditions that end the game - you hit a bigger dot or got ran off the screen!
        if not alive and timeEnd == -1:
            dotfunctions.draw("Sorry, you died with a score of "+str(me.diameter), font, "white", DISPLAYSURF, 250, 400)
            timeEnd = time.time()
            
        elif me.offScreen() and timeEnd == -1:
            alive = False
            dotfunctions.draw("Sorry, you ran off the screen (died) with a score of "+str(me.diameter), font, "white", DISPLAYSURF, 80, 400)
            timeEnd = time.time()

        pygame.display.update()

        # Here we finally return the score to the main screen.
        if not alive and time.time() - timeEnd > 2:
            return me.diameter
            
        clock.tick(30)
        step += 1
