Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzig and Seymour Papert in 1966.

Turtle是Python3的内置模块,位于Lib/turtle.py,所以无需安装,方便实用。

基本步骤 - Basic Steps

引入Turtle模块

1
2
3
4
5
from turtle import *
# or
import turtle
# or
import turtle as t

创建画布(canvas)

  • turtle.screensize(canvwidth=None, canvheight=None, bg=None)

    设置画布的, 高, 背景颜色,默认大小为(400, 300)

  • turtle.setup(width=0.5, height=0.75, startx=None, starty=None)

    • width, height:宽和高为整数时, 表示像素; 为小数时, 表示比例
    • (startx, starty):矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心

使用画笔(Pen)

  • turtle.pensize() :设置画笔的宽度;

  • turtle.pencolor():返回当前画笔颜色或设置画笔颜色

  • turtle.speed(speed):设置画笔移动速度,速度范围[0,10]整数, 数字越大越快

绘图

​ 绘图命令可以划分为3种:运动命令,画笔控制命令和全局控制命令

画笔运动

命令(METHOD) 说明(DESCRIPTION)
turtle.forward(distance) 向当前画笔方向移动distance长度
turtle.backward(distance) 向当前画笔相反方向移动distance长度
turtle.right(degree) 顺时针(clockwise)方向旋转degree°
turtle.left(degree) 逆时针(counter clockwise)方向旋转degree°
turtle.pendown() 下笔(Puts down the turtle’s Pen)
turtle.goto(x,y) 移动画笔到坐标为(x,y)的位置
turtle.penup() 提笔(Picks up the turtle’s Pen)
turtle.speed(speed) 画笔绘制的速度范围[0,10]整数
turtle.circle() 画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆画笔控制

画笔控制

命令(METHOD) 说明(DESCRIPTION)
turtle.pensize(width) 绘制图形时的宽度
turtle.pencolor() 画笔颜色
turtle.fillcolor(colorstring) 绘制图形的填充颜色
turtle.color(color1,color2) 设置pencolor=color1, fillcolor=color2
turtle.filling() 返回当前是否在填充状态
turtle.begin_fill() 开始填充图形
turtle.end_fill() 填充完成
turtle.hideturtle() 隐藏箭头显示
turtle.showturtle() 与hideturtle()函数对应
turtle.shape() 设置画笔形状(‘arrow’, ‘classic’, ‘turtle’ , ‘circle’)
turtle.shapesize(x,y,z) 设置形状的长度、宽度、厚度

全局控制

命令(METHOD) 说明(DESCRIPTION)
turtle.clear() 清空turtle窗口,但是turtle的位置和状态不会改变
turtle.reset() 清空窗口,重置turtle状态为起始状态
turtle.undo() 撤销上一个turtle动作
turtle.isvisible() 返回当前turtle是否可见
turtle.clone() 复制当前图形
turtle.write(s[,font=(“font-name”,font_size,“font_type”)]) 写文本,s为文本内容,font是字体的参数,里面分别为字体名称,大小和类型;font为可选项,

例子 - Examples

自己写的简单例子,供参考:)

正方形 - Square

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
## 引入模块
import turtle as t
import math
import time

## 起笔
t.pendown()
## 睡5秒,以截取gif
time.sleep(5)

## 画正方形
for i in range(4):
t.forward(100)
t.left(90)

## 移动画笔到适当位置
t.forward(50)
t.left(45)

## 画内部正方形
for j in range(4):
t.forward(50*math.sqrt(2))
t.left(90)
## 提笔
t.penup()
## 结束绘画
t.done()

rectangle

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
import turtle as t
import time
import math

WIDTH = 250

## 乌龟
t.shape('turtle')

## 提笔,使得正方形居中
t.penup()
t.goto(-WIDTH/2, -WIDTH/2)

## 起笔
t.pendown()

## 睡5秒,以截取gif
time.sleep(5)

for i in range(5):
## 画正方形
for i in range(4):

## 开始正方形宽度
t.forward(WIDTH)
t.left(90)

## 移动画笔到适当位置
t.forward(WIDTH/2)
t.left(45)

## 内部正方形宽度
WIDTH = WIDTH/2*math.sqrt(2)
## 画内部正方形
for j in range(4):
t.forward(WIDTH)
t.left(90)

t.forward(WIDTH/2)
t.left(45)

## 重新计算后新的正方形宽度
WIDTH = WIDTH/2*math.sqrt(2)

## 提笔
t.penup()
## 结束绘画
t.done()

rectangle_2

十二角星 - Star

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
## 引入模块
import turtle as t
import time
## 设置画笔为红色,填充色为黄色
##== t.pencolor('red') t.fillcolor('yellow')
t.color("red", "yellow")
## 设置速度适中,为5
t.speed(5)
## 开始填充图形
t.begin_fill()
## 睡5秒,以截取gif
time.sleep(5)
## 12 * (180-150) = 360
for _ in range(12):
t.forward(200)
t.left(150)
## 填充完成
t.end_fill()
##== t.done()
t.mainloop()

littlesunflower

心形 - Heart

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
import turtle as t

def drawHeart(WIDTH, x = 0, y = WIDTH/2, fill = False, color = "red", fillcolor = "red", antler = False):

t.pensize(2)
t.pencolor(color)

if antler:
## 开始画角
t.penup()
t.goto(x, y)
t.setheading(90)
t.pendown()
t.circle(WIDTH/2, 120)

t.penup()
t.goto(x, y)
t.setheading(90)
t.pendown()
t.circle(-WIDTH/2, 120)

t.penup()
## 设定起点
t.goto(x, y)

## 设定方向朝北
t.setheading(135)

if fill:
t.begin_fill()
t.color(color, fillcolor)

## half of 爱心 begin
t.pendown()
t.circle(WIDTH/2, 180)
t.forward(WIDTH)
t.penup()
## half of 爱心 done

## 回到起点
t.goto(x, y)
t.setheading(45)
t.pendown()
t.circle(-WIDTH/2, 180)
t.forward(WIDTH)
t.penup()
## half of 另一个爱心 done

if fill:
t.end_fill()

drawHeart(200, antler = True)
drawHeart(100, 0, 50, True)
t.done()

heart

九色花 - Flower

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
## 九色花 from Rainbow Flower
cls = ['red','orange','yellow','green','cyan','blue','purple','khaki','magenta','black']

t.pensize(2)
t.pendown()
t.setheading(90)
len = 1

for petal in range(9):
for half in range(2):
for i in range(60):
if i < 30:
t.pencolor(cls[petal])
len += 0.2
else:
t.pencolor(cls[petal])
len -= 0.2
t.forward(len)
t.left(3)
t.left(40)

t.setheading(-90)
t.pensize(5)
t.pencolor(cls[-1])
t.forward(300)
t.penup()
t.done()

heart

花环 - Wreath

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
import turtle as t
import random

# set a shape and colour
t.shape("circle")

## 设置随机色
def random_color():
R = random.randrange(0,257,10)
G = random.randrange(0,257,10)
B = random.randrange(0,257,10)
t.fillcolor(R,G,B)
t.pencolor(int(R/2),int(G/2),int(B/2))
t.colormode(255)

t.shapesize(5,1,1)
t.penup()
t.goto(0,-100)

## 72*5
for i in range(72):
random_color()
t.forward(10)
t.left(5)
## 只是旋转 不改变画笔方向
t.tilt(15)
## 复制形状
t.stamp()
t.mainloop()

wreath

分形 - Fractal

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
import random
p = turtle.Pen()
p.speed(10)
p.penup()
p.goto(0, -200)
p.setheading(90)
p.pensize(7)
p.pendown()

## 设置随机色
def random_color():
R = random.randrange(0,257,10)
G = random.randrange(0,257,10)
B = random.randrange(0,257,10)
p.fillcolor(R,G,B)
p.pencolor(int(R/2),int(G/2),int(B/2))
turtle.colormode(255)

def branch(plist, len):
if (len > 50):
list = []
# print(plist) 画笔列表
for p in plist:
p.forward(len)
for j in range(6):
p.forward(len/3)
p.back(len/3)
p.right(60)
p.right(60)
list.extend([p])
for k in range(5):
q = p.clone()
q.left(60*(k+1))
list.extend([q])
print(len)
if len*0.55 <=50:
for p in list:
p.forward(len*0.55)
for j in range(6):
p.forward(len*0.55/3)
p.back(len*0.55/3)
p.right(60)
p.right(60)
print("done!")
## 递归,树枝长为55%
branch(list, len * 0.55)

random_color()
## tracer 忽略过程动画, 加速绘制
turtle.tracer(0, 0)
branch([p], 200)
turtle.update()
turtle.mainloop()

snowflakes

REFERENCES