# Arup Guha
# 7/15/2015
# "Rain" simulation - uses lists
# Edited on 6/4/2026 - old code made all raindrops blue, this one uses
#                      random colors.

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 = 10

# 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, SCREEN_HEIGHT):
    for item in items:
        if item.y > SCREEN_HEIGHT:
            items.remove(item)

# Returns a randomly generated color.
def rndColor():
    return pygame.Color(random.randint(0,255), random.randint(0,255), random.randint(0,255) )

def main():

    # Basic setup.
    pygame.init()
    DISPLAYSURF = pygame.display.set_mode((SCREEN_W, SCREEN_H))
    pygame.display.set_caption("Let it rain!")
    clock = pygame.time.Clock()

    # Store all raindrops here.
    rain = []

    frame = 0

    # Game loop.
    while True:
        
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()    

        # Calculate number of new drops to add this iteration, then randomly
        # generate that many unique values.
        numNewRain = random.randint(1, 10)
        xvals = set()
        while len(xvals) < numNewRain:
            x = random.randint(1, SCREEN_W)
            xvals.add(x)

        # Add each if these items into the list rain.
        for val in xvals:
            rain.append(token(x,0,0,DY,RADIUS, rndColor()))
         
        DISPLAYSURF.fill(pygame.Color("white"))

        # Draw each raindrop individually.
        for item in rain:
            item.draw(DISPLAYSURF)

        pygame.display.update()

        #if frame%5 == 0:
        for item in rain:
            item.changeVelocity(0,1)

        # Move the drops for the next iteration and remove useless ones.
        move(rain)
        removeUseless(rain, SCREEN_H)

        frame += 1
        clock.tick(30)

# Run it.
main()
