“This is the first day of my participation in the Gwen Challenge in November. See details of the event: The last Gwen Challenge in 2021”.

preface

The beginning of winter! Formally entered the winter, but the weather of changsha in these two days, but today the sun is up in the sun, suddenly thought of before painting a video of the cherry blossom trees, while in a good mood then parse it out, hey hey is really quite good, and a code can be random draw a variety of styles, take a look at the beep

Meet the target

Draw cherry trees with Python

Video presentation:

Because a cherry tree to draw for a long time, I will show you a little, of course, there are many kinds of screen is not recorded, you can go to study oh, hee Hee hee

Tools to prepare

Development tool: Pycharm Development environment: PYTHon3.7, Windows11 tool kit: Turtle

Project analysis

The project idea is divided into three parts:

  • Draw deciduous petals of cherry blossoms, falling petals
  • Add branches to the cherry tree
  • Add a painting background to the cherry trees
  • Color drawing selects various styles of color

Draw petal drop function

Determine the number of petals fell, fallen petal number according to the cherry blossom tree branches, and branches number by 15, the bigger the tree drawing out the coordinates of the doha set the petals petals set to (10, 10) to control the size of the brush to move to the designated area, sifting y ahead, turn left 90, x, put pen to paper, draw the petal shape, finish painting so the number of petals are ok, After drawing the shape of the color, after drawing the circle, go back to the starting point, pick up the pen, back x, turn right 90, back Y, start

Def tap_petal (flower) : petal(turtle_obj, flower): petal(flower): petal(flower) X = flower - 4 * flower * random() y = 10-20 * random() # Pick up the pen, y = forward y, left 90, go x, Turtle_obj.penup () Turtle_obj.forward (y) Turtle_obj.left (90) Turtle_obj.forward (x) Turtle_obj.pendown () # Coral Turtle_obj. Pencolor ("lightcoral") # Return to the starting point # Take the pen back x, turn right 90, back y, Backward (x) Backward (90) Backward (y) Backward (y) Backward () backward(y) backward()Copy the code

Draw the branches

To determine the number of branches, the color of the color number selection, first set the default minimum branch length of branches on both sides of the color can look more beautiful, the left side is white, the right side is coral, the probability of branching is set at 0.5, branches can be set to random growth, set by random number

Def draw_tree(turtle_obj, branch, tree_color): def draw_tree(turtle_obj, branch, tree_color): If randint(0, 1) == 0: if randint(0, 1) == 0: Pencolor ("lightcoral") # turtle_obj. elif (branch / 2) 8 <= branch <= 16: If randint(0, 2) == 0: turtle_obj.pencolor("snow") else: Pencolor ("lightcoral") # turtle_obj.pensize(branch / 4) else: # brown turtle_obj.pencolor(tree_color) # twine turtle_obj.pensize(branch / 10) # starting tree length Turtle_obj. forward(branch) # random degree factor Turtle_obj.right (20 * a) # random length factor b = 1.5 * random() # Draw_tree (turtle_obj, branch -10 * b, tree_color) # turtle_obj.left(40 * a) # Draw_tree (turtle_obj, branch -10 * b, Tree_color) # turtle_obj.right(20 * a) # turtle_obj.penup() # End of recurrent_obj.backward (branch) turtle_obj.pendown()Copy the code

Create canvas, add data colors, and set acceleration of lucky lines

def get_screen(width, height, color, speed): Screen_obj = turtle.screen () # canvas size: (width, height) Color screen_obj. Screensize (width, height, bg=color) Tracer (speed) return screen_objCopy the code

Color canvas brush selection:

  • The color of the trunk
  • Brush size
  • Advanced phase prime lattice
  • Create a brush
  • Brush thickness adjustment
  • The choice to start writing
  • The color configuration of the brush
  • Set constant parameter: length of branches, number of fallen flowers
Def trees(tree_num): # color = ['brown', 'tan', 'black'] for j in range(tree_num): Tree_color = color[randint(0, len(color) -1)] # pensize = randint(2, len(color) -1) 5) # forward pixel forward = ((-1) ** pensize) * pensize * randint(20, 50) # back pixel if pensize <= 3: backward = ((-1) ** pensize) * (5 - pensize) * randint(10, 15) else: Backward = pensize * randint(45, 50) # Create a brush turtle_obj = turtle.turtle () # brush thickness turtle_obj.pensize(pensize) # Get a pen, Forward forward, left ninety, backward, Turtle_obj.penup () turtle_obj.forward(forward) turtle_obj.left(90) turtle_obj.backward(backward) turtle_obj.pendown() # brush color: Turtle_obj. Pencolor (tree_color) # branch = pensize * 15 # flowers = branch # draw_tree(turtle_obj, tree_color) Branch, tree_color) # petal(petal, flowers)Copy the code

I am white and white I, a program yuan like to share knowledge ❤️ interested can follow my public account: White and white learning Python [very thank you for your likes, booklets, comments, three links support]