Spider Web in Python Turtle
Intermediate · Nature · 63 lines · draws in about 7 seconds
Twelve straight spokes come out from the centre, and a spiral thread hops from spoke to spoke, a little further out each time.

How the code works
A leg with a knee
Each leg starts at the centre, walks 30 steps, turns at the knee and walks 34 more. Legs on the right bend with a positive angle and legs on the left with a negative one, so the two sides mirror each other.
def draw_leg(heading, bend): # A leg in two parts with a knee in the middle t.penup() t.goto(0, 0) t.setheading(heading) t.pendown() t.forward(30) t.right(bend) t.forward(34)
The spokes
The turtle goes back to the centre for every spoke and turns to i * 360 / spokes degrees, so 12 spokes are 30 degrees apart.
# The spokes: straight threads from the centre outwards for i in range(spokes): t.penup() t.goto(0, 0) t.setheading(i * 360 / spokes) t.pendown() t.forward(215)
The spiral thread
Each step aims at the next spoke, 2 further out than the step before. math.cos(angle) and math.sin(angle) turn that angle and radius into the x and y for goto(), and spokes * 8 steps go round the web eight times.
# The spiral: hop from spoke to spoke, a little further out each time t.penup() for step in range(spokes * 8): angle = math.radians(step * 360 / spokes) radius = 15 + step * 2 t.goto(radius * math.cos(angle), radius * math.sin(angle)) t.pendown()
The spider
Two loops draw four legs on each side. Dots on top of the legs make the body, the head and two white eyes.
# The spider sits in the middle of its web: four legs on each side t.pensize(4) t.pencolor("black") for heading in [75, 40, 5, -30]: draw_leg(heading, 50) for heading in [105, 140, 175, 210]: draw_leg(heading, -50)
Make it yours
Change the code above and press Run. Some ideas:
- Change
spokes = 12tospokes = 8for a web with fewer, wider gaps. - Change
step * 2tostep * 1.5andspokes * 8tospokes * 11for a tighter spiral with more rings. - Change
screen.bgcolor("lightsteelblue")toscreen.bgcolor("midnightblue")for a web at night.
What it uses
Every turtle command the site supports is listed on Python turtle online.