Editor

Python Turtle Butterfly

Intermediate · Nature · 70 lines · draws in about 9 seconds

One function draws a teardrop-shaped wing at any angle and size, and four calls to it make the two big and two small wings.

Python Turtle Butterfly: the finished drawing
import turtle
screen = turtle.Screen()
screen.bgcolor("lightcyan")
screen.title("Butterfly")
t = turtle.Turtle()
t.speed(6)
t.pensize(5)
t.pencolor("black")
body_x = 0
body_y = -20
def draw_wing(heading, size, color):
# A teardrop pointing at the body: a straight edge out,
# two thirds of a circle round the end, and a straight edge back.
# steps=40 makes the round end smooth.
t.penup()
t.goto(body_x, body_y)
t.setheading(heading - 30)
t.pendown()
t.fillcolor(color)
t.begin_fill()
t.forward(size * 1.73)
t.circle(size, 240, steps=40)
t.forward(size * 1.73)
t.end_fill()
# A spot in the round part of the wing
t.penup()
t.setheading(heading)
t.forward(size * 2)
t.dot(size, "gold")
t.dot(size / 3, "white")
def draw_antenna(heading, bend):
# A thin curved line from the head with a dot at the tip
t.penup()
t.goto(body_x, body_y + 80)
t.setheading(heading)
t.pendown()
t.circle(bend, 50)
t.dot(12)
# Big wings on top, small wings below: the left ones are a mirror image
draw_wing(40, 80, "darkorange")
draw_wing(140, 80, "darkorange")
draw_wing(-40, 60, "orangered")
draw_wing(220, 60, "orangered")
# The body: a very thick line with round ends, and a head
t.penup()
t.goto(body_x, body_y - 80)
t.pendown()
t.pensize(20)
t.pencolor("#3b2a20")
t.goto(body_x, body_y + 70)
t.dot(34)
# Two antennae that curve outwards
t.pensize(4)
draw_antenna(80, -110)
draw_antenna(100, 110)
t.hideturtle()
turtle.done()

How the code works

A teardrop wing

The turtle starts at the body facing 30 degrees to one side of heading, walks out, goes 240 degrees round a circle and walks back. With these numbers the straight edges meet the circle smoothly, so the only corner is the point at the body. forward(size * 2) along heading then reaches the middle of the round part for the spot.

def draw_wing(heading, size, color):
    # A teardrop pointing at the body: a straight edge out,
    # two thirds of a circle round the end, and a straight edge back.
    # steps=40 makes the round end smooth.
    t.penup()
    t.goto(body_x, body_y)
    t.setheading(heading - 30)
    t.pendown()
    t.fillcolor(color)
    t.begin_fill()
    t.forward(size * 1.73)
    t.circle(size, 240, steps=40)
    t.forward(size * 1.73)
    t.end_fill()

    # A spot in the round part of the wing
    t.penup()
    t.setheading(heading)
    t.forward(size * 2)
    t.dot(size, "gold")
    t.dot(size / 3, "white")

Curved antennae

circle(bend, 50) draws a short curve. A negative bend curves to the right and a positive one to the left, so the two antennae lean away from each other.

def draw_antenna(heading, bend):
    # A thin curved line from the head with a dot at the tip
    t.penup()
    t.goto(body_x, body_y + 80)
    t.setheading(heading)
    t.pendown()
    t.circle(bend, 50)
    t.dot(12)

Four wings

Headings 40 and 140 are mirror images across the body, and so are -40 and 220. The top wings have a size of 80 and the bottom wings 60.

# Big wings on top, small wings below: the left ones are a mirror image
draw_wing(40, 80, "darkorange")
draw_wing(140, 80, "darkorange")
draw_wing(-40, 60, "orangered")
draw_wing(220, 60, "orangered")

The body

A line drawn with pensize(20) has round ends, so one goto() makes the whole body. dot(34) with no colour uses the pen colour, which makes the head match.

# The body: a very thick line with round ends, and a head
t.penup()
t.goto(body_x, body_y - 80)
t.pendown()
t.pensize(20)
t.pencolor("#3b2a20")
t.goto(body_x, body_y + 70)
t.dot(34)

Make it yours

Change the code above and press Run. Some ideas:

  • Change "darkorange" to "deepskyblue" and "orangered" to "royalblue" for a blue butterfly.
  • Make the top wings bigger with draw_wing(40, 95, "darkorange") and draw_wing(140, 95, "darkorange").
  • Change body_y = -20 to body_y = 0 to move the whole butterfly up, because every part is placed from the body.

What it uses

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