Python to Draw Sakura Tree with Source Code

In this article, you’ll learn how to draw Sakura tree using python

Prerequisites:

  1. Python Basics
  2. turtle module

Install Necessary Modules:

Open your  Prompt  and type and run the following command (individually):

pip install turtle
  • turtle is a pre-installed Python library that allows users to create pictures and shapes with a provided, virtual canvas.
  • We can use functions like turtle.forward(…) and turtle.right(…) which can move the turtle around.
  • The onscreen pen you use to draw is called the turtle.

Once Installed now we can import it inside our python code.

Source Code:

'''
Python Program to Create Digital Clock
'''

# Import necessary modules!
import turtle
import random
from turtle import *
from time import sleep

static_turtle = turtle.Turtle()
frame = turtle.Screen()

def tree(branch_len, inner_turtle):
    if branch_len > 3:
        if 9 <= branch_len <= 12:
            if random.randint(0, 2) == 0:
                inner_turtle.color("snow")
            else:
                inner_turtle.color("lightcoral")
            inner_turtle.pensize(branch_len / 3)
        elif branch_len < 9:
            if random.randint(0, 1) == 0:
                inner_turtle.color("snow")
            else:
                inner_turtle.color("lightcoral")
            inner_turtle.pensize(branch_len / 2)
        else:
            inner_turtle.color("sienna")
            inner_turtle.pensize(branch_len / 10)

        inner_turtle.forward(branch_len)
        angle_elem = 1.5 * random.random()
        inner_turtle.right(20 * angle_elem)
        length_elem = 1.5 * random.random()
        tree(branch_len - 10 * length_elem, inner_turtle)
        inner_turtle.left(40 * angle_elem)
        tree(branch_len - 10 * length_elem, inner_turtle)
        inner_turtle.right(20 * angle_elem)
        inner_turtle.up()
        inner_turtle.backward(branch_len)
        inner_turtle.down()
        
def petal(maxim, inner_turtle):
    for i in range(maxim):
        right_branch = 300 - 500 * random.random()
        left_branch = 30 - 50 * random.random()
        inner_turtle.up()
        inner_turtle.forward(left_branch)
        inner_turtle.left(90)
        inner_turtle.forward(right_branch)
        inner_turtle.down()
        inner_turtle.color("lightcoral")
        inner_turtle.circle(1)
        inner_turtle.up()
        inner_turtle.backward(right_branch)
        inner_turtle.right(90)
        inner_turtle.backward(left_branch)
        
if __name__ == '__main__':
    static_turtle = turtle.Turtle()
    my_frame = turtle.Screen()
    getscreen().tracer(5, 0)
    turtle.screensize(bg="#f3cef5") 
    static_turtle.left(90)
    static_turtle.up()
    static_turtle.backward(150)
    static_turtle.down()
    static_turtle.color("sienna") 
    tree(60, static_turtle)
    petal(100, static_turtle)
    my_frame.exitonclick()

Output:

Leave a Comment