Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Cherry trees

Knowing the command description helps you understand how the source code works

screensize(width, height, bg=color) Set the screen size and color
setup(p1, p2) Set the screen size. If P1 and P2 are decimal numbers, the screen proportion is indicated. When P1 and P2 are integers, they represent pixels
tracer(speed) Set the drawing speed. A higher speed indicates a faster drawing speed
penup() Start (think of drawing with ink and then pick up the pen)
forward() To move forward
backward() Move back
left(degree) Rotate degree counterclockwise
right(degree) Rotate degree clockwise
pendown() Put pen to paper
pencolor(color) The pen and ink color is color
circle(r) Let me draw a circle of radius R

The source code to share

Can copy the following source code directly to run, no error.

import turtle
 
from random import random
from random import randint
 
 
def draw_petal(turtle_obj, flower) :
    # Draw falling petals
    for i in range(int(flower)):
        # Positive and negative can make the brush go in both directions
        x = flower - 4 * flower * random()
 
        # Overall petal width (-10, 10)
        y = 10 - 20 * random()
 
        # Pick up pen, forward Y, turn left 90, go X, write
        turtle_obj.penup()
        turtle_obj.forward(y)
        turtle_obj.left(90)
        turtle_obj.forward(x)
        turtle_obj.pendown()
 
        # coral
        turtle_obj.pencolor("lightcoral")
        # circle
        turtle_obj.circle(1)
 
        # Back to square one
        # Pick up pen, back x, turn right 90, back Y, start pen
        turtle_obj.penup()
        turtle_obj.backward(x)
        turtle_obj.right(90)
        turtle_obj.backward(y)
        turtle_obj.pendown()
 
 
# 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:
            Branch left and right with a probability of 0.5
            if randint(0.1) = =0:
                # left is white
                turtle_obj.pencolor("snow")
            else:
                # Coral on the right
                turtle_obj.pencolor("lightcoral")
            # branches
            turtle_obj.pensize(branch / 2)
        elif 8 <= branch <= 16:
            With a probability of 0.33, it is divided into left, middle and right branches
            if randint(0.2) = =0:
                # left is white
                turtle_obj.pencolor("snow")
            else:
                # Coral color in center and right
                turtle_obj.pencolor("lightcoral")
            # branch
            turtle_obj.pensize(branch / 4)
        else:
            # brown
            turtle_obj.pencolor(tree_color)
            # twigs
            turtle_obj.pensize(branch / 10)
 
        # Initial trunk length
        turtle_obj.forward(branch)
 
        # Random degree factor
        a = 1.5 * random()
        # Rotate clockwise at random Angle (0 ~ 30 degrees)
        turtle_obj.right(20 * a)
 
        # Random length factor
        b = 1.5 * random()
        # Draw to the right until you can't move
        draw_tree(turtle_obj, branch - 10 * b, tree_color)
 
        # Random left-turn 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 an Angle
        turtle_obj.right(20 * a)
        # start
        turtle_obj.penup()
 
        # Return to the starting point
        turtle_obj.backward(branch)
        turtle_obj.pendown()
 
 
def get_screen(width, height, color, speed) :
    Create a screen
    screen_obj = turtle.Screen()
    # canvas size: (width, height
    screen_obj.screensize(width, height, bg=color)
    screen_obj.setup(1.0.1.0)
    # speed up
    screen_obj.tracer(speed)
 
    return screen_obj
 
# Draw multiple trees
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 pixels
        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 brush
        turtle_obj = turtle.Turtle()
        # Brush thickness
        turtle_obj.pensize(pensize)
        # Pick up pen, forward forward, left 90, backward, draw pen
        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)
 
        # Stem thickness
        branch = pensize * 15
        # the fallen petal number
        flowers = branch
        # JTH tree
        draw_tree(turtle_obj, branch, tree_color)
        # petals
        draw_petal(turtle_obj, flowers)
 
 
if __name__ == '__main__':
 
    Create a screen
    my_screen_width = 800
    my_screen_height = 600
    my_screen_color = 'wheat'
    my_screen_speed = 5
    my_screen_obj = get_screen(my_screen_width, my_screen_height,
                               my_screen_color, my_screen_speed)
 
    The cherry blossom trees #
    # tree
    my_tree_num = 5
    trees(my_tree_num)
 
    # Click close canvas
    my_screen_obj.exitonclick()
Copy the code

More interesting source code to share, you can follow me.