SoFunction
Updated on 2024-11-13

Drawing cherry blossom trees with python

The night may be long, but it will always usher in the warmth of the sun, and March is on schedule, and the cherry blossoms of Wudan University are once again in full bloom. So today, let's see how to draw a beautiful cherry blossom tree in python~!

Speaking of drawing with python, that is, of course, the small turtle Turtle library, for what is turtle it, imagine a small turtle, in a horizontal axis for x, vertical axis for y coordinate system origin, (0,0) position to start, it according to a set of function instructions to control, in this plane coordinate system to move, so as to crawl in the path of its graphic drawn.

I. Installation and introduction

Installation is not that difficult just pip install it directly

pip install turtle

It can be introduced using the following 3 methods:

II. Use

The turtle library is also very simple to use, mainly utilizing a few core control codes

(x,y):Jump directly to(x,y)point (in space or time),in order to绘图窗口中心为原point (in space or time),

(d):away from,press forwarddpixels。

(d):Keep the current direction unchanged,Move back.dpixels。

(r,angle):in order torroundedangleangular rotation。

():start to write

():put pen to paper

(angle):turn leftangledegree (angles, temperature etc)。

(angle):turn rightangledegree (angles, temperature etc)。

For example, the code to draw the trunk part of the tree can be written like this

def Tree(branch, t):
 (0.0005)
 if branch > 3:
  if 6 <= branch <= 12: #
   if (0, 2) == 0: # Random drawing
    ('snow') #Set the color
   else:
    ('lightcoral') # Leaf color
   (branch / 3)
  elif branch < 6:
   if (0, 1) == 0:
    ('snow')
   else:
    ('lightcoral') #
   (branch / 2)
  else:
   ('sienna') # Ochre (zhě) color
   (branch / 10) # 6
  (branch)
  a = 1.5 * ()
  (20 * a)
  b = 1.5 * ()
  Tree(branch - 10 * b, t)
  (40 * a)
  Tree(branch - 10 * b, t)
  (20 * a)
  ()
  (branch)
  ()

And the writing part will not have to draw one stroke by one stroke, you can directly use () to write the text you need in the specified position.

write(arg,move=false,align='left',font=('arial',8,'normal'))

#arg - Text that will be written to the Turtle painting screen.

#align (optional) - one of "left", "center" or "right".

#font(selectable)--(fontname、fontsize、fonttype)。

So take a look at the final result!

Full Source:

import turtle as T
import random
import time

# Drawing cherry blossom torsos (60,t) #
def Tree(branch, t):
  (0.0005)
  if branch > 3:
    if 8 <= branch <= 12:
      if (0, 2) == 0:
        ('snow') # White
      else:
        ('lightcoral') # Light coral
      (branch / 3)
    elif branch < 8:
      if (0, 1) == 0:
        ('snow')
      else:
        ('lightcoral') # Light coral
      (branch / 2)
    else:
      ('sienna') # Ochre (zhě) color
      (branch / 10) # 6
    (branch)
    a = 1.5 * ()
    (20 * a)
    b = 1.5 * ()
    Tree(branch - 10 * b, t)
    (40 * a)
    Tree(branch - 10 * b, t)
    (20 * a)
    ()
    (branch)
    ()

# Falling petals
def Petal(m, t):
  for i in range(m):
    a = 200 - 400 * ()
    b = 10 - 20 * ()
    ()
    (b)
    (90)
    (a)
    ()
    ('lightcoral') # Light coral
    (1)
    ()
    (a)
    (90)
    (b)

# Drawing area
t = ()
# Canvas size
w = ()
() # Hide the brushes
().tracer(5, 0)
(bg='wheat') # wheat
(90)
()
(150)
()
('sienna')

# Drawing the trunk of a cherry blossom
Tree(60, t)
# Falling petals
Petal(200, t)
()

Above is the details of drawing cherry blossom trees with python, for more information about python drawing, please pay attention to my other related articles!