Skip to content

Instantly share code, notes, and snippets.

@stephengruppetta
Created December 18, 2023 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephengruppetta/8ca05da581fb956f57d8339444f41670 to your computer and use it in GitHub Desktop.
Save stephengruppetta/8ca05da581fb956f57d8339444f41670 to your computer and use it in GitHub Desktop.
# Do not try this at home…
import turtle
import random
import itertools
# Starts off fairly "normal"…
n_turtles = 100
window = turtle.Screen()
window.tracer(0)
window.setup(700, 700)
window.bgcolor(0.2, 0.2, 0.2)
# A mammoth list comprehension to create all
# the turtles and set their initial state
cute_turtles = [
(
cute_turtle := turtle.Turtle(),
cute_turtle.left(idx * (360 / n_turtles)),
cute_turtle.color(
random.random(),
random.random(),
random.random(),
),
cute_turtle.penup(),
cute_turtle.shape("turtle"),
)
for idx in range(n_turtles)
]
# Run the animation without a while loop
list(
map(
lambda _: (
[
(
cute_turtle.forward(1),
cute_turtle.setheading(180 - cute_turtle.heading())
if abs(cute_turtle.xcor()) > window.window_width() / 2
else None,
cute_turtle.setheading(-cute_turtle.heading())
if abs(cute_turtle.ycor()) > window.window_height() / 2
else None,
)
for cute_turtle, *_ in cute_turtles
],
window.update(),
),
itertools.cycle([None]),
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment