# The same Python code draws many different cherry trees. Which one do you like?

2022-01-31 05:34:35

「 This is my participation 11 The fourth of the yuegengwen challenge 1 God , Check out the activity details ：2021 One last more challenge 」.

## Preface

Lidong ！ Officially entering winter , But the weather in Changsha is good and bad in these two days , But it's sunny today , I suddenly thought of the video of drawing cherry blossoms before , Then take advantage of a good mood to analyze it , Hey, hey, it's really nice , And a code can draw a variety of styles at random , Let's have a look

## Achieve the goal

### Video presentation ：

Because a kind of cherry tree needs to be painted for a long time , I'll show you a little , Of course, there are many kinds that have not been recorded one by one , You can study it yourself , Xi xi xi

## Tool preparation

development tool ：pycharm
development environment ：python3.7, Windows11
Using the toolkit ：turtle

## Project analysis ideas

The project idea is divided into 3 part ：

• Draw the fallen petals of cherry blossoms , Fallen petals
• Add branches to the cherry tree
• Add a painting background to the cherry tree
• Color drawing selects colors of various styles

### Draw falling petals

Determine the number of petals falling , The number of fallen petals is judged according to the cherry tree branches , Multiply by the number of branches 15, The bigger the tree, the more it falls
Set the coordinates of the petals. Set the size of the petals to （10,10） Control the brush to move to the specified area and lift the brush , forward y, Turn left 90, go x, Set pen to paper , Draw the petal shape , After painting, the number of petals will ok, Assign color to the drawing shape , Outline the circle , Go back to the starting point and pick up the pen , back off x, Turn right 90, back off y, Set pen to paper

``````def draw_petal(turtle_obj, flower):
#  Draw falling petals
for i in range(int(flower)):
#  Positive and negative can make the brush go in two directions
x = flower - 4 * flower * random()

#  Overall petal width (-10, 10)
y = 10 - 20 * random()

#  write , forward y, Turn left 90, go x, Set pen to paper
turtle_obj.penup()
turtle_obj.forward(y)
turtle_obj.left(90)
turtle_obj.forward(x)
turtle_obj.pendown()

#  Coral
turtle_obj.pencolor("lightcoral")
#  A circle
turtle_obj.circle(1)

#  Back to the beginning
#  write , back off x, Turn right 90, back off y, Set pen to paper
turtle_obj.penup()
turtle_obj.backward(x)
turtle_obj.right(90)
turtle_obj.backward(y)
turtle_obj.pendown()

Copy code ``````

### Draw the branches

Determine the number of branches , Color number selection of color , First, set the minimum branch length by default, and set the color on both sides of each branch to look better , White on the left , Coral on the right , The probability of branching is set at 0.5, Branches can be set to grow randomly , Set by random number

``````#  Draw the branches
def draw_tree(turtle_obj, branch, tree_color):
#  Set a minimum branch length
min_branch = 4

if branch > min_branch:
if branch < 8:
#  With 0.5 Probability , towards the left 、 Right branch
if randint(0, 1) == 0:
#  White on the left
turtle_obj.pencolor("snow")
else:
#  Coral on the right
turtle_obj.pencolor("lightcoral")
#  Branches
turtle_obj.pensize(branch / 2)
elif 8 <= branch <= 16:
#  With 0.33 Probability , Divided into left 、 in 、 Right branch
if randint(0, 2) == 0:
#  White on the left
turtle_obj.pencolor("snow")
else:
#  in 、 Coral on the right
turtle_obj.pencolor("lightcoral")
#  The branches of the
turtle_obj.pensize(branch / 4)
else:
#  brown
turtle_obj.pencolor(tree_color)
#  Twig
turtle_obj.pensize(branch / 10)

#  The initial trunk length
turtle_obj.forward(branch)

#  Random degree factor
a = 1.5 * random()
#  Rotate the random angle clockwise （0～30 degree ）
turtle_obj.right(20 * a)

#  Random length factor
b = 1.5 * random()
#  Draw to the right , Until the painting doesn't move
draw_tree(turtle_obj, branch - 10 * b, tree_color)

#  Left turn random angle
turtle_obj.left(40 * a)
#  Draw to the left , Until you can't move the position
draw_tree(turtle_obj, branch - 10 * b, tree_color)

#  Turn right at an angle
turtle_obj.right(20 * a)
#  write
turtle_obj.penup()

#  The end of recursion returns to the starting point
turtle_obj.backward(branch)
turtle_obj.pendown()

Copy code ``````

Create a canvas , Add data color , Set the acceleration factor of operation

``````def get_screen(width, height, color, speed):
#  Create a screen
screen_obj = turtle.Screen()
#  Canvas size ：(width, height), Color ：color
screen_obj.screensize(width, height, bg=color)
screen_obj.setup(1.0, 1.0)
# speed Double acceleration
screen_obj.tracer(speed)

return screen_obj

Copy code ``````

Color canvas brush selection ：

• The color of the trunk
• The size of the brush
• Progressive phase prime lattice
• Create a brush
• The choice of picking up and writing
• Brush color configuration
• Set constant parameters The thickness of the branches Number of fallen flowers Number of pieces
``````def trees(tree_num):
#  Color
color = ['brown', 'tan', 'black']

for j in range(tree_num):
#  Trunk color
tree_color = color[randint(0, len(color) - 1)]

#  Brush size
pensize = randint(2, 5)
#  Forward pixel
forward = ((-1) ** pensize) * pensize * randint(20, 50)
#  Back pixels
if pensize <= 3:
backward = ((-1) ** pensize) * (5 - pensize) * randint(10, 15)
else:
backward = pensize * randint(45, 50)

#  Create a brush
turtle_obj = turtle.Turtle()
#  Brush thickness
turtle_obj.pensize(pensize)
#  write , forward forward, Turn left 90,backward, Set pen to paper
turtle_obj.penup()
turtle_obj.forward(forward)
turtle_obj.left(90)
turtle_obj.backward(backward)
turtle_obj.pendown()
#  Brush color ： brown
turtle_obj.pencolor(tree_color)

#  The branches are thick and thin
branch = pensize * 15
#  Number of fallen flowers
flowers = branch
#  The first j tree
draw_tree(turtle_obj, branch, tree_color)
#  petals
draw_petal(turtle_obj, flowers)

Copy code ``````

I am white and white i, A program Yuan who likes to share knowledge ️
Interested can pay attention to my official account ： White and white Python【 Thank you very much for your praise 、 Collection 、 Focus on 、 Comment on , One key three links support 】