Python cherry tree

Cherry tree

foreword

Pink is my favorite! The Python cherry blossom tree is waiting for you~
Hello, friends, it’s been a long time. The cherry blossoms are so beautiful recently. The blogger wants to share the joy of spring with you. Let’s take a look at the cherry blossom tree drawn by the blogger!

1. Turtle basics

It's a cliché, before using python to draw cherry blossom trees, let's learn about turtle first!

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

1.1 Turtle drawing board

Turtle's artboard size can be set with the turtle.screensize() function

turtle.screensize(width,height,bg)

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

1.2 Turtle brushes

Turtle's brush has several commonly used functions:

①turtle.penup(): Lift up the paintbrush, moving the brush at this time will not leave marks on the canvas
②turtle.pendown(): Put down the paintbrush, corresponding to turtle.penup. Drawing after painting 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 of them online The colors that can be used in python, there are many colors that can be used in python)
⑤turtle.hideturtle(): hide the brush (after hiding the brush, the brush will not be visible 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 a distance)
②turtle.backward(x): Move the brush backward x pixels (x can be understood as a distance)
③turtle.left(n): Rotate the brush to the left by n degrees
④turtle.right(n): Rotate the brush to the right by n degrees
⑤turtle.speed(): Set the speed of the brush drawing (1~10 increments, 0 is the fastest)

1.4 Turtle coloring

After drawing a picture, we often need to fill it with color. Here we can use the turtle.fillcolor() function, and write the color you want to fill in the brackets.
Pay attention to its basic format when using the turtle.fillcolor() function:

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)

① Fill in the word to be written in the double quotation marks in the first position
② move (optional): By default, move is false. If move is true, the pen will move to the lower right corner
③ align (optional): The possible value is one of left, center, and right, in string format
④ font (optional): font three Tuple (fontname, fontsize, fonttype), fontname is the font name (string format, such as "Arial"), fontsize is the font size), fonttype is the font type such as: normal (common), bold (bold), italic ( italics)

2. Python cherry blossom tree

After a brief understanding of the little turtle, we can step into today's topic!

Cherry tree

2.1 Cherry Blossoms

Since it is a cherry blossom tree, I must draw cherry blossoms. Here I chose cherry blossoms with five petals

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 Cherry tree

After the cherry blossom is drawn, we can recursively draw the tree

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 sakura 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 code of the cherry blossom tree in detail!

  1. Import the 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 randomly generates values.

  1. Define the 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, set the pen size and color. Then use a random number to generate the size of the flower, and draw 5 petals in a loop.

  1. Define the 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 number of stages of the trunk, and k represents the length of the trunk. In the function, first set the turtle to the pen state, set the color and pen size, and draw the tree trunk. If n>0, use random numbers to generate the angle, length and random scaling factor of the branch, then turn right at angle r, recursively call the tree function to draw the right branch, turn left at angle l+r, and recursively call the tree function to draw the left branch , and finally turn right at angle l to restore the angle. If n=0, it means that the trunk has reached the top, and petals need to be drawn. Draw two flowers, the distance between them and the distance back after are generated by random numbers. Then set the turtle to the pen-up state and return to the starting position of the trunk to prepare for drawing another tree.

  1. set canvas
tu.setup(800,480)
tu.bgcolor("lavenderblush") 
tu.hideturtle()  
tu.tracer(0)

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

  1. drawing 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 of random positions and sizes.

  1. finish drawing
tu.done()

Finally, the done function is used to indicate the end of the drawing.

2.5 Cherry Blossom Forest

How can a cherry blossom tree be enough? Use the loop statement reasonably and prepare a cherry blossom forest for her!

Sakura Show 1
Sakura Show 2

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 guys next time!

Guess you like

Origin blog.csdn.net/m0_68111267/article/details/130350743