Nanfeng

Notes on software development, code, and curious ideas

Drawing a Rabbit with Python Turtle

Rabbit drawing result

This Turtle example uses ellipses and circles to build a rabbit. Breaking the illustration into reusable primitives makes the code much easier to adjust than a single long sequence of coordinates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import turtle as t

t.setup(800, 700)
t.bgcolor("#fff3df")
t.speed(0)
t.hideturtle()
t.tracer(False)

def move(x, y, heading=0):
t.penup()
t.goto(x, y)
t.setheading(heading)
t.pendown()

def ellipse(x, y, width, height, color):
move(x, y)
t.color(color)
t.begin_fill()
for _ in range(2):
t.circle(height, 90)
t.circle(width, 90)
t.end_fill()

def dot(x, y, size, color):
move(x, y)
t.dot(size, color)

# Body and head
ellipse(-70, -185, 105, 150, "white")
dot(0, 70, 290, "white")

# Long ears with pink inner sections
ellipse(-90, 165, 28, 105, "white")
ellipse(35, 165, 28, 105, "white")
ellipse(-78, 180, 13, 80, "#f3a7b8")
ellipse(47, 180, 13, 80, "#f3a7b8")

# Face
dot(-48, 95, 20, "#2d2730")
dot(48, 95, 20, "#2d2730")
dot(0, 56, 18, "#e98fa6")

move(-35, 42, -25)
t.pencolor("#6d4b56")
t.width(4)
t.circle(40, 50)
move(35, 42, 205)
t.circle(-40, 50)

t.update()
t.done()

Turtle is included with standard desktop Python installations, but it requires a graphical Tk environment. If the window does not open, verify that Tk support is installed and run the script locally rather than in a headless terminal. Adjust the coordinates and colors gradually, keeping t.tracer(False) and one final t.update() for fast rendering.

+