# Arup Guha
# 6/12/2025
# Game where we are a token trying to avoid red tokens and eat green ones.

import random
import math
import time
import pygame, sys
from pygame.locals import *
from TokenFile import token

# Useful Constants
SCREEN_W = 1000
SCREEN_H = 600
DY = 5
RADIUS = 25
WHITE = pygame.Color(255,255,255)
BLUE = pygame.Color(0,0,255)
RED = pygame.Color(255,0,0)
GREEN = pygame.Color(0,255,0)

# This function handles moving each item listed in items.
def move(items):
    for item in items:
        item.move()

# This function removes all items that will never be visible again.
def removeUseless(items):
    for item in items:
        if item.y > SCREEN_H:
            items.remove(item)

def main():

    level = 1
    GOODDOTSPEED = [50,40,30,20,10,5]
    BADDOTSPEED =  [35,30,25,15,8, 3]

    # Basic setup.
    pygame.init()
    DISPLAYSURF = pygame.display.set_mode((SCREEN_W, SCREEN_H))
    pygame.display.set_caption("Catch the green dots! Avoid the red.")
    clock = pygame.time.Clock()

    # Number of lives you have.
    lives = 3

    # Keep score here.
    score = 0

    # Store good and bad dots separately.
    goodDots = []
    badDots = []
    frame = 0

    # This is me.
    me = token(SCREEN_W//2,SCREEN_H-50,0,0,50,BLUE)

    # Game loop.
    while True:
        
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            # Look for a key pressed down.
            if event.type == KEYDOWN:

                # Set the velocity to go left every frame.
                if event.key == K_LEFT:
                    me.setVelocity(-5,0)

                # Velocity to go right.
                if event.key == K_RIGHT:
                    me.setVelocity(5,0)

            # If a key is let up, then we stop immediately.
            if event.type == KEYUP:

                # Has to be the left or right key though...
                if event.key == K_LEFT or event.key == K_RIGHT:
                    me.setVelocity(0,0)

        # We add a good dot once every 50 frames.
        if frame%GOODDOTSPEED[level-1] == 5:
            val = random.randint(1, SCREEN_W)
            goodDots.append(token(val,0,0,DY,RADIUS,GREEN))

        # A bad one every 35.
        if frame%BADDOTSPEED[level-1] == 0:
            val = random.randint(1, SCREEN_W)
            badDots.append(token(val,0,0,DY,RADIUS,RED))

        # Store good dots left.
        goodLeft = []

        # See if I ate any good dots.
        for good in goodDots:
            if me.collide(good):
                score += 1
                print("new score is",score)
            else:
                goodLeft.append(good)
        goodDots = goodLeft

        # Same for bad dots.
        badLeft = []
        for bad in badDots:
            if me.collide(bad):
                lives -= 1
                print("new lives is", lives)
            else:
                badLeft.append(bad)
        badDots = badLeft

        # Update the level every 5 points unless you are at the top level.
        level = min(6, 1 + score//5)

         
        DISPLAYSURF.fill((255,255,255))

        # Draw each raindrop individually.
        for item in goodDots:
            item.draw(DISPLAYSURF)
        for item in badDots:
            item.draw(DISPLAYSURF)

        # And me!
        me.draw(DISPLAYSURF)
        pygame.display.update()

        # Move the drops for the next iteration and remove useless ones.
        move(goodDots)
        removeUseless(goodDots)
        move(badDots)
        removeUseless(badDots)

        # I move also!
        me.move()

        # Update frame count and wait.
        frame += 1
        clock.tick(30)

        # Handle end of game.
        if lives <= 0:
            print("You died!")
            print("Score is",score)
            pygame.quit()
            sys.exit() 

# Run it.
main()
