# Arup Guha
# 7/15/2015
# "Rain" simulation - uses lists

import random
import math
import time
import pygame, sys
from pygame.locals import *

# Useful Constants
SCREEN_W = 1000
SCREEN_H = 600
DY = 5
RADIUS = 10
BLACK = pygame.Color(0,0,0)
BLUE = pygame.Color(0,0,255)


# This function handles moving each item listed in items.
def move(items):
    for item in items:
        item[0] += item[2]
        item[1] += item[3]

# This function removes all items that will never be visible again.
def removeUseless(items):
    for item in items:
        if item[1] > SCREEN_H:
            items.remove(item)

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 = []

    # 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([val,0,0,DY])
         
        DISPLAYSURF.fill(BLACK)

        # Draw each raindrop individually.
        for item in rain:
            r = random.randint(0,255)
            g = random.randint(0,255)
            b = random.randint(0,255)
            pygame.draw.circle(DISPLAYSURF, pygame.Color(r,g,b), (item[0], item[1]), RADIUS, 0)

        pygame.display.update()

        # Move the drops for the next iteration and remove useless ones.
        move(rain)
        removeUseless(rain)
        
        clock.tick(30)

# Run it.
main()
