下列turtle库的简单常用指令

forward(distance) #将箭头移到某一指定坐标

left(angel)  right(angel)

penup() #提起笔,用于另起一个地方绘制时用,与pendown()配对使用

goto(x,y)

home()

circle(radius)

speed()

 #五角星图形
from turtle import Turtle p = Turtle()
p.speed(3)
p.pensize(5)
p.color("black", 'yellow')
#p.fillcolor("red")
p.begin_fill()
for i in range(5):
p.forward(200) #将箭头移到某一指定坐标
p.right(144) #当前方向上向右转动角度
p.end_fill()

Turtle库-LMLPHP

树的绘制

Turtle库-LMLPHP

观察:对称树, 从主杆出发以一定角度向左向右生成对称的枝丫, 且每一棵枝杈上以相同的角度生成更小的左右枝杈,如此往复。

    联系:所学内容,易想到利用递归程序实现;

 # drawtree.py

 from turtle import Turtle, mainloop

 def tree(plist, l, a, f):
""" plist is list of pens
l is length of branch
a is half of the angle between 2 branches
f is factor by which branch is shortened
from level to level."""
if l > 5: #
lst = []
for p in plist:
p.forward(l)#沿着当前的方向画画Move the turtle forward by the specified distance, in the direction the turtle is headed.
q = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties.
p.left(a) #Turn turtle left by angle units
q.right(a)#Turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.
lst.append(p)#将元素增加到列表的最后
lst.append(q)
tree(lst, l*f, a, f) def main():
p = Turtle()
p.color("green")
p.pensize(5)
#p.setundobuffer(None)
p.hideturtle() #Make the turtle invisible. It’s a good idea to do this while you’re in the middle of doing some complex drawing,
#because hiding the turtle speeds up the drawing observably. p.getscreen().tracer(10,0)
#Return the TurtleScreen object the turtle is drawing on.
#TurtleScreen methods can then be called for that object.
#p.speed(10)
p.left(90) #Turn turtle left by angle units. direction 调整画笔 p.penup() #Pull the pen up – no drawing when moving.
p.goto(0,-200)#Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle’s orientation.
p.pendown()# Pull the pen down – drawing when moving. 这三条语句是一个组合相当于先把笔收起来再移动到指定位置,再把笔放下开始画
#否则turtle一移动就会自动的把线画出来 #t = tree([p], 200, 65, 0.6375)
t = tree([p], 200, 65, 0.6375) main()

森林的绘制 

如何画出多棵树,甚至整片森林呢?

答案很简单,只要在画每棵树之前调整画笔的位置,调用画树程序,就可以从新位置生成一颗新树了。

利用模块化的函数思想,调整代码:

将每棵树的绘制以maketree函数封装,参数x,y为画树的起点位置即树根位置。在main函数中只要以

 不同的参数设置来调用maketree函数就可以完成多棵树的绘制了

 # drawtree.py
from turtle import Turtle, mainloop def tree(plist, l, a, f):
""" plist is list of pens
l is length of branch
a is half of the angle between 2 branches
f is factor by which branch is shortened
from level to level."""
if l > 5: #
lst = []
for p in plist:
p.forward(l)#沿着当前的方向画画Move the turtle forward by the specified distance, in the direction the turtle is headed.
q = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties.
p.left(a) #Turn turtle left by angle units
q.right(a)#Turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.
lst.append(p)#将元素增加到列表的最后
lst.append(q)
tree(lst, l*f, a, f) def maketree(x, y):
p = Turtle()
p.color("green")
p.pensize(5)
p.hideturtle()
p.getscreen().tracer(30, 0)
p.left(90) p.penup()
p.goto(x, y)
p.pendown() t = tree([p], 110, 65, 0.6375)
print(len(p.getscreen().turtles())) #用了多少个turtle绘制 def main():
maketree(-200, -200)
maketree(0, 0)
maketree(200, -200) main()

  图像:

    Turtle库-LMLPHP

05-11 11:38