Drawing cherry blossoms with python code is simple, tutorial for drawing cherry blossoms with python code

This article will talk about how easy it is to draw cherry blossoms with python code, as well as a tutorial for drawing cherry blossoms with python code. I hope it will be helpful to you. Don’t forget to bookmark this site.

Preface My favorite
color is pink! The Python cherry blossom tree is waiting for you~
Hello friends, it’s been a long time since we last met. The cherry blossoms are blooming so beautifully recently. The blogger wants to share the joy of spring with everyone. Let’s take a look at the cherry blossom tree painted by the blogger!

1. Turtle Basics

老生常谈啦,在用python画樱花树前,我们先来了解一下turtle吧!

Turtle is an important package (built-in package) for drawing in Python. It contains a wealth of drawing tools and various drawing functions. After you learn to draw with Turtle, you can draw any pattern you want. oh.

1.1 Turtle artboard
Turtle’s artboard size can be set using the turtle.screensize() function

turtle.screensize(width,height,bg)

Set the size of the artboard, including width and height, width is the width, height is the height, and bg is the canvas color.

1.2 Turtle brush
Turtle’s brush has several commonly used functions:

①turtle.penup(): Lift the brush. At this time, moving the brush will not leave traces on the canvas.
②turtle.pendown(): Put down the brush, corresponding to turtle.penup. You can continue painting after putting down the brush (put it down Painting after the brush will leave traces on the canvas)
③turtle.pensize(): Control the size of the brush (you can define the size of the brush according to your needs)
④turtle.pencolor(): Control the color of the brush (you can check all the brushes online There are many colors that can be used in python)
⑤turtle.hideturtle(): Hide the brush (after hiding the brush, you will not see the brush when drawing)

1.3 Turtle drawing
In the process of drawing, we often use some simple movement functions:

①turtle.forward(x): Move the brush forward x pixels (x can be understood as distance)
②turtle.backward(x): Move the brush back x pixels (x can be understood as distance)
③turtle.left(n): Rotate the brush n degrees to the left
④ turtle.right(n): Rotate the brush n degrees to the right
⑤ turtle.speed(): Set the speed of the brush drawing (1~10 increases, 0 is the fastest)

1.4 Turtle coloring


After drawing the picture, we often need to fill it with color. Here you can use the turtle.fillcolor() function. Write the color you want to fill in the brackets to learn c+ or python .
When using the turtle.fillcolor() function, pay attention to its basic format:
turtle.beginfill()     #开始填充
turtle.fillcolor()      #输入填充的颜色
turtle.endfill()        #结束填充

1.5 Turtle writing

After completing the entire drawing, we can use the turtle.write() function to write

turtle.write(" ",move,align,font)

2. Python Sakura Tree

简单的了解turtle小海龟后,我们就可以步入今天的主题啦!

2.1 Sakura category

Since it is a cherry blossom tree, it must be painted with cherry blossoms. Here I chose a five-petal cherry blossom.

def flower():  
    tu.hideturtle() 
    tu.pensize(2)  
    tu.pencolor("pink")
    flowersize = ra.randint(1,5)  
    for j in range(5):  
        tu.forward(int(flowersize))
        tu.backward(int(flowersize))
        tu.right(72)

2.2 Sakura tree

After the cherry blossoms are drawn, we can draw the tree recursively.

def tree(n,k):
    tu.pendown()  
    tu.pencolor("black")
    tu.pensize(n/4)
    tu.forward(k)  
    if n>0:
        r=ra.random()*10+10
        l=ra.random()*10+10
        x=k*(ra.random()*0.25+0.7)  
        tu.right(r)
        tree(n-1,x)
        tu.left(l+r)
        tree(n-1, x)
        tu.right(l)
    else:
        tu.right(90)
        flower()
        tu.left(90)
        tu.pu()
        t=tu.heading()
        s=-ra.random()
        tu.setheading(s)
        x=ra.randint(1,5)
        tu.forward(x)
        tu.setheading(t)
        tu.pd()
        tu.right(90)
        flower()
        tu.left(90)
        tu.pu()
        t=tu.heading()
        tu.setheading(s)
        tu.backward(x)
        tu.setheading(t)
    tu.penup()
    tu.backward(k) 

2.3 Main function

Finally, we need a main function to implement the cherry blossom tree

tu.setup(800,480)
tu.bgcolor("lavenderblush") 
tu.hideturtle()  
tu.tracer(0)
tu.penup()  
tu.goto(0,-150)
tu.pendown()
tu.left(90)  
tree(12,50)
for i in range(99):
    tu.penup()
    tu.goto(ra.randint(-150,150),ra.randint(-150,0))
    tu.pendown()
    flower()

2.4 Program Analysis

Let’s analyze the Sakura Tree code in detail!

  1. Import turtle and random libraries
    import turtle as tu
    import random as ra
    

    In the code, the turtle library is used for drawing, and the random library is used to randomly generate values.

  2. Define flower function
    def flower():  
        tu.hideturtle() 
        tu.pensize(2)  
        tu.pencolor("pink")
        flowersize = ra.randint(1,5)  
        for j in range(5):  
            tu.forward(int(flowersize))
            tu.backward(int(flowersize))
            tu.right(72)
    

    The flower function is used to draw a flower. First, hide the turtle and set the pen size and color. Then use random numbers to generate the size of the flower and draw 5 petals in a loop.

  3. Define tree function
    def tree(n,k):
        tu.pendown()  
        tu.pencolor("black")
        tu.pensize(n/4)
        tu.forward(k)  
        if n>0:
            r=ra.random()*10+10
            l=ra.random()*10+10
            x=k*(ra.random()*0.25+0.7)  
            tu.right(r)
            tree(n-1,x)
            tu.left(l+r)
            tree(n-1, x)
            tu.right(l)
        else:
            tu.right(90)
            flower()
            tu.left(90)
            tu.pu()
            t=tu.heading()
            s=-ra.random()
            tu.setheading(s)
            x=ra.randint(1,5)
            tu.forward(x)
            tu.setheading(t)
            tu.pd()
            tu.right(90)
            flower()
            tu.left(90)
            tu.pu()
            t=tu.heading()
            tu.setheading(s)
            tu.backward(x)
            tu.setheading(t)
        tu.penup()
        tu.backward(k) 
    

    The tree function is used to draw a tree. The parameter n represents the level of the trunk, and k represents the length of the trunk. In the function, first set the turtle to the pen down state, set the color and pen size, and draw the trunk. If n>0, use random numbers to generate the angle, length and random scaling coefficient of the branch, then turn right by angle r, recursively call the tree function to draw the right branch, turn left by angle l+r, and recursively call the tree function to draw the left branch , and finally turn right to restore the angle l. If n=0, it means that the trunk has reached the top and the petals need to be drawn. Draw two flowers, the distance between them and the distance behind them are generated by random numbers. Then set the turtle to the pen-up state and return to the starting position of the trunk in preparation for drawing another tree.

  4. Set up canvas
    tu.setup(800,480)
    tu.bgcolor("lavenderblush") 
    tu.hideturtle()  
    tu.tracer(0)
    

    This code initializes the canvas settings, including the canvas size, background color, turtle's initial state and drawing speed.

  5. Draw trees and flowers
    tu.penup()  
    tu.goto(0,-150)
    tu.pendown()
    tu.left(90)  
    tree(12,50)
    for i in range(99):
        tu.penup()
        tu.goto(ra.randint(-150,150),ra.randint(-150,0))
        tu.pendown()
        flower()
    

    This code calls the tree function to draw a 12-level tree and the flowers in it. After that, draw 99 flowers in random positions and sizes.

  6. Complete drawing
    tu.done()
    

    Finally, use the done function to indicate the end of drawing.

    2.5 Cherry Blossom Forest

    How can one cherry blossom tree be enough? Use loop statements rationally and prepare a cherry blossom forest for her!

    for i in range(20):
        tu.penup()  
        tu.goto(ra.randint(-300,300),ra.randint(-150,0))
        tu.pendown()
        tu.left(90)  
        tree(8,30)
        tu.setheading(0)
        ……
    

    end

    See you next time, friends!

Guess you like

Origin blog.csdn.net/mynote/article/details/135232026