# Arup Guha
# 6/4/2026
# Edit of the bounceballsplit.py example.
# This one has two balls collide change to be the average color of both of them!
# Eventually everything becomes brown!

import random
import math
import time
import pygame, sys

from TokenFile import token
from pygame.locals import *

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 = 50

# 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

# Returns the average color of c1 and c2.
def getAvgColor(c1, c2):

    myr = (c1.r + c2.r)//2
    myg = (c1.g + c2.g)//2
    myb = (c1.b + c2.b)//2
    return pygame.Color((myr, myg, myb))

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)
        
        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()

        # Look for collistion,
        for i in range(len(mytokens)):
            for j in range(i+1, len(mytokens)):
                if mytokens[i].collide(mytokens[j]):

                    # Switch dx, dy.
                    tempDX = mytokens[i].dx
                    tempDY = mytokens[i].dy
                    mytokens[i].setVelocity(mytokens[j].dx, mytokens[j].dy)
                    mytokens[j].setVelocity(tempDX, tempDY)

                    # Here we assign both balls the average color of the two
                    # old colors.
                    tempColor = getAvgColor(mytokens[i].color, mytokens[j].color)
                    mytokens[i].color = tempColor
                    mytokens[j].color = tempColor

        # Wait a bit!
        clock.tick(50)

# Run it
main()
