python知识:用turtle绘制樱桃树
生活随笔
收集整理的這篇文章主要介紹了
python知识:用turtle绘制樱桃树
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
用pyhon的turtle結(jié)合分形知識,進行櫻花樹的繪畫,供大家研究。
代碼來源在github上,Turtle/cherry tree.py at main · AXYZdong/Turtle · GitHub,大家自己下載,或拷貝粘貼下述代碼。
1 代碼實現(xiàn)
import turtle as T import random import time# 畫櫻花的軀干(60,t) def Tree(branch, t):time.sleep(0.0005)if branch > 3:if 8 <= branch <= 12:if random.randint(0, 2) == 0:t.color('snow') # 白else:t.color('lightcoral') # 淡珊瑚色t.pensize(branch / 3)elif branch < 8:if random.randint(0, 1) == 0:t.color('snow')else:t.color('lightcoral') # 淡珊瑚色t.pensize(branch / 2)else:t.color('sienna') # 赭(zhě)色t.pensize(branch / 10) # 6t.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()# 掉落的花瓣 def Petal(m, t):for i in range(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)def fallingFlowers(m):x, y = -900, -400for i in range(30):t.up()t.goto(x, y)x += 100t.down()yval = 50for i in range(m):a = 100 * random.random()b = 2 * random.random()if a > 59:t.color('#FE2E9A')else:t.color('#04B486')t.circle(5)t.up()t.goto(x, y + (yval * b))t.fd(a)yval += 50t.down()# 愛心def Love(x, y):t.up()t.home()t.goto(x, y)t.pensize(4)t.color('pink', 'pink') # 粉t.left(90)t.forward(100)t.down()t.begin_fill()t.circle(70, 230)t.forward(140)t.end_fill()t.begin_fill()t.seth(40)t.forward(135)t.right(5)t.circle(70, 235)t.end_fill()t.up()# 文字 def Font():t.penup()t.goto(-500, -300)t.pencolor('black')t.write("By AXYZdong,不一樣的櫻花+愛心送給不一樣的你,520", font=('方正行黑簡體', 30, 'normal'))# 繪圖區(qū)域 t = T.Turtle() # 畫布大小 w = T.Screen() t.hideturtle() # 隱藏畫筆 t.getscreen().tracer(5, 0) w.screensize(1000, 800, 'wheat') # wheat小麥 t.left(90) t.up() t.backward(150) t.down() t.color('sienna')# 畫櫻花的軀干 Tree(60, t) # 掉落的花瓣 Petal(300, t)fallingFlowers(10) # 愛心 Love(-400, 100) Love(400, 100) Love(-400, -150) Love(400, -150) # 文字 Font() w.exitonclick()2 參考結(jié)果
總結(jié)
以上是生活随笔為你收集整理的python知识:用turtle绘制樱桃树的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基础理论:啥是分布函数CDF、啥叫联合分
- 下一篇: 基础理论:给定离散概率表求分布函数