-
-
Save stephengruppetta/12e434784a5a59289756cdc2abd319ce 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
# magic_sparkle.py | |
import random | |
import turtle | |
import time | |
from dot import Dot | |
BACKGROUND_COLOUR = "#1a6b72" | |
spawn_delay_range = 0.15, 0.25 | |
dot_size_range = 0.05, 0.5 | |
dot_speed_range = 0.5, 1 | |
lead_dot_speed = 1 | |
lead_dot_rotation_angle = 5 | |
window = turtle.Screen() | |
window.tracer(0) | |
window.bgcolor(BACKGROUND_COLOUR) | |
lead_dot = turtle.Turtle() | |
lead_dot.shape("circle") | |
lead_dot.color("white") | |
lead_dot.penup() | |
def turn_left(): | |
lead_dot.left(lead_dot_rotation_angle) | |
def turn_right(): | |
lead_dot.right(lead_dot_rotation_angle) | |
window.onkeypress(turn_left, "Left") | |
window.onkeypress(turn_right, "Right") | |
window.listen() | |
dots = [] | |
interval_start = 0 | |
while True: | |
lead_dot.forward(lead_dot_speed) | |
# Spawn a new dot after a random delay | |
if time.time() - interval_start > delay: | |
interval_start = time.time() | |
delay = ( | |
random.random() | |
* (spawn_delay_range[1] - spawn_delay_range[0]) | |
+ spawn_delay_range[0] | |
) | |
dots.append( | |
Dot( | |
lead_dot.xcor(), | |
lead_dot.ycor(), | |
dot_size_range, | |
dot_speed_range, | |
) | |
) | |
# Move all the dots | |
for dot in dots: | |
dot.fall() | |
if dot.ycor() < -window.window_height() / 2: | |
dots.remove(dot) | |
turtle.turtles().remove(dot) | |
print(f"\nNumber of dots in the 'dots' list: {len(dots)}") | |
print(f"Number of dots in the 'turtle.turtles()' list: {len(turtle.turtles())}") | |
window.update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment