SoFunction
Updated on 2024-11-15

Draw a Cherry Blossom Tree for Yourself with Python Turtle

A lot of things have been happening lately, unhappy at work, fruitless love, and the small friends around me leaving one after another. Although none of them are things that will break you down all at once, but when they accumulate, there will still be times when you suddenly feel tired and weary, and there is a sense of powerlessness that you don't know where to go next.

I've left a lot of memories in this city, but the people around me are leaving one by one, and now that I've come this far, there doesn't seem to be much left, and I don't have any idea where I'm going to go in the future. I am a person who always needs freshness, and I have stayed here for too long, so it's probably time to say goodbye.

Then, in an unfamiliar language, draw a cherry blossom tree to give yourself, the code can also be very romantic is not it. 520 has just passed, there is no love, you have to love yourself right.

I. Drawing steps

1. Environmental installation

You can refer to the previous blog:

Python crawler to crawl the movie site information and into the library

2. Image beautification

I've never used Python before, so I'll just analyze and optimize other people's code, and learn on the way.

This is the original rendering, a bit gray and drab, slightly altered to brighten it up a bit.

Code after modification:

from turtle import *
from random import *
from math import *

def tree(n,l):
    pd()# Putting pen to paper
    #Shadow effects
    t = cos(radians(heading()+45))/8+0.25
    pencolor(t,t,t)
    pensize(n/3)
    forward(l)#Drawing branches

    if n>0:
        b = random()*15+10 # Right branch deflection angle
        c = random()*15+10 # Left branch deflection angle
        d = l*(random()*0.25+0.7) # The length of the next branch
        # Turn right at a certain angle and draw a right branch #
        right(b)
        tree(n-1,d)
        # Turn left at an angle and draw a left branch
        left(b+c)
        tree(n-1,d)
        # Turn back
        right(c)
    else:
        #Drawing leaves
        right(90)
        n=cos(radians(heading()-45))/4+0.5
        ran=random()
        # here compared to the original randomly added filled circles to make the cherry blossom leaves look a little more
        if(ran>0.7):
            begin_fill()
            circle(3)
            fillcolor('pink')
        #Replaced the original randomly generated leaves with a uniform pink color
        pencolor("pink")
        circle(3)
        if(ran>0.7):
            end_fill()
        left(90)
        #Add 0.3x falling leaves
        if(random()>0.7):
            pu()
            #Falling
            t = heading()
            an = -40 +random()*40
            setheading(an)
            dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2)
            forward(dis)
            setheading(t)
            #Drawing leaves
            pd()
            right(90)
            n = cos(radians(heading()-45))/4+0.5
            pencolor(n*0.5+0.5,0.4+n*0.4,0.4+n*0.4)
            circle(2)
            left(90)
            pu()
            #Return
            t=heading()
            setheading(an)
            backward(dis)
            setheading(t)
    pu()
    backward(l)# Returned
    
bgcolor(0.956,0.9255,0.9882)#Set the background color (replace gray with lavender)
ht()# Hide the turtle
speed(0)#Speed 1-10 progressive, 0 fastest
tracer(0,0)
pu()# Lift the pen
backward(50)
left(90)# Turn left 90 degrees
pu()# Lift the pen
backward(300)# Back up 300
tree(12,100)# Recursive 7 layers
done()

Warmer effect afterward, love the warmer tones:

II. Technical learning

1. Introduction to turtle

The cherry blossom tree is drawn using turtle in Python. The power of Python lies in its many powerful libraries, and turtle is one of them, a library that comes with Python.

2. Basics of turtle drawing

turtle library drawing principle: there is a turtle in the center of the form, wandering around the canvas, walking through the trajectory of the formation of the drawing of the graphics, turtles controlled by the program, you can freely change the color, direction width and so on.

(1) . turtle Drawing Forms

setup is used to set the size of the form, the last two parameters are optional;

(width,height,startx,starty)

(2) . The coordinate system of turtle

On the canvas, by default, there is a coordinate axis with the origin at the center of the canvas, and a small turtle facing in the positive direction of the x-axis at the origin.

Here we use two words to describe the turtle: the origin (position) and the direction (orientation) of the face toward the x-axis. turtle drawings use position and orientation to describe the state of the turtle (the paintbrush).

(3) . turtle Brush Properties

(4) . turtle Drawing commands

motion command

control

Global control commands

Although I didn't change too much of the code from the original, and simply changed the style of the cherry blossom tree, I actually did a lot of experimenting and tweaking, and learned how to use turtle.

This blog references code from the following blogs:Drawing cherry blossoms, roses, Christmas trees using python graphics module turtle library code example

to this article on the use of Python Turtle to draw a cherry blossom tree to their own article is introduced to this, more related Python Turtle cherry blossom tree content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!