Introduction:

Uh huh! So soon it’s Saturday again.

The days at work are so long!

The rotational speed of the head is also slower, really do not know what to do, go to work has been playing soy sauce, the brain a blank sa, also do not know what they are doing!

Until you see a big guy’s article, look up, this code love love, so should be your favorite type!

Today take you to write a beautiful floral small fresh style tree! Fallen flowers!

The body of the

Turtle: The longer I use it, the better it is! How sweet!

(1) First draw the trunk of the tree.

From Turtle import * left (80) fd(100) right (30) fd(30) right (30) fd(40) input()Copy the code

Let’s change the thickness and color of the line segment to make it look more like a tree. color|pensize()

Change the thickness and color of the pen by using t.pensize() and t.Color () to lengthen the branches of the tree and reduce the thickness of the pen:

from turtle import *

color('#5E5E5E')
pensize(6)
left(80)
fd(150)
right(30)
fd(50)
right(30)
fd(60)
input()
Copy the code

The effect is as follows:

(2) First draw the trunk of the tree, then draw the branches of the tree using functions:

from turtle import * def drawTree(length): Right (20) fd(length) color('#5E5E5E') pensize(5) up() goto(0,-300)# down() left(80) fd(140) drawTree(60) input()Copy the code

Start painting branches using recursion:

from turtle import * def drawTree(length): if length>1: Right (20) fd(length) drawTree(lengh-10) color('#5E5E5E') pensize(5) up() goto(0,-300)# down() left(80) fd(140)  drawTree(60) input()Copy the code

Now the length, rotation Angle is fixed difference, use random number to draw more realistic branches:

def drawTree(length):
    if length>1:
        right(20)
        fd(length)
        randlen=random.random()
        drawTree(length - 10*randlen)

def drawTree(length):
    if length>1:
        randangle=random.random()
        randlen=random.random()
        right(20*randangle)
        fd(length)

        drawTree(length - 10*randlen)
Copy the code

(3) Draw left and right branches:

from turtle import * import random def drawTree(length): if length>1: Randlen =random. Random (); randlen=random. Random () Fd (length) right(20*randangle) drawTree(length - 10*randlen) left(40 *randangle) drawTree(length -10 * Randlen) # Why do I need to turn another 20 degrees to the right? That's because I've taken a total of 40 degrees to the left, using a backward backward, it has to be the same Angle, Otherwise the Angle will be different and the position won't be right(20 * randangle) up() backward(length) down() setworldcoordinates(-1000,-750,1000,750) Tracer (False) color('#5E5E5E') pensize(5) up() goto(0,-300)# down() left(80) fd(140) drawTree(120) input()Copy the code

However, since there is no limit on the Angle and length of branches for random numbers, you can multiply an appropriate number at random time and lower the starting position further, with the code as follows:

randangle=2*random.random()
randlen=2*random.random()
.
.
.
.
.
.
goto(0,-700)
Copy the code

Effect:

(4) The branches and trunks are all painted, leaving only red flowers and green leaves!

from turtle import * import random def drawTree(length): if length>1: If length<30 and length>14: pensize(4) elif length<15 and length>5: Elif length<5 and length>1: color('#FE2E9A') else: Random () randlen=2*random. Random () randlen=2*random. Random () randlen=2*random. Let's adjust the Angle, Fd (length) right(20*randangle) drawTree(length - 10*randlen) left(40 *randangle) drawTree(length -10 * Randlen) # Why do I need to turn another 20 degrees to the right? That's because I've taken a total of 40 degrees to the left, using a backward backward, it has to be the same Angle, Otherwise the Angle will be different and the position won't be right(20 * randangle) up() backward(length) down() setworldcoordinates(-1000,-750,1000,750) Tracer (False) color('#5E5E5E') pensize(5) up() goto(0,-700)# down() left(80) fd(140) drawTree(120) input()Copy the code

The effect is as follows:

(5) Draw falling flowers:

from turtle import * import random def drawTree(length): if length>1: If length<30 and length>14: pensize(4) elif length<15 and length>5: Elif length<5 and length>1: color('#FE2E9A') else: Random () randlen=2*random. Random () randlen=2*random. Random () randlen=2*random. Let's adjust the Angle, Fd (length) right(20*randangle) drawTree(length - 10*randlen) left(40 *randangle) drawTree(length -10 * Randlen) # Why do I need to turn another 20 degrees to the right? That's because I've taken a total of 40 degrees to the left, using a backward backward, it has to be the same Angle, Otherwise the Angle will be different and the position will not be right(20 * randangle) up() backward(length) down() def fallingFlowers(m): x,y=-1000,-750 for i in range(30): up() goto(x,y) x+=100 down() yval=50 for i in range(m): a = 100*random.random() b = 2*random.random() print(a) if a>59: color('#FE2E9A') else: Color ('#04B486') circle(5) up() goto(x,y+(yval*b)) fd(a) yval+=50 down() setWorldcoordinates (-1000,-750,1000,750) Tracer (False) color('#5E5E5E') pensize(5) # up() # goto(0,-700)# down() # left(80) # fd(140) # drawTree(120) fallingFlowers(10) input()Copy the code

The renderings are as follows:

The final completed renderings:

conclusion

I show you fallen petals, do you like it? Remember one key three even oh!!

If you need a new installation package activation code, complete project + source notes, and more Python materials

Source code base: # private xiaobian 06# can be free oh!!