how to draw a simple tree in python turtle
I won't go into details about the introduction of the tools here, and here is the address of the most detailed document that I feel is attached:https://docs.python.org/zh-cn/3/library/turtle.html?highlight=turtle#turtle.TurtleScreen
firstSee the renderings:
The above is what Xiaosheng sacrificed during lunch break~ Although it is not so spicy, but I still want to share it with everyone and get into the topic.
The code is mainly divided into two parts:Tree code part, ground petal part
Tree code part
In fact, the code is very simple, mainly because we have to know how to implement those key points (I know this is nonsense, but I have to say something to make up the word count...):
1. The trunk should be thinner as it goes up-that is, the bigger the brush, the smaller the size.
2. The branches become shorter as they go up-the length of the branches
3. The branches will fork-the entry point for the recursive event to continue execution
4. The angle of branches and branches-pen offset
4. What should be longer-the exit of recursive events
Once the above points are met, the tree will look normal (or in line with popular belief). In general, we need a point to determine recursive events
Here I use the number of forks to determine the entrance and exit of the .Of course, we can also use extension length or a variable, as long as we can find the entry and exit conditions, see the code:
1 # Tree drawing method: n is the number of branches and l is the length of branch extension 2 def drawTree(n, l): 3 pendown() 4 # Set pen color-branch color 5 pencolor(' #5d3c3c ' ) 6 # Pen size 7 pensize( n / 1.5) 8 # Extension length 9 forward(l) 10 # According to n, judge whether to continue to extend the branch recursively or end the branch extension 11 if n > 0: 12 # dr is the clockwise offset of the brush, dl is the counterclockwise offset of the brush 13 dr = randint(30, 40) 14 dl = randint(30, 40) 15 16 # This extension length: Randomly reduce the extension length each time (so you can also set pensize according to l) 17 move = l * (random() * 0.4 + 0.5) 18 19 # Offset clockwise dr 20 right(dr) 21 # Call itself recursively, the remaining number of extensions -1 22 drawTree(n - 1, move) 23 # Because the above is clockwise offset by dr, the clockwise offset plus the counterclockwise offset: dr + dl is the actual offset of the bifurcation 24 left(dr + dl) 25 drawTree(n - 1, move) 26 # Offset dl clockwise, back to the original point of the pen 27 right(dl) 28 else : 29 # The branch extension is over, call the method of drawing flowers 30 drawPetal(3) 31 # Lift the pen 32 penup() 33 # Back to the starting point 34 backward(l)
Flower code:
After the branch completes the specified number of forks, in order to look better, something needs to be longer, such as the code:
1 # Flower painting method: n is the radius of the flower (actually the radius of the circle...) 2 def drawPetal(n): 3 # Specify the color mode as rgb mode 4 colormode(255) 5 # Randomly generate rgb color values 6 r = randint(200, 255) 7 g = randint(8, 158) 8 b = randint(8, 158) 9 10 # Draw circle and fill color 11 begin_fill() 12 fillcolor(r, g, b) 13 pencolor(r, g, b) 14 circle(n) 15 end_fill()
If here is just to draw a tree, then our work has been basically completed, of course we need a startup method:
def run(): # Set canvas ratio: relative to display setup(1.0, 1.0) penup() # Move to coordinates (-50, -150) goto(-50, -150) # Rotate 90 degrees counterclockwise so that the pen is pointing directly up left(90) pendown() # Hide brush hideturtle() # Whether to track the painting process: whether to show the painting process step by step tracer(False) # Call tree method drawTree(13, 150)
Copy the appeal code into the editor, and call the run() method, and a magical tree will appear (assuming you have installed the python runtime environment).
Let's sprinkle some petals on it. The position of the petals here is placed below the starting point of the tree. In fact, you can specify the position, range and size at will.
To show the effect of a falling image (I have already seen it, it looks good):
1 # Petal position generation: m is the number of petals, x y determines the initial position of the petals 2 # Here I set the initial position of the petals to be near the starting point of the tree 3 def petalPlace(m, x, y): 4 penup() 5 goto(x, y) 6 pendown() 7 setheading(0) 8 tracer(False) 9 for i in range(m): 10 # Draw a flower at the starting point 11 if i == 0: 12 drawPetal(5) 13 else : 14 penup() 15 goto(x, y) 16 # a is the distance from the starting point of the next petal in the horizontal direction 17 a = randint(20, 400) 18 # b is the vertical distance of the next petal from the starting point 19 b = randint(-50, 50) 20 21 # Move the brush the corresponding distance 22 forward(a) 23 left(90) 24 forward(b) 25 right(90) 26 pendown() 27 28 # Call the petal painting method 29 drawPetal(5)
At this point, all our codes have been completed. Below is all the codes (comments have been removed):
1 from turtle import * 2 from random import * 3 4 # Tree drawing method 5 def drawTree(n, l): 6 pendown() 7 pencolor(' #5d3c3c ' ) 8 pensize( n / 1.5) 9 forward(l) 10 if n > 0: 11 dr = randint(30, 40) 12 dl = randint(30, 40) 13 move = l * (random() * 0.4 + 0.5) 14 right(dr) 15 drawTree(n - 1, move) 16 left(dr + dl) 17 drawTree(n - 1, move) 18 right(dl) 19 else : 20 drawPetal(3) 21 penup() 22 backward(l) 23 24 # Petal position generation 25 def petalPlace(m, x, y): 26 penup() 27 goto(x, y) 28 pendown() 29 setheading(0) 30 tracer(False) 31 for i in range(m): 32 if i == 0: 33 drawPetal(5) 34 else : 35 penup() 36 goto(x, y) 37 a = randint(20, 400) 38 b = randint(-50, 50) 39 forward(a) 40 left(90) 41 forward(b) 42 right(90) 43 pendown() 44 drawPetal(5) 45 46 # Flower painting method 47 def drawPetal(n): 48 colormode(255) 49 r = randint(200, 255) 50 g = randint(8, 158) 51 b = randint(8, 158) 52 begin_fill() 53 fillcolor(r, g, b) 54 pencolor(r, g, b) 55 circle(n) 56 end_fill() 57 58 # Start method 59 def run(): 60 setup(1.0, 1.0) 61 penup() 62 goto(-50, -150) 63 left(90) 64 pendown() 65 hideturtle() 66 tracer(False) 67 drawTree(13, 150) 68 petalPlace(160, -100, -150) 69 70 run() 71 done()
It's worth noting that because we use a lot of randomly generated values, the tree generated every time is the same, that is to say, we can build a forest,
With the above code, we have very little to add:
The general idea is to randomly generate the starting point of the tree, and then call our above method. Of course, some values need to be fine-tuned for aesthetics.
At least the petals under each tree do not need this method. We can replace the petals falling effect on the trees in the entire area. Here is a forest example
(Without changing the petals):
1 # m corresponds to the number of spanning trees 2 def run(m): 3 setup(1.0, 1.0) 4 for i in range(m): 5 penup() 6 x = randint(-500, 500) 7 y = randint(-300, 300) 8 goto(x ,y) 9 left(90) 10 tracer(False) 11 drawTree(10, 150) 12 petalPlace(100, x, y)
Python's Beautiful Soup is simple to use
Beautiful Soup is a library of python, the main function is to grab data from web pages Beautiful Soup provides some simple, python-style functions to handle navigation, search, modify analysis tree a...
Note-8: Use turtle library to draw graphics
1. Form function turtle.setup(width,height,startx,starty) Role: Set the size and position of the form width: The width of the window. If the value is an integer, it means the pixel value; if the value...
Use turtle library to draw a rose with text
Reference link:https://jingyan.baidu.com/article/d169e18689f309026611d8c8.html https://blog.csdn.net/weixin_41939278/article/details/88342406 Hahaha, I didn't draw this myself. The drawing in B...
Source: https://www.programmerall.com/article/4199561950/
0 Response to "how to draw a simple tree in python turtle"
Post a Comment