Created
January 28, 2023 11:01
-
-
Save stephengruppetta/bc11eb6804b7c694c21cd4bee68a203c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle | |
import random | |
import time | |
image_size = 125 | |
falling_speed = 2 | |
max_time_interval = 3 | |
image = "snowflake-g3d31b3007_125.gif" | |
window = turtle.Screen() | |
window.bgcolor(0.7, 0.8, 0.9) | |
window.tracer(0) | |
window.register_shape(image) | |
# Create snowflakes and add to list | |
all_snow = [] | |
def create_snow(): | |
snow = turtle.Turtle() | |
snow.shape(image) | |
snow.penup() | |
snow.sety(window.window_height() / 2 + image_size) | |
snow.setx( | |
random.randint( | |
-window.window_width() // 2, | |
window.window_width() // 2, | |
) | |
) | |
snow.setheading(-90) | |
all_snow.append(snow) | |
# Animation loop | |
start = time.time() | |
interval = 0 | |
while True: | |
# Create new snowflake when time interval elapsed | |
if time.time() - start > interval: | |
create_snow() | |
start = time.time() | |
interval = random.random() * max_time_interval | |
# Move all snowflakes | |
for snow in all_snow.copy(): | |
snow.forward(falling_speed) | |
# Remove snowflake if it leaves the screen | |
if snow.ycor() < -window.window_height() / 2 - image_size: | |
snow.hideturtle() | |
all_snow.remove(snow) | |
turtle.turtles().remove(snow) | |
window.update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment