python turtle 可以画图,搞出许多有趣的可视化东西,比如单身狗:
代码如下:
from turtle import *
# setup
# 设置画布大小
screensize(100, 100)
# 设置画笔粗细
pensize(5)
# 设置画笔方向(0~360)
seth(0)
# 设置画笔移动速度(1~10)
speed(2)
# 设置颜色, 参数可以是RGB三元组或字符串
color('black')
fillcolor('black')
# face
# 画笔落下,移动时绘制图形
pd()
# 向前移动距离
fd(25)
# 绘制度数为 90的圆弧
circle(100, 90)
circle(120, 90)
fd(10)
circle(120, 90)
circle(100, 90)
fd(25)
# 画笔抬起,移动时不绘制图形
pu()
# eyes
# 将画笔移动到(x,y)位置
goto(10, 100)
pd()
fd(50)
circle(15, 180)
fd(50)
circle(15, 180)
pu()
goto(60, 100)
# 填充
begin_fill()
circle(17)
end_fill()
goto(-100, 100)
pd()
fd(50)
circle(15, 180)
fd(50)
circle(15, 180)
pu()
goto(-50, 100)
begin_fill()
circle(17)
end_fill()
# mouth
goto(-60, 50)
pd()
seth(340)
circle(30, 100)
seth(180)
fd(20)
seth(280)
circle(30, 120)
pu()
# ears
goto(60, 212)
pd()
seth(60)
circle(-80, 50)
circle(-5, 90)
circle(-150, 35)
pu()
goto(-60, 212)
pd()
seth(110)
circle(90, 40)
seth(240)
circle(90, 60)
pu()
# goodbye
hideturtle()
done()