posted on 2023-05-21 15:58 read(253) comment(0) like(8) collect(1)
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!
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 functions of drawing. After you learn to draw with Turtle, you can draw any pattern you want oh.
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
Turtle's brush has several commonly used functions:
①turtle.penup(): Lift up the brush, moving the brush at this time will not leave traces on the canvas
②turtle.pendown(): Put down the brush, corresponding to turtle.penup, you can continue to draw after putting down the brush (put down 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)
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)
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() #结束填充
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)
After a brief understanding of the little turtle, we can step into today's topic!
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)
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)
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()
Let's analyze the code of the cherry blossom tree in detail!
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.
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.
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.
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.
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.
tu.done()
Finally, the done function is used to indicate the end of the drawing.
How can a cherry blossom tree be enough? Use the loop statement reasonably 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)
……
See you guys next time!
Author:Theheartoflove
link:http://www.pythonblackhole.com/blog/article/25246/40bfc8139ad950c48690/
source:python black hole net
Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!