Python+Turtle動(dòng)態(tài)繪制一棵樹實(shí)例分享
本文實(shí)例主要是對(duì)turtle的使用,實(shí)現(xiàn)Python+turtle動(dòng)態(tài)繪制一棵樹的實(shí)例,具體代碼:
# 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)#沿著當(dāng)前的方向畫畫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.speed(10)
# p.getscreen().tracer(1,0)#Return the TurtleScreen object the turtle is drawing on.
p.speed(10)
#TurtleScreen methods can then be called for that object.
p.left(90)# Turn turtle left by angle units. direction 調(diào)整畫筆
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. 這三條語句是一個(gè)組合相當(dāng)于先把筆收起來再移動(dòng)到指定位置,再把筆放下開始畫
#否則turtle一移動(dòng)就會(huì)自動(dòng)的把線畫出來
#t = tree([p], 200, 65, 0.6375)
t = tree([p], 200, 65, 0.6375)
main()
實(shí)現(xiàn)效果:

總結(jié)
以上就是本文關(guān)于Python+Turtle動(dòng)態(tài)繪制一棵樹實(shí)例分享的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
詳解分布式系統(tǒng)中如何用python實(shí)現(xiàn)Paxos
提到分布式算法,就不得不提 Paxos 算法,在過去幾十年里,它基本上是分布式共識(shí)的代 名詞,因?yàn)楫?dāng)前最常用的一批共識(shí)算法都是基于它改進(jìn)的。比如,F(xiàn)ast Paxos 算法、 Cheap Paxos 算法、Raft 算法、ZAB 協(xié)議等等。2021-05-05
對(duì)pyqt5中QTabWidget的相關(guān)操作詳解
今天小編就為大家分享一篇對(duì)pyqt5中QTabWidget的相關(guān)操作詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Python使用re模塊實(shí)現(xiàn)正則表達(dá)式操作指南
在Python中需要通過正則表達(dá)式對(duì)字符串進(jìn)?匹配的時(shí)候,可以使??個(gè)python自帶的模塊,名字為re,下面這篇文章主要給大家介紹了關(guān)于Python使用re模塊實(shí)現(xiàn)正則表達(dá)式操作的相關(guān)資料,需要的朋友可以參考下2022-07-07
使用selenium和pyquery爬取京東商品列表過程解析
這篇文章主要介紹了使用selenium和pyquery爬取京東商品列表過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Python pytest.main()運(yùn)行測(cè)試用例
這篇文章主要介紹了Python pytest.main()運(yùn)行測(cè)試用例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12
pycharm運(yùn)行程序時(shí)看不到任何結(jié)果顯示的解決
今天小編就為大家分享一篇pycharm運(yùn)行程序時(shí)看不到任何結(jié)果顯示的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python算法應(yīng)用實(shí)戰(zhàn)之隊(duì)列詳解
隊(duì)列是一種先進(jìn)先出(First-In-First-Out,F(xiàn)IFO)的數(shù)據(jù)結(jié)構(gòu)。隊(duì)列被用在很多地方,比如提交操作系統(tǒng)執(zhí)行的一系列進(jìn)程、打印任務(wù)池等,一些仿真系統(tǒng)用隊(duì)列來模擬銀行或雜貨店里排隊(duì)的顧客。下面就介紹了Python中隊(duì)列的應(yīng)用實(shí)戰(zhàn),需要的可以參考。2017-02-02

