小白学Python(20)—— Turtle 海龟绘图

 Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。

在1966年,Seymour Papert和Wally Feurzig发明了一种专门给儿童学习编程的语言——LOGO语言,它的特色就是通过编程指挥一个小海龟(turtle)在屏幕上绘图。海龟绘图(Turtle Graphics)后来被移植到各种高级语言中,Python内置了turtle库,基本上100%复制了原始的Turtle Graphics的所有功能。

使用之前需要导入库:

from turtle import *

 画笔运动命令

命令

说明

turtle.forward(distance)

向当前画笔方向移动distance像素长度

turtle.backward(distance)

向当前画笔相反方向移动distance像素长度

turtle.right(degree)

顺时针移动degree°

turtle.left(degree)

逆时针移动degree°

turtle.pendown()

移动时绘制图形,缺省时也为绘制

turtle.goto(x,y)

将画笔移动到坐标为x,y的位置

turtle.penup()

提起笔移动,不绘制图形,用于另起一个地方绘制

turtle.circle()

画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆

setx( )

将当前x轴移动到指定位置

sety( )

将当前y轴移动到指定位置

setheading(angle)

设置当前朝向为angle角度

home()

设置当前画笔位置为原点,朝向东。

dot(r)

绘制一个指定直径和颜色的圆点

 

画笔控制命令

命令

说明

turtle.fillcolor(colorstring)

绘制图形的填充颜色

turtle.color(color1, color2)

同时设置pencolor=color1, fillcolor=color2

turtle.filling()

返回当前是否在填充状态

turtle.begin_fill()

准备开始填充图形

turtle.end_fill()

填充完成

turtle.hideturtle()

隐藏画笔的turtle形状

turtle.showturtle()

显示画笔的turtle形状

 

 

 

如,画一个长方形:

# 导入turtle包的所有内容:
from turtle import *
 
# 设置笔刷宽度:
width(4)
 
# 前进:
forward(200)
# 右转90度:
right(90)
 
# 笔刷颜色:
pencolor('red')
forward(100)
right(90)
 
pencolor('green')
forward(200)
right(90)
 
pencolor('blue')
forward(100)
right(90)
 
# 调用done()使得窗口等待被关闭,否则将立刻关闭窗口:
done()

  

 

更多操作请参考turtle库的说明。https://docs.python.org/3.6/library/frameworks.html

 

五角星: 

1
2
3
4
5
from turtle import *
for i in range(5):
    fd(200)
    rt(144)
done()

  

填充颜色

 

1
2
3
4
5
6
7
8
9
10
11
from turtle import *
width(4)
pencolor("yellow")
fillcolor("red")
 
begin_fill()
for i in range(5):
    fd(200)
    rt(144)
end_fill()
done()

  

太阳花:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from turtle import *
 
color('red', 'yellow')
 
begin_fill()
 
while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
 
end_fill()
 
done()

  

太极图:

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 turtle import *
 
circle(100,180)
circle(200,180)
circle(100,-180)
 
fillcolor('black')
 
begin_fill()
 
circle(100,180)
circle(200,180)
circle(100,-180)
 
end_fill()
 
penup()
 
goto(0,100)
dot(50)
goto(0,-100)
pencolor('white')
dot(50)
 
hideturtle()
 
done()

  

利用循环嵌套方法画图:

1
2
3
4
5
6
7
8
9
10
11
12
from turtle import *
 
for i in range(6):
    pendown()   
    fd(150)
     
    for j in range(10):
        circle(40)
        lt(36)
    lt(60)   
    penup()  <br>    goto(0,0)
done()

  

 

画一个笑脸

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
from turtle import *
def go(x,y):
    penup()
    goto(x,y)
    pendown()
def arc(radius):
    circle(radius,90)
reset()
speed(0)
go(0,-150)
circle(200)
go(50,100)
seth(225)
arc(10)
arc(50)
arc(10)
arc(50)
go(-50,100)
seth(-45)
arc(-10)
arc(-50)
arc(-10)
arc(-50)
go(-70,-50)
arc(100)
hideturtle()

  

 

 绘制一棵分型树:

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
from turtle import *
 
# 设置色彩模式是RGB:
colormode(255)
 
lt(90)
lv = 14
l = 120
s = 45
 
width(lv)
 
# 初始化RGB颜色:
r = 0
g = 0
b = 0
pencolor(r, g, b)
 
penup()
bk(l)
pendown()
fd(l)
 
def draw_tree(l, level):
    global r, g, b
    # save the current pen width
    w = width()
 
    # narrow the pen width
    width(w * 3.0 / 4.0)
    # set color:
    r = r + 1
    g = g + 2
    b = b + 3
    pencolor(r % 200, g % 200, b % 200)
 
    l = 3.0 / 4.0 * l
 
    lt(s)
    fd(l)
 
    if level < lv:
        draw_tree(l, level + 1)
    bk(l)
    rt(2 * s)
    fd(l)
 
    if level < lv:
        draw_tree(l, level + 1)
    bk(l)
    lt(s)
 
    # restore the previous pen width
    width(w)
 
speed("fastest")
 
draw_tree(l, 4)
 
done()

  

 

 螺旋图:

1
2
3
4
5
from turtle import *
for i in range(100):
    fd(2*i)
    lt(90)
done()

  

 

  

1
2
3
4
5
6
7
8
from turtle import *
 
speed(10)
 
for i in range(200):
    fd(2*i)
    lt(92)
done()

 

 

 

posted @   徐海建  阅读(7075)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示