Skip to content

Instantly share code, notes, and snippets.

@stephengruppetta
Last active August 25, 2022 08:17
Show Gist options
  • Save stephengruppetta/f1d613ed7c1a962966d8233719ac0c76 to your computer and use it in GitHub Desktop.
Save stephengruppetta/f1d613ed7c1a962966d8233719ac0c76 to your computer and use it in GitHub Desktop.
import turtle
import random
window = turtle.Screen()
window.bgcolor("light blue")
window.title("It's raining all day today here…")
window.tracer(0)
width = window.window_width()
height = window.window_height()
rain_speed = 10
rain_length = 20
raindrop_colours = [
"white",
"gainsboro",
"light gray",
"silver",
"alice blue",
"white smoke",
"powder blue",
]
x_boundary_left = -width // 2 - width // 5
x_boundary_right = width // 2
# initialise (empty) list
many_raindrops = []
# create turtles and place them in list
# we cannot have multiple turtles with the same name, and we do not want to
# have to create different names for each one
for repeat in range(200):
raindrop = turtle.Turtle()
raindrop.color(random.choice(raindrop_colours))
raindrop.penup()
raindrop.pensize(random.randint(2, 4))
raindrop.right(90) # turtle faces downwards
raindrop.left(10) # give rain another orientation (wind)
# place at top of screen, but random y positions
# out of the screen so that not all rain drops are in same line
raindrop.sety(random.randint(height // 2, height * 4))
# but random x coord
raindrop.setx(random.randint(x_boundary_left, x_boundary_right))
raindrop.pendown()
raindrop.hideturtle()
many_raindrops.append(raindrop)
while True:
# iterate through the list: for each raindrop in raindrops do the following
for raindrop in many_raindrops:
# move raindrop forward at desired speed
raindrop.forward(rain_speed)
raindrop.clear()
# draw raindrop (as line)
raindrop.forward(rain_length)
raindrop.forward(-rain_length)
if raindrop.ycor() < -height // 2: # if raindrop has hit the ground
raindrop.penup()
raindrop.setx(
random.randint(
x_boundary_left, x_boundary_right
)
)
raindrop.sety(height // 2)
raindrop.pendown()
window.update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment