Aug-23-2017, 07:28 PM
Hi!
I am trying to simulate rain, similar to this example: hxxps://www.youtube.com/watch?v=e5LCLDoVx2I
The code you see below can so far create drops in random colors in random locations at the top of the canvas. However, I can't make the rain fall down, no matter what I try. Can someone give me a hint on how to make each drop fall down individually? As you can see, I save the raindrop-objects in a list, and basically when one drop reaches the ground I want to delete it and create a new one.
Btw, the code I already have can probably be improved a lot since I am very new to programming, feel free to critique!
Thank you!!
I am trying to simulate rain, similar to this example: hxxps://www.youtube.com/watch?v=e5LCLDoVx2I
The code you see below can so far create drops in random colors in random locations at the top of the canvas. However, I can't make the rain fall down, no matter what I try. Can someone give me a hint on how to make each drop fall down individually? As you can see, I save the raindrop-objects in a list, and basically when one drop reaches the ground I want to delete it and create a new one.
Btw, the code I already have can probably be improved a lot since I am very new to programming, feel free to critique!
Thank you!!
from tkinter import * from random import randint import random root=Tk() width = 400 height = width colors = ["red", "orange", "yellow", "green", "blue", "violet", "white"] dropcount = width//2 drops = [] background = Canvas(root, width=width, height=height, background ='black') background.pack() class Drop(): def __init__ (self, locationx, locationy, heightdrop, widthdrop, color): self.locationx = locationx self.locationy = locationy self.heightdrop = heightdrop self.widthdrop = widthdrop self.color = color def newdrop(self): drop=background.create_rectangle(self.locationx, self.locationy, self.locationx+self.widthdrop, self.locationy+self.heightdrop, fill=self.color) drops.append(drop) for i in range (dropcount): drop=Drop(randint(0, width), randint(-100, 100), 10, 2, random.choice(colors)) drop.newdrop() root.mainloop()