Turtle图形库是教小孩子学习编程的一种流行的方式,是1966年Wally Feurzig和Seymour Paper开发的原始图标编程语言的一部分。
设想在一个x-y的二维平面的原点(x=0,y=0)上有一个机器小乌龟,通过import turtle引入该包,向小乌龟发出命令turtle.forward(15),小乌龟便沿着当前的朝向移动15像素,并画出一条直线。发出指令turtle.right(25),则顺时针旋转25度。同理指令turtle.left(25)是逆时针旋转25度,turtle.forward(-15)则是反方向移动15像素。
通过把这些类似的命令组合在一起,可以很容易的画出错综复杂的形状和图形。
画五角星例子,从原点出发沿x轴向右移动200像素,然后逆时针170度,再移动200像素,如此循环。函数turtle.pos()可以获得机器小乌龟的当前坐标(x,y),而方法abs(turtle.pos())可以获得机器小乌龟当前距离原点的直线距离(根据勾股定理计算)。经过若干次循环之后,机器小乌龟将再次经过原点(x=0,y=0),此时直线距离小于1,将跳出循环,程序执行结束。
import turtle
while True:
turtle.forward(200)
turtle.left(170)
if abs(turtle.pos()) < 1 :
break
为了好看,可以设置画线的颜色和背景,使用函数turtle.color(‘red’,’yellow’),画线的线条颜色是红色,背景填充色是黄色。使用函数turtle.begin_fille()和turtle.end_fille()实现背景填充。完整程序如下:
import turtle
turtle.color('red','yellow')
turtle.begin_fill()
while True:
turtle.forward(200)
turtle.left(170)
if abs(turtle.pos()) < 1 :
break
turtle.end_fill()
另外一个例子,画彩色螺纹图形,使用了turtle包里的Pen()方法获得一个画笔对象(注意Pen首字母大写),可以设置画笔颜色pencolor(),画笔线条粗细width(),画笔的移动forward(),逆时针转动角度left(),顺时针角度right(),执行速度speed(),速度由0-10整数指定,0-最快,1-最慢,3-比较慢,6-正常,10-比较快。完整程序如下:
import turtle
colors = ['red','purple','blue','green','yellow','orange']
t = turtle.Pen()
turtle.bgcolor('black')
for x in range(360):
t.speed(0)
t.pencolor(colors[x%6])
t.width(x/100+1)
t.forward(x)
t.left(59)
这里可以改变逆时针转动的角度,形成不同的图形。
python画图turtle模块介绍之(二)
移动和画线
Turtlemotion
Move and draw
程序举例如下:
import turtle
#设置画线速度
turtle.speed(1)
#向前移动50像素
turtle.forward(50)
#向前移动50像素
turtle.fd(50)
#向后移动10像素
turtle.forward(-10)
#向后移动30像素
turtle.fd(-30)
#向后移动10像素
turtle.back(10)
#向后移动10像素
turtle.bk(10)
#机器小乌龟定位到x=100,y=100
turtle.goto(100,100)
#机器小乌龟定位到x=0,y=0
turtle.setpos(0,0)
#机器小乌龟定位到x=-10,y=-10
turtle.setposition(-10,-10)
#机器小乌龟定位到x=200, y不变
turtle.setx(200)
#机器小乌龟定位到y=-50, x不变
turtle.sety(-50)
#机器小乌龟箭头方向调整,0-向右,90-向上,180-向左,270-向下
turtle.setheading(0)
#机器小乌龟箭头方向调整,0-向右,90-向上,180-向左,270-向下
turtle.seth(90)
#回到原点(x=0,y=0)
turtle.home()
#画圆(逆时针),半径10像素
turtle.circle(10)
#画270度的圆(逆时针),半径20像素
turtle.circle(20,270)
#画90度的圆(顺时针),半径30像素
turtle.circle(-30,90)
#画360度的圆(顺时针),半径100像素,指定该圆由1000个内接多边形近似而成
turtle.circle(-100,360,1000)
#画点,直径10像素,填充颜色红色
turtle.dot(10,'red')
#获得当前位置
turtle.pos()
#设置画线颜色,蓝色
turtle.color('blue')
#取消上一步操作
turtle.undo()
获取状态
TellTurtle’s state
举例如下:
import turtle
#获得当前位置坐标
turtle.pos()
turtle.position()
#设置位置坐标
turtle.goto(3,5)
#获得角度
turtle.towards(10,120)
#获得x坐标
turtle.xcor()
#获得y坐标
turtle.ycor()
#逆时针旋转67度
turtle.left(67)
#获得箭头得角度
x = turtle.heading()
print(x)
#设置位置坐标
turtle.setposition(0,0)
#获得距离
y = turtle.distance(30,40)
print(y)
Setting and measurement
程序举例如下:
import turtle
#设置一圈的角度计算单位,默认一圈是360度。
turtle.degrees(180)
turtle.left(45)
print(turtle.heading())
#把角度转换为弧度
turtle.radians()
print(turtle.heading())
画笔控制
Pen control
Drawing state
举例如下:
Import turtle
#画笔控制
#画笔落下,移动时会画线
turtle.pendown()
turtle.pd()
turtle.down()
#向前移动50像素
turtle.fd(50)
#画笔抬起,移动时不画线
turtle.penup()
turtle.pu()
turtle.up()
#向前移动50像素
turtle.fd(50)
#顺时针旋转30度
turtle.right(30)
#画笔落下,画线
turtle.pendown()
turtle.fd(50)
#画笔线条粗细
turtle.pensize(2)
turtle.fd(50)
#显示画笔属性
print(turtle.pen().items())
#设置画笔属性
turtle.pen(pencolor='red',pensize='2')
turtle.fd(20)
#获得画笔是否落下
print(turtle.isdown())
颜色控制
Color control
Filling
More drawing control
举例如下:
#获得画笔颜色
print(turtle.pencolor())
#设置画笔颜色
turtle.pencolor('red')
turtle.fd(20)
turtle.pencolor('#32c18f')
turtle.fd(20)
#设置颜色模式。1或者255
turtle.colormode(1)
turtle.pencolor((0.2,0.8,0.54))
turtle.fd(10)
#设置颜色模式。1或者255
turtle.colormode(255)
turtle.pencolor((10,100,200))
turtle.left(30)
turtle.fd(10)
#设置填充颜色
turtle.fillcolor('violet')
turtle.fd(10)
turtle.fillcolor('#8856a0')
turtle.fd(10)
turtle.colormode(255)
turtle.fillcolor((10,25,90))
turtle.fd(10)
#设置画笔颜色以及填充颜色
turtle.color('red','yellow')
turtle.fd(50)
turtle.colormode(255)
turtle.color((100,20,30),(90,240,125))
#填充
turtle.begin_fill()
turtle.end_fill()
#判断是否填充
turtle.filling()
#删除屏幕上所画的图形,机器小乌龟回到原点(x=0,y=0)
turtle.reset()
#删除屏幕上所画的图形,机器小乌龟不动
turtle.fd(100)
turtle.clear()
#往屏幕上写字,画笔移动(Ture),
turtle.write('hello world',True,align='right',('宋体','20','宋'))