#BY - VISHNU SIVAKUMAR (10F)
from JMSSGraphics import *
from Particle import *
import random 

#Setting the framework to run the script
jmss = Graphics(width = 800, height = 600, title = "Luigi", fps = 60)

#Creating an 'empty' list 
luigi_list = []

#Initiaing variables 
n = 0
m = 0
#Preloading images/sounds into R.A.M for quicker access
tree = jmss.loadImage("Tree.png")
leaf = jmss.loadImage("leaf1.png")
wind_sound = jmss.loadSound("Wind1.wav", streaming = False)

#Displays the controls on python shell 
print("Use Keys - A & D for wind simulation")

#Plays the sound file 
jmss.playSound(wind_sound, loop = True)

#Creating functions for each of the responsible fields
def Particles():
    #Calling variables from outside the function 
    global n, y, x, img, luigi
    #A while loop to control the amount of particles spawned on the screen
    while n < 100:
        #Creating a 'Particle()' variable
        luigi = Particle()
        #Variables for 'Particles'
        luigi.img = leaf
        luigi.x = random.randint(210, 600)
        luigi.y = random.randint(250, 530)
        luigi_list.append(luigi)
        #Referencing to the element in the list and skipping to the next 
        n += 1


def DrawParticles():
    global Particles, n
    #Wiping the screen and resetting the variable
    jmss.clear()
    n = 0

    #Draws the particles on the screen 
    while n < len(luigi_list):
        #Controlling the speed of the particles 
        luigi_list[n].y = luigi_list[n].y - random.randint(1, 8)
        luigi_list[n].x = luigi_list[n].x - m
        jmss.drawImage(luigi_list[n].img, luigi_list[n].x + random.randint(-2, 2), luigi_list[n].y, width = 30, height = 40)
        n += 1


def Constraints():
    global Particles, n, y
    #Resets the variable 
    n = 0
    #Deleting the particles after they are off the screen from all the three sides 
    while n < len(luigi_list):
        luigi_list[n].y = luigi_list[n].y - random.randint(0,600)
        if luigi_list[n].y < 0 or luigi_list[n].x < 0 or luigi_list[n].x > 800:
            luigi_list.pop(n)
            Particles()
            

def InputKeys():
    global m
    #Uses keys to change the value of the 'm' variable controlling the direction
    if jmss.isKeyDown(KEY_A):
        m += 1
    if jmss.isKeyDown(KEY_D):
        m -= 1
    

#MAIN FUNCTION
@jmss.mainloop
def ParticlesSimulation():
    #Background image(Tree) 
    jmss.drawImage(tree, x = 150 + random.randint(-2, 2), y = 0, width = 500, height = 545)

    #Calling functions 
    InputKeys()
    Particles()
    DrawParticles()
    Constraints()
    
jmss.run()
