This article mainly introduces how to realize the painting of different varieties of cherry trees based on python, the text through the sample code is very detailed, for everyone's learning or work has a certain reference learning value, need to friends can refer to the next!
Dynamically generated cherry blossoms
Rendering (this one is dynamic):
Implementation Code:
import turtle as T import random import time # Drawing cherry blossom torsos (60,t) # def Tree(branch, t): (0.0005) if branch > 3: if 8 <= branch <= 12: if (0, 2) == 0: ('snow') # White else: ('lightcoral') # Light coral (branch / 3) elif branch < 8: if (0, 1) == 0: ('snow') else: ('lightcoral') # Light coral (branch / 2) else: ('sienna') # Ochre (zhě) color (branch / 10) # 6 (branch) a = 1.5 * () (20 * a) b = 1.5 * () Tree(branch - 10 * b, t) (40 * a) Tree(branch - 10 * b, t) (20 * a) () (branch) () # Falling petals def Petal(m, t): for i in range(m): a = 200 - 400 * () b = 10 - 20 * () () (b) (90) (a) () ('lightcoral') # Light coral (1) () (a) (90) (b) # Drawing area t = () # Canvas size w = () () # Hide the brushes ().tracer(5, 0) (bg='wheat') # wheat (90) () (150) () ('sienna') # Drawing the trunk of a cherry blossom Tree(60, t) # Falling petals Petal(200, t) ()
Falling effect
Rendering:
Implementation Code:
from turtle import * from random import * from math import * def tree(n,l): pd()# Putting pen to paper #Shadow effects t = cos(radians(heading()+45))/8+0.25 pencolor(t,t,t) pensize(n/3) forward(l)#Drawing branches if n>0: b = random()*15+10 # Right branch deflection angle c = random()*15+10 # Left branch deflection angle d = l*(random()*0.25+0.7) # The length of the next branch # Turn right at a certain angle and draw a right branch # right(b) tree(n-1,d) # Turn left at an angle and draw a left branch left(b+c) tree(n-1,d) # Turn back right(c) else: #Drawing leaves right(90) n=cos(radians(heading()-45))/4+0.5 pencolor(n,n*0.8,n*0.8) circle(3) left(90) #Add 0.3x falling leaves if(random()>0.7): pu() #Falling 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) #Drawing leaves 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) circle(2) left(90) pu() #Return t=heading() setheading(an) backward(dis) setheading(t) pu() backward(l)# Returned bgcolor(0.5,0.5,0.5)#Background color ht()# Hide the turtle speed(0)#Speed 1-10 progressive, 0 fastest tracer(0,0) pu()# Lift the pen backward(100) left(90)# Turn left 90 degrees pu()# Lift the pen backward(300)# Back up 300 tree(12,100)# Recursive 7 layers done()
dark effect
Effect:
Implementation Code:
from turtle import * from random import * from math import * def tree(n, l): pd() t = cos(radians(heading() + 45)) / 8 + 0.25 pencolor(t, t, t) pensize(n / 4) forward(l) if n > 0: b = random() * 15 + 10 c = random() * 15 + 10 d = l * (random() * 0.35 + 0.6) right(b) tree(n - 1, d) left(b + c) tree(n - 1, d) right(c) else: right(90) n = cos(radians(heading() - 45)) / 4 + 0.5 pencolor(n, n, n) circle(2) left(90) pu() backward(l) bgcolor(0.5, 0.5, 0.5) ht() speed(0) tracer(0, 0) left(90) pu() backward(300) tree(13, 100) done()
This is the whole content of this article.