Created
January 10, 2023 18:06
-
-
Save stephengruppetta/bbda24aefdcf0f440e2ec41601b265b5 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 | |
speed = 5 | |
max_interval = 0.3 | |
window = turtle.Screen() | |
window.tracer(0) | |
window.bgcolor(0.2, 0.2, 0.2) | |
dots = [] | |
interval = 0 # initial value only | |
spawn_time = time.time() | |
while True: | |
# Spawn new dots at random time intervals | |
if time.time() - spawn_time > interval: | |
interval = random.random() * max_interval | |
spawn_time = time.time() | |
# Create new turtle | |
dot = turtle.Turtle() | |
dot.shape("circle") | |
dot.color( | |
random.random(), | |
random.random(), | |
random.random(), | |
) | |
dot.penup() | |
dot.left(random.randint(0, 359)) | |
# Add to list | |
dots.append(dot) | |
# Move all dots forward and clear those | |
# that leave the screen | |
for dot in dots: | |
dot.forward(speed) | |
# Remove dots that leave screen | |
if ( | |
abs(dot.xcor()) > window.window_width() / 2 | |
or abs(dot.ycor()) > window.window_height() / 2 | |
): | |
dot.hideturtle() | |
# Update needed here to remove image of dot | |
# before removing object | |
window.update() | |
dots.remove(dot) | |
turtle.turtles().remove(dot) | |
window.title(f"{len(dots) = } | {len(turtle.turtles()) = }") | |
window.update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment