# Arup Guha
# 7/15/2015
# Edit of rain simulation to load an image from a .png file.

import random
import math
import time
import pygame, sys
from pygame.locals import *

# Useful Constants
SCREEN_W = 1000
SCREEN_H = 600
DY = 5
DX = -1
RADIUS = 5
WHITE = pygame.Color(255,255,255)
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 set up.
    pygame.init()
    DISPLAYSURF = pygame.display.set_mode((SCREEN_W, SCREEN_H))
    pygame.display.set_caption("Let it rain!")
    clock = pygame.time.Clock()

    # Store drops here.
    rain = []

    # This is the one image we will use.
    drop = pygame.image.load("raindrop.png")

    # So we know how many frames we've run.
    step = 0

    curT = time.time()

    # Event loop.
    while True:
        
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()    

        # These images are big, so I only add a drop once every 10 time steps.
        if step%10 == 0:
            x = random.randint(1, SCREEN_W)
            rain.append([x,0,DX,DY])
         
        DISPLAYSURF.fill(WHITE)

        # blit allows us to draw a surface onto another surface.
        for item in rain:
            DISPLAYSURF.blit(drop,(item[0], item[1]))

        pygame.display.update()

        # Move the drops for the next iteration and remove useless ones.
        move(rain)
        removeUseless(rain)
        clock.tick(30)
        step += 1

# Run it!
main()

    
