Preface
TURTLE library is a very popular function library in Python language.
Imagine a small turtle, which starts with a coordinate system with X and vertical axis as X, and (0,0). It moves according to the control of a set of function instructions to move in this plane coordinate system, so as to be in it in this plane coordinate system, thereby Draw a graphic on the path of crawling
Next, we will use the TURTLE function to draw several cherry trees:
Description: The code uses a random function, so each execution process is not only dynamic, but also the results are different.
1. Dynamic generate cherry blossoms
result display: (This is dynamic, you will not do GIF, you can only show the result)
code is as follows:
Left branch deflection angle
d = l*(random ()*0.25+0.7) #The length of the next branch
#Right to a certain angle, draw the right branch
Right (b)
Tree (n-1, d)
#Left to a certain angle, draw the left branch
left (B+C)
Tree (n-1, d)
#Turn back
Right (C)
else:
# #画 画
Right (90)
n = cos (radians (heading () -4))/4+0.5
Pencolor (n, n*0.8, n*0.8)
circle (3)
left (90)
#Add 0.3 times falling leaves
if (random ()> 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 () -4))/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)#Return
BGCOLOR (0.5, 0.5,0.5)#background color
ht ()#Hide tultle
Speed (0)#speed 1-10 gradual, 0 fastest
tracer (0,0)
pu ()#p
backward (100)
left (90)#to 90 degrees left to 90 degrees
pu ()#p
backward (300)#back 300
Tree (12,100)#recursively 7 floors
Done (Done ()
2, fall effect cherry blossom
result display:
code is as follows:
From TURTLE Import *
From Random Import *
From Math Import *
DEF TREE (n, L):
pd ()#lower pen
#Shadow effect
T = COS (Radians (Heading ()+45))/8+0.25
Pencolor (t, t, t)
Pensize (n/3)
forward (l)#Draw branches
if n> 0:
B = Random ()*15+
3, dark effect cherry blossoms
result display:
code is as follows:
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()
I amGardeniall, your likes, comments, and attention are the unremitting motivation I continue to create.
Learning is endless, and the qi is vast. Let us cheer together. Intersection