Rush! Turtle of Logo! 中文版

I am so excited that Python involved module turtle since version 2.5. Once seen it I am grabbed back to the golden time of my childhood. In this blog, I will try to demo how to use the turtle to play tangram.

What is the turtle?

So what does turtle mean here? Exactly the turtle is the figure of cursor in Logo, which is a classic education programming language as an initiation of computer programming for children. Using simple directives we can lead the turtle to draw kinds of graphics. Because this WYSIWYG way the turtle may be a best-case of teaching through lively activities.

Competition of Computer Application

It was great luck for me to attend the 3rd National Children Competition of Computer Application stands for Wuxi in 1999. There were two events for me, the first was making papers using Microsoft Paint shipped in Windows 95, the second was using Logo to play tangram in finite time, the more the better.

At the time of 486, these computer competitions were held to answer the call of
Xiaoping Deng to initiate the education of computers from kids. What the most impressed me is that except the events I participated in, event as operating robots to go out of mazes were also included. It is amazon even now!

After the game, we visited the computer museum in Shanghai which exhibited 8086 series chips. I only knew these chips are brains of computers at that age, but how it works? Whatever that trip and question opened the main theme of my life in the future.

Group Photo

Review the Classic Turtle

To pay honor to Logo, I will mock the environment of Logo programming when coding in turtle of Python3. This means the following choices:

  • Use procedure programming pattern instead of OOP
  • Following logo mode to code degrees:
    • 0 means north
    • 90 means east
    • 180 means south
    • 270 means west

Follow my Order

To move the turtle, the first step is learning basic motion and redirect directives:

  • Forward
    turtle.forward(distance) or turtle.fd(distance)
  • Backword
    turtle.backward(distance) or turtle.back(distance)
  • Turn left
    turtle.left(degree)
  • Turn right
    turtle.right(degree)
  • Of cause you can set direction of turtle in directly
    turtle.setheading(degree)

We can draw a basic square using these basic directives:

1
2
3
4
5
6
7
8
9
10
11
12
13
import turtle

# Initiation
turtle.mode('logo') # Set mode
turtle.home() # Move turtle to Origin Point(0, 0)

for i in range(4):
turtle.forward(100)
turtle.right(90)

if __name__=='__main__':
turtle.exitonclick() # To keep the canvas from closed

Basic Square

Make it Colorful

It is boring without color. Turtle offers two directives to control color.

  • Set pen color
    turtle.pencolor(color_string)
  • Set filling color
    turtle.fillcolor(color_string)
  • Also you can set them at once
    turtle.color(pencolor, fillcolor)

If want to fill a shape with one specified color, we should wrap our code with function calls begin_fill and end_fill. The following code demos how to draw a red triangle:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import turtle

# Initiation
turtle.mode('logo') # Set mode
turtle.home() # Move turtle to Origin Point(0, 0)
turtle.color('red', 'red') # Set pen color and fill color to red both
turtle.setheading(30) # Set direction to 30 degree

turtle.begin_fill()
for i in range(3):
turtle.forward(100)
turtle.right(120)
turtle.end_fill()

if __name__=='__main__':
turtle.exitonclick() # To keep the canvas from closed

Red Triangle

Procedural Programming

Procedural programming is a pattern to organize codes into procedures for reuse. When we play tangrams in Logo, we can define a specific procedure for each shape like the following codes:

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'''
filename: tangrams.py
Define procedures for each tangram.
'''
import turtle

turtle.mode('logo')

# Set unit side length
SMALL_LENGTH = 100
MIDDLE_LENGTH = round(SMALL_LENGTH * (2 ** 0.5), 2)
LARGE_LENGTH = SMALL_LENGTH * 2
LARGEST_LENGTH = round(2 * SMALL_LENGTH * (2 ** 0.5), 2)

# Set default color value
COLOR_LARGE_TRIANGLE_1 = '#99FF00' #green
COLOR_LARGE_TRIANGLE_2 = '#FF3300' #orange
COLOR_MIDDLE_TRIANGLE = '#0033FF' #blue
COLOR_SMALL_TRIANGLE_1 = '#FF66FF' #pink
COLOR_SMALL_TRIANGLE_2 = '#6600CC' #purple
COLOR_SQUARE = '#FFFF33' #yellow
COLOR_TRAPEZOID = '#CCCCCC' #gray

# draw basic triangle
def base_triangle(side_length, color):
turtle.color(color, color)
turtle.begin_fill()
turtle.fd(side_length)
turtle.right(135)
turtle.fd(round(side_length * (2 ** 0.5), 2))
turtle.right(135)
turtle.fd(side_length)
turtle.right(90)
turtle.end_fill()

# draw basic square
def base_square(side_length, color):
turtle.color(color, color)
turtle.begin_fill()

for i in range(4):
turtle.fd(side_length)
turtle.right(90)
turtle.end_fill()

# draw basic trapezoid
def base_trapezoid(side_length, color, direction='left'):
turtle.color(color, color)
turtle.begin_fill()

turtle.fd(round(side_length * (2 ** 0.5), 2))
if direction=='left':
turtle.left(45)
else:
turtle.right(45)
turtle.fd(side_length)
if direction=='left':
turtle.left(135)
else:
turtle.right(135)
turtle.fd(round(side_length * (2 ** 0.5), 2))
if direction=='left':
turtle.left(45)
else:
turtle.right(45)
turtle.fd(side_length)
if direction=='left':
turtle.left(135)
else:
turtle.right(135)
turtle.end_fill()

# move with pen up, then pen down
def move(distance):
turtle.penup()
turtle.fd(distance)
turtle.pendown()

# shorts for turn right
def right(degree):
turtle.right(degree)

# shorts for turn left
def left(degree):
turtle.left(degree)

# shorts for show graphic
def end():
turtle.hideturtle()
turtle.exitonclick()

# draw large triangle 1
def large_triangle_1():
base_triangle(LARGE_LENGTH, COLOR_LARGE_TRIANGLE_1)

# draw large triangle 2
def large_triangle_2():
base_triangle(LARGE_LENGTH, COLOR_LARGE_TRIANGLE_2)

# draw small triangle 1
def small_triangle_1():
base_triangle(SMALL_LENGTH, COLOR_SMALL_TRIANGLE_1)

# draw small triangle 2
def small_triangle_2():
base_triangle(SMALL_LENGTH, COLOR_SMALL_TRIANGLE_2)

# draw middle triangle
def middle_triangle():
base_triangle(MIDDLE_LENGTH, COLOR_MIDDLE_TRIANGLE)

# draw the square
def square():
base_square(SMALL_LENGTH, COLOR_SQUARE)

# draw the trapezoid
def trapezoid(direction):
base_trapezoid(SMALL_LENGTH, COLOR_TRAPEZOID, direction)

if __name__=='__main__':
left(45)
square()
right(90)
large_triangle_1()
right(90)
large_triangle_2()
right(90)
small_triangle_1()
move(SMALL_LENGTH*2)
right(135)
trapezoid('right')
move(LARGEST_LENGTH)
right(90)
middle_triangle()
move(MIDDLE_LENGTH)
right(45)
move(SMALL_LENGTH)
right(180)
small_triangle_2()
end()

We get the following classic tangram after running the codes above:
Tangram example

Let’s Play Tangram Together

After import the module trangrams.py above, you can draw any tangram patterns yourself. For example, the following code uses it to draw the digit 1:

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
import turtle
from tangrams import *

left(45)
square()

move(SMALL_LENGTH)
right(90)
move(SMALL_LENGTH)
small_triangle_1()

right(90)
move(SMALL_LENGTH)
left(135)
trapezoid('right')

left(135)
move(SMALL_LENGTH)
right(180)
large_triangle_1()

right(90)
move(LARGE_LENGTH)
right(90)
large_triangle_2()

move(SMALL_LENGTH)
right(180)
small_triangle_2()

right(90)
middle_triangle()

end()

Tangram example - Number 1

Then please draw the other digits if interested.
Tangram Homework

At the Bottom

I figure that Logo successfully catches children’s eyes for drawing is attractive in nature. It makes children familiar with concepts of the coordinate system, degree, direction and so on.

Although time flies, Today I still appreciate the farseeing educators who held the competitions. The events at that time imply how we use computational ability in modern life.

  • Expand our sense
    All modern info carriers (web pages, digital music/video, even game e.g.) expands our sense system to improve our efficiency of getting information.
  • Model our life
    The general computer run codes in memory to model human activities. Information processing speed up for more and more processes are implemented online.

More and more info we get, faster and faster we processed. The singularity is near.

Reference

  1. Programming Language Logo
  2. Tangram
  3. Procedural Programming
  4. 24.1. turtle — Turtle graphics — Python 3.6.3rc1 documentation