Turtle庫


下列turtle庫的簡單常用指令

forward(distance) #將箭頭移到某一指定坐標 

left(angel)  right(angel) 

penup() #提起筆,用於另起一個地方繪制時用,與pendown()配對使用 

goto(x,y) 

home() 

circle(radius) 

speed() 

 1 #五角星圖形
 2 from turtle import Turtle
 3 
 4 p = Turtle()
 5 p.speed(3)
 6 p.pensize(5)
 7 p.color("black", 'yellow')
 8 #p.fillcolor("red")
 9 p.begin_fill()
10 for i in range(5):
11     p.forward(200)  #將箭頭移到某一指定坐標 
12     p.right(144)    #當前方向上向右轉動角度
13 p.end_fill()

樹的繪制 

觀察:對稱樹, 從主桿出發以一定角度向左向右生成對稱的枝丫, 且每一棵枝杈上以相同的角度生成更小的左右枝杈,如此往復。

    聯系:所學內容,易想到利用遞歸程序實現; 

 

 1 # drawtree.py
 2 
 3 from turtle import Turtle, mainloop
 4 
 5 def tree(plist, l, a, f):
 6     """ plist is list of pens
 7     l is length of branch
 8     a is half of the angle between 2 branches
 9     f is factor by which branch is shortened
10     from level to level."""
11     if l > 5: #
12         lst = []
13         for p in plist:
14             p.forward(l)#沿着當前的方向畫畫Move the turtle forward by the specified distance, in the direction the turtle is headed.
15             q = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties.
16             p.left(a) #Turn turtle left by angle units
17             q.right(a)#Turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.
18             lst.append(p)#將元素增加到列表的最后
19             lst.append(q)
20         tree(lst, l*f, a, f)
21   
22            
23 
24 def main():
25     p = Turtle()
26     p.color("green")
27     p.pensize(5)
28     #p.setundobuffer(None)
29     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,
30                    #because hiding the turtle speeds up the drawing observably.
31     
32     p.getscreen().tracer(10,0)
33         #Return the TurtleScreen object the turtle is drawing on.
34         #TurtleScreen methods can then be called for that object.
35     #p.speed(10)
36     p.left(90)  #Turn turtle left by angle units. direction 調整畫筆
37 
38     p.penup() #Pull the pen up – no drawing when moving.
39     p.goto(0,-200)#Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle’s orientation.
40     p.pendown()# Pull the pen down – drawing when moving. 這三條語句是一個組合相當於先把筆收起來再移動到指定位置,再把筆放下開始畫
41     #否則turtle一移動就會自動的把線畫出來
42 
43     #t = tree([p], 200, 65, 0.6375)
44     t = tree([p], 200, 65, 0.6375)
45     
46 main()

 

森林的繪制 

如何畫出多棵樹,甚至整片森林呢? 

答案很簡單,只要在畫每棵樹之前調整畫筆的位置,調用畫樹程序,就可以從新位置生成一顆新樹了。 

利用模塊化的函數思想,調整代碼: 

將每棵樹的繪制以maketree函數封裝,參數x,y為畫樹的起點位置即樹根位置。在main函數中只要以

 不同的參數設置來調用maketree函數就可以完成多棵樹的繪制了 

 

 1 # drawtree.py
 2 from turtle import Turtle, mainloop
 3 
 4 def tree(plist, l, a, f):
 5     """ plist is list of pens
 6     l is length of branch
 7     a is half of the angle between 2 branches
 8     f is factor by which branch is shortened
 9     from level to level."""
10     if l > 5: #
11         lst = []
12         for p in plist:
13             p.forward(l)#沿着當前的方向畫畫Move the turtle forward by the specified distance, in the direction the turtle is headed.
14             q = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties.
15             p.left(a) #Turn turtle left by angle units
16             q.right(a)#Turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.
17             lst.append(p)#將元素增加到列表的最后
18             lst.append(q)
19         tree(lst, l*f, a, f)
20 
21 def maketree(x, y):
22     p = Turtle()
23     p.color("green")
24     p.pensize(5)
25     p.hideturtle()
26     p.getscreen().tracer(30, 0)
27     p.left(90)
28 
29     p.penup()
30     p.goto(x, y)
31     p.pendown()
32 
33     t = tree([p], 110, 65, 0.6375)
34     print(len(p.getscreen().turtles())) #用了多少個turtle繪制
35 
36 def main():
37     maketree(-200, -200)
38     maketree(0, 0)
39     maketree(200, -200)
40 
41 main()

 

  圖像:

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM