Editor

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.

Spider Web in Python Turtle: the finished drawing
import turtle
import math
screen = turtle.Screen()
screen.bgcolor("lightsteelblue")
screen.title("Spider web")
t = turtle.Turtle()
t.speed(9)
t.pensize(2)
t.pencolor("white")
spokes = 12
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: 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: 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 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)
# Its body, head and two white eyes
t.penup()
t.goto(0, -14)
t.dot(46, "black")
t.goto(0, 16)
t.dot(30, "black")
t.goto(-6, 20)
t.dot(8, "white")
t.goto(6, 20)
t.dot(8, "white")
t.hideturtle()
turtle.done()

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 = 12 to spokes = 8 for a web with fewer, wider gaps.
  • Change step * 2 to step * 1.5 and spokes * 8 to spokes * 11 for a tighter spiral with more rings.
  • Change screen.bgcolor("lightsteelblue") to screen.bgcolor("midnightblue") for a web at night.

What it uses

Every turtle command the site supports is listed on Python turtle online.