# Arup Guha
# 6/4/2026
# Edit of the bounceballsplit.py example.
# This one has two balls collide both disappear.

import random
import math
import time
import pygame, sys

from TokenFile import token
from pygame.locals import *

# We'll assign colors to each ball sequentially from this list with wraparound.
COLORLIST = [pygame.Color("red"), pygame.Color("green"), pygame.Color("purple"),
             pygame.Color("black"), pygame.Color("yellow"), pygame.Color("aquamarine"),
             pygame.Color("blue"), pygame.Color("orange"), pygame.Color("pink"),
             pygame.Color("violet")]

# Useful Constants
SCREEN_W = 1000
SCREEN_H = 600
BALL_R = 15
NUM_BALLS = 10

# Returns a random integer in between low and high not equal to 0.
def myrand(low,high):
    res = 0
    while res == 0:
        res = random.randint(low, high)
    return res

def main():

    # Basic Set Up
    pygame.init()
    DISPLAYSURF = pygame.display.set_mode((SCREEN_W, SCREEN_H))
    pygame.display.set_caption("Object Oriented Bouncing")

    clock = pygame.time.Clock()

    # Make NUM_BALLS random tokens.
    mytokens = []
    for i in range(NUM_BALLS):

        # Somewhere on the screen.
        x = random.randint(1, SCREEN_W-BALL_R)
        y = random.randint(1, SCREEN_H-BALL_R)

        # Random non-zero movement in both directions.
        dx = myrand(-2,2)
        dy = myrand(-2,2)

        # Add ball, color is assigned as previously described.
        mytok = token(x,y,dx,dy,BALL_R,COLORLIST[i%len(COLORLIST)])
        mytokens.append(mytok)
        
    # Game loop.
    while True:

        # Look for events - we aren't using this right now.
        for event in pygame.event.get():
            
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        # White background.
        DISPLAYSURF.fill(pygame.Color("white"))

        # Essentially update each ball.
        for item in mytokens:
            item.updateFrame(DISPLAYSURF)
            
        # Update what we put on the canvas.
        pygame.display.update()

        # Put list of balls to delete here.
        delList = []

        # Look for collisions.
        for i in range(len(mytokens)):
            for j in range(i+1, len(mytokens)):
                if mytokens[i].collide(mytokens[j]):

                    # Remove i if not in the remove list.
                    if not i in delList:
                        delList.append(i)

                    # Same for j.
                    if not j in delList:
                        delList.append(j)

        # We'll delete from high to low, so sort this in reverse.
        delList.sort(reverse=True)

        # Be careful here, we need to index mytokens with values stored IN delList.
        for i in range(len(delList)):
            del mytokens[ delList[i] ]
            
        # Wait a bit!
        clock.tick(50)

# Run it
main()
