利用Python畫一顆神奇的樹!
阿新 • • 發佈:2020-08-13
turtle是一隻神奇的小海龜,可以畫出大千世界。而turtle加入了python大家族,像是如龜得水,變得更加受歡迎。
很多人學習python,不知道從何學起。
很多人學習python,掌握了基本語法過後,不知道在哪裡尋找案例上手。
很多已經做案例的人,卻不知道如何去學習更加高深的知識。
那麼針對這三類人,我給大家提供一個好的學習平臺,免費領取視訊教程,電子書籍,以及課程的原始碼!
QQ群:101677771
古人有詩云“庭中有奇樹,綠葉發華滋”,樹之美,或婀娜、或繁茂、或蒼勁、或青翠。python-turtle筆下的樹更是別有一番風味。
小園新種紅櫻樹,閒繞花行便當遊
#coding=gbk
importturtleasT
importrandom
importtime
#畫櫻花的軀幹(60,t)
defTree(branch,t):
time.sleep(0.0005)
ifbranch>3:
if8<=branch<=12:
ifrandom.randint(0,2)==0:
t.color('snow')#白
else:
t.color('lightcoral')#淡珊瑚色
t.pensize(branch/3)
elifbranch<8:
ifrandom.randint(0,1)==0:
t.color('snow')
else:
t.color('lightcoral')#淡珊瑚色
t.pensize(branch/2)
else:
t.color('sienna')#赭(zhě)色
t.pensize(branch/10)#6
t.forward(branch)
a=1.5*random.random()
t.right(20*a)
b=1.5*random.random()
Tree(branch-10*b,t)
t.left(40*a)
Tree(branch-10*b,t)
t.right(20*a)
t.up()
t.backward(branch)
t.down()
#掉落的花瓣
defPetal(m,t):
foriinrange(m):
a=200-400*random.random()
b=10-20*random.random()
t.up()
t.forward(b)
t.left(90)
t.forward(a)
t.down()
t.color('lightcoral')#淡珊瑚色
t.circle(1)
t.up()
t.backward(a)
t.right(90)
t.backward(b)
#繪圖區域
t=T.Turtle()
#畫布大小
w=T.Screen()
#隱藏畫筆
t.hideturtle()
t.getscreen().tracer(5,0)
#wheat小麥
w.screensize(bg='white')
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')
#畫櫻花的軀幹
Tree(60,t)
#掉落的花瓣
Petal(200,t)
w.exitonclick()
江南有丹橘,經冬猶綠林
#coding=gbk
fromturtleimport*
fromrandomimport*
frommathimport*
classTree:
def__init__(self):
setup(1000,500)
bgcolor(1,1,1)#背景色
#ht()#隱藏turtle
speed(10)#速度1-10漸進,0最快
#tracer(1,100)#設定繪圖螢幕重新整理頻率,引數1設定在正常重新整理頻次的第引數1次重新整理,引數2設定每次重新整理的時延
tracer(0,0)
pu()#抬筆
backward(100)
#保證筆觸箭頭方向始終不向下,此處使其左轉90度,而不是右轉
left(90)#左轉90度
backward(300)#後退300
deftree(self,n,l):
pd()#下筆
#陰影效果
t=cos(radians(heading()+45))/8+0.25
pencolor(t,t,t)
pensize(n/1.2)
forward(l)#畫樹枝
ifn>0:
b=random()*15+10#右分支偏轉角度
c=random()*15+10#左分支偏轉角度
d=l*(random()*0.25+0.7)#下一個分支的長度
#右轉一定角度,畫右分支
right(b)
self.tree(n-1,d)
#左轉一定角度,畫左分支
left(b+c)
self.tree(n-1,d)
#轉回來
right(c)
else:
#畫葉子
right(90)
n=cos(radians(heading()-45))/4+0.5
pencolor(n,n*0.8,n*0.8)
fillcolor(n,n*0.8,n*0.8)
begin_fill()
circle(3)
left(90)
end_fill()
#新增0.3倍的飄落葉子
ifrandom()>0.7:
pu()
#飄落
t=heading()
an=-40+random()*40
setheading(an)
dis=int(800*random()*0.5+400*random()*0.3+200*random()*0.2)
forward(dis)
setheading(t)
#畫葉子
pd()
right(90)
n=cos(radians(heading()-45))/4+0.5
pencolor(n*0.5+0.5,0.4+n*0.4,0.4+n*0.4)
fillcolor(n,n*0.8,n*0.8)
begin_fill()
circle(2)
left(90)
end_fill()
pu()
#返回
t=heading()
setheading(an)
backward(dis)
setheading(t)
#pass
pu()
backward(l)#退回
defmain():
tree=Tree()
tree.tree(12,100)#遞迴7層
done()
if__name__=='__main__':
main()
落紅不是無情物,化作春泥更護花
#coding=gbk
fromturtleimport*
fromrandomimport*
#畫樹方法
defdrawTree(n,l):
pendown()
pencolor('#5d3c3c')
pensize(n/1.5)
forward(l)
ifn>0:
dr=randint(30,40)
dl=randint(30,40)
move=l*(random()*0.4+0.5)
right(dr)
drawTree(n-1,move)
left(dr+dl)
drawTree(n-1,move)
right(dl)
else:
drawPetal(3)
penup()
backward(l)
#花瓣位置生成
defpetalPlace(m,x,y):
penup()
goto(x,y)
pendown()
setheading(0)
tracer(False)
foriinrange(m):
ifi==0:
drawPetal(5)
else:
penup()
goto(x,y)
a=randint(20,400)
b=randint(-50,50)
forward(a)
left(90)
forward(b)
right(90)
pendown()
drawPetal(5)
#花朵繪畫方法
defdrawPetal(n):
colormode(255)
r=randint(200,255)
g=randint(8,158)
b=randint(8,158)
begin_fill()
fillcolor(r,g,b)
pencolor(r,g,b)
circle(n)
end_fill()
#啟動方法
defrun():
setup(1.0,1.0)
penup()
goto(-50,-150)
left(90)
pendown()
hideturtle()
tracer(False)
drawTree(13,150)
petalPlace(160,-100,-150)
run()
done()
川原秋色靜,蘆葦晚風鳴
importturtle
importrandom
stack=[]
defcreateWord(max_it,word,proc_rules,x,y,turn):
turtle.up()
turtle.home()
turtle.goto(x,y)
turtle.right(turn)
turtle.down()
t=0
whilet<max_it:
word=rewrite(word,proc_rules)
drawit(word,5,20)
t=t+1
defrewrite(word,proc_rules):
wordList=list(word)
foriinrange(len(wordList)):
curChar=wordList[i]
ifcurCharinproc_rules:
wordList[i]=proc_rules[curChar]
return"".join(wordList)
defdrawit(newWord,d,angle):
newWordLs=list(newWord)
foriinrange(len(newWordLs)):
cur_Char=newWordLs[i]
ifcur_Char=='F':
turtle.forward(d)
elifcur_Char=='+':
turtle.right(angle)
elifcur_Char=='-':
turtle.left(angle)
elifcur_Char=='[':
state_push()
elifcur_Char==']':
state_pop()
defstate_push():
globalstack
stack.append((turtle.position(),turtle.heading()))
defstate_pop():
globalstack
position,heading=stack.pop()
turtle.up()
turtle.goto(position)
turtle.setheading(heading)
turtle.down()
defrandomStart():
x=random.randint(-300,300)
y=random.randint(-320,-280)
heading=random.randint(-100,-80)
return((x,y),heading)
defmain():
rule_sets=[]
rule_sets.append(((3,5),'F',{'F':'F[+F][-F]F'}))
rule_sets.append(((4,6),'B',{'B':'F[-B][+B]','F':'FF'}))
rule_sets.append(((2,4),'F',{'F':'FF+[+F-F-F]-[-F+F+F]'}))
tree_count=50
turtle.tracer(10,0)
forxinrange(tree_count):
rand_i=random.randint(0,len(rule_sets)-1)
selected_ruleset=rule_sets[rand_i]
i_range,word,rule=selected_ruleset
low,high=i_range
i=random.randint(low,high)
start_position,start_heading=randomStart()
start_x,start_y=start_position
createWord(i,word,rule,start_x,start_y,start_heading)
if__name__=='__main__':main()
上面程式碼都可以直接執行,試試吧!