-
-
Save stephengruppetta/ed0d290e761afb6696bc0e19ad42f840 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 random | |
import time | |
import turtle | |
spawn_interval = 0.05 | |
ball_speed = 2 | |
increase_rotation_step = 0.1 | |
# Set up window | |
window = turtle.Screen() | |
window.tracer(0) | |
spinner = turtle.Turtle() | |
spinner.rotation_speed = 0.5 | |
# Spawn new balls | |
colour = random.random(), random.random(), random.random() | |
balls = [] | |
pool_of_balls = [] | |
def spawn_ball(reference): | |
if pool_of_balls: | |
balls.append(pool_of_balls.pop()) | |
balls[-1].showturtle() | |
else: | |
balls.append(turtle.Turtle()) | |
balls[-1].shape("circle") | |
balls[-1].color(colour) | |
balls[-1].turtlesize(0.5) | |
balls[-1].penup() | |
balls[-1].setposition(reference.position()) | |
balls[-1].setheading(reference.heading()) | |
# Change speed of rotation | |
def increase_anticlockwise_rotation(): | |
spinner.rotation_speed += increase_rotation_step | |
def decrease_anticlockwise_rotation(): | |
spinner.rotation_speed -= increase_rotation_step | |
window.onkeypress(increase_anticlockwise_rotation, "Left") | |
window.onkeypress(decrease_anticlockwise_rotation, "Right") | |
window.listen() | |
# Main animation loop | |
spawn_timer = time.time() | |
while True: | |
spinner.left(spinner.rotation_speed) | |
# Spawn new ball | |
if time.time() - spawn_timer > spawn_interval: | |
spawn_ball(spinner) | |
spawn_timer = time.time() | |
# Move all balls and clear balls that leave the screen | |
for ball in balls.copy(): | |
ball.forward(ball_speed) | |
# Check if ball has left the screen | |
if ( | |
abs(ball.xcor()) > window.window_width() / 2 | |
or abs(ball.ycor()) > window.window_height() / 2 | |
): | |
ball.hideturtle() | |
balls.remove(ball) | |
pool_of_balls.append(ball) | |
print(len(balls), len(pool_of_balls), len(turtle.turtles())) | |
window.update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment