Python趣味挑戰(zhàn)之turtle庫(kù)繪畫(huà)飄落的銀杏樹(shù)
import turtle
import random
from math import *
二、生成斐波那契數(shù)列斐波那契數(shù)列是指前兩項(xiàng)的和加起來(lái)等于后一項(xiàng)的一個(gè)數(shù)列,這里使用了兩個(gè)函數(shù)來(lái)生成斐波契那數(shù)列。
def Fibonacci_Recursion_tool(n): #斐波那契數(shù)列方法 if n <= 0:return 0 elif n == 1:return 1 else:return Fibonacci_Recursion_tool(n - 1) + Fibonacci_Recursion_tool(n - 2)def Fibonacci_Recursion(n): #生成斐波那契數(shù)列,并存入列表 result_list = [] for i in range(1, n + 3):result_list.append(Fibonacci_Recursion_tool(i)) return result_list
調(diào)用函數(shù)生成一個(gè)數(shù)列如下:
yu = Fibonacci_Recursion(top) #生成斐波契那數(shù)列print(yu)
運(yùn)行結(jié)果如下:
def leaf(x, y, node):#定義畫(huà)葉子的方法 til = turtle.heading() i = random.random() an = random.randint(10, 180) ye = random.randint(6, 9)/10 turtle.color(ye, ye*0.9, 0) turtle.fillcolor(ye+0.1, ye+0.05, 0) turtle.pensize(1) turtle.pendown() turtle.setheading(an + 90) turtle.forward(8*i) px = turtle.xcor() py = turtle.ycor() turtle.begin_fill() turtle.circle(7.5*i, 120) # 畫(huà)一段120度的弧線 turtle.penup() # 抬起筆來(lái) turtle.goto(px, py) # 回到圓點(diǎn)位置 turtle.setheading(an + 90) # 向上畫(huà) turtle.pendown() # 落筆,開(kāi)始畫(huà) turtle.circle(-7.5*i, 120) # 畫(huà)一段120度的弧線 turtle.setheading(an + 100) turtle.circle(10.5*i, 150) turtle.end_fill() # 畫(huà)一段150度的弧線 turtle.penup() turtle.goto(x, y) turtle.setheading(til) turtle.pensize(node / 2 + 1)四、定義生成樹(shù)的方法
這里用x生成隨機(jī)數(shù),用if條件進(jìn)行判斷來(lái)決定要不要繼續(xù)畫(huà)分支,要不要畫(huà)葉子,使樹(shù)更加自然,無(wú)規(guī)律性,更好看一點(diǎn),這樣會(huì)導(dǎo)致你每次運(yùn)行時(shí),畫(huà)出來(lái)的樹(shù)都是不一樣的。具體的細(xì)節(jié),我已經(jīng)加上了注釋。如果想調(diào)整空中葉子的比例,樹(shù)的分叉程度,修改if判斷語(yǔ)句中的x取值范圍,以增加概率或減小概率即可。至于如何達(dá)到你心中完美的效果就要慢慢去嘗試了。
def draw(node, length, level, yu, button): #定義畫(huà)樹(shù)的方法 turtle.pendown() t = cos(radians(turtle.heading()+5)) / 8 + 0.25 turtle.pencolor(t*1.6, t*1.2, t*1.4) #(r, g, b)顏色對(duì)應(yīng)的RGB值 turtle.pensize(node/1.2) #畫(huà)筆的尺寸 x = random.randint(0, 10) #生成隨機(jī)數(shù)決定要畫(huà)樹(shù)枝還是畫(huà)飄落的葉子 if level == top and x > 6: #此時(shí)畫(huà)飄落的葉子,x范圍太大會(huì)導(dǎo)致樹(shù)太禿turtle.forward(length) # 畫(huà)樹(shù)枝yu[level] = yu[level] - 1c = random.randint(2, 10)for i in range(1, c): leaf(turtle.xcor(), turtle.ycor(), node) # 添加0.3倍的飄落葉子 if random.random() > 0.3:turtle.penup() # 飄落t1 = turtle.heading()an1 = -40 + random.random() * 40turtle.setheading(an1)dis = int(800 * random.random() * 0.5 + 400 * random.random() * 0.3 + 200 * random.random() * 0.2)turtle.forward(dis)turtle.setheading(t1)turtle.right(90) # 畫(huà)葉子leaf(turtle.xcor(), turtle.ycor(), node)turtle.left(90) # 返回t2 = turtle.heading()turtle.setheading(an1)turtle.backward(dis)turtle.setheading(t2) elif level==top and x < 7 : #此時(shí)畫(huà)枝葉,x范圍太大會(huì)導(dǎo)致飄落的葉子太少turtle.penup()turtle.forward(length) elif level>3 and (x>6) :#三級(jí)樹(shù)枝以上,有40%的概率執(zhí)行以下策略turtle.pendown()turtle.forward(length)c = random.randint(4, 6)for i in range(3, c): leaf(turtle.xcor(), turtle.ycor(),node)leaf(turtle.xcor(), turtle.ycor(),node)button=1# jump''' else:turtle.forward(length) # 畫(huà)樹(shù)枝yu[level] = yu[level] -1 if node > 0 and button == 0:# 計(jì)算右側(cè)分支偏轉(zhuǎn)角度,在固定角度偏轉(zhuǎn)增加一個(gè)隨機(jī)的偏移量right = random.random() * 5 + 17# 計(jì)算左側(cè)分支偏轉(zhuǎn)角度,在固定角度偏轉(zhuǎn)增加一個(gè)隨機(jī)的偏移量left = random.random() * 20 + 19# 計(jì)算下一級(jí)分支的長(zhǎng)度child_length = length * (random.random() * 0.25 + 0.7)# 右轉(zhuǎn)一定角度,畫(huà)右分支r=random.randint(0, 1)if r==1: turtle.right(right) level = level + 1 #print('level', level)else: turtle.left(right) level = level + 1 #print('level', level)draw(node - 1, child_length,level,yu,button)yu[level] = yu[level] +1if yu[level] > 1: # 左轉(zhuǎn)一定角度,畫(huà)左分支 if r==1: turtle.left(right + left) draw(node - 1, child_length, level, yu,button) # 將偏轉(zhuǎn)的角度,轉(zhuǎn)回 turtle.right(left) yu[level] = yu[level] - 1 else:turtle.right(right + left)draw(node - 1, child_length, level, yu,button)# 將偏轉(zhuǎn)的角度,轉(zhuǎn)回turtle.left(left)yu[level] = yu[level] - 1else: if r==1: turtle.left(right + left) turtle.right(left) else:turtle.right(right + left)turtle.left(left) turtle.penup() #退回到上一級(jí)節(jié)點(diǎn)頂部位置 turtle.backward(length) 5.主函數(shù)部分主函數(shù)中直接調(diào)用上述函數(shù)就行,top控制樹(shù)的高度,turtle.speed控制畫(huà)的速度,最后的turtle.write()用來(lái)書(shū)寫最下方的簽名。```clikeif __name__ == ’__main__’: turtle.setup(width=1.0, height=1.0) #設(shè)置全屏顯示 turtle.hideturtle() # 隱藏turtle turtle.speed(0) # 設(shè)置畫(huà)筆移動(dòng)的速度,0-10 值越小速度越快 # turtle.tracer(0,0) #設(shè)置動(dòng)畫(huà)的開(kāi)關(guān)和延遲,均為0 turtle.penup() # 抬起畫(huà)筆 turtle.left(90) # 默認(rèn)方向?yàn)槌痻軸的正方向,左轉(zhuǎn)90度則朝上 turtle.backward(300) # 設(shè)置turtle的位置,朝下移動(dòng)300 top = 9 #樹(shù)高 yu = Fibonacci_Recursion(top) #生成斐波契那數(shù)列 yu.remove(yu[0]) #print(yu) 打印斐波那契數(shù)列 button = 0 draw(top, 120, 0, yu, button) # 調(diào)用函數(shù)開(kāi)始繪制 turtle.write(' wsw', font=('微軟雅黑', 14, 'normal')) #生成簽名 turtle.done()
運(yùn)行程序后,“海龜”會(huì)幫你畫(huà)出整棵樹(shù),你只需要看著它畫(huà)就行,需要等待一定的時(shí)間,最后的一種成品如下,是想要的一半葉子在空中的感覺(jué)了,哈哈哈哈~
到此這篇關(guān)于Python趣味挑戰(zhàn)之turtle庫(kù)繪畫(huà)飄落的銀杏樹(shù)的文章就介紹到這了,更多相關(guān)turtle庫(kù)繪畫(huà)飄落的銀杏樹(shù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. IntelliJ IDEA創(chuàng)建web項(xiàng)目的方法2. ASP.NET MVC通過(guò)勾選checkbox更改select的內(nèi)容3. Python寫捕魚(yú)達(dá)人的游戲?qū)崿F(xiàn)4. Python Request類源碼實(shí)現(xiàn)方法及原理解析5. python實(shí)現(xiàn)與redis交互操作詳解6. python基礎(chǔ)之匿名函數(shù)詳解7. python numpy中setdiff1d的用法說(shuō)明8. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說(shuō)明(學(xué)習(xí))9. python中的bool數(shù)組取反案例10. Python多線程實(shí)現(xiàn)支付模擬請(qǐng)求過(guò)程解析
