Sitemap
Jiang

Jiang

Python Snowflake Code

3 min readNov 30, 2023

--

baby turtle

Let’s learn about turtle before we draw cherry trees in python!

Get Jiang’s stories in your inbox

Join Medium for free to get updates from this writer.

Turtle is an important package (built-in package) for drawing in Python, it contains rich drawing tools and various functions for drawing, when you learn to draw with Turtle, you can draw any pattern you want.

  1. 1 Turtle drawing board

The size of Turtle’s drawing board can be set using the turtle.screensize() function.

turtle.screensize(width,height,bg)

Set the size of the panel, including width and height, width is width, height is height, bg is canvas color.

  1. 2 Turtle’s brushes
    Turtle’s brushes have several commonly used functions:

①turtle.penup(): lift the brush, move the brush will not leave traces on the canvas Oh!
② turtle.pendown (): put down the brush, and turtle.penup corresponds to, put down the brush can continue to draw (put down the brush after painting will leave traces on the canvas)
③turtle.pensize(): control the size of the brush (you can define the size of the brush according to the needs of their own Oh)
④turtle.pencolor(): control the color of the brush (you can check all the colors available in python online, there are many colors available in python)
⑤turtle.hideturtle(): hide the brush (the brush won’t be visible when you draw later)

  1. 3 Turtle drawing
    In the process of drawing diagrams, we often have to use some simple move functions:

①turtle.forward(x): move the brush forward x pixels (x can be interpreted as distance)
② turtle.backward(x): move the brush back x pixels (x can be interpreted as the distance)
③turtle.left(n): rotate the brush n degrees to the left
④turtle.right(n): rotate the brush to the right by n degrees
⑤turtle.speed(): set the speed of brush drawing (1~10 increment, 0 is the fastest)

  1. 4 Turtle color filling
    After drawing a good picture, we often need to fill in the color, you can use turtle.fillcolor () function, write the color you want to fill in the parentheses can be.
    In the use of turtle.fillcolor () function to pay attention to its basic format:

turtle.beginfill() #Start the fill
turtle.fillcolor() #Enter the color of the fill.
turtle.endfill() #End the fill.

  1. 5 Turtle writing
    After completing the entire drawing, we can write using the turtle.write() function
turtle.write(" ",move,align,font)

① Fill in the word to be written in double quotes in the first position.
② move (optional): by default, move is false, if move is true, then the pen will move to the lower right corner
③ align (optional): the value can be left that is, left, center that is, right that is, one of the right, is a string format
④ font (optional): font triad (fontname, fontsize, fonttype), fontname is the name of the font (string format, such as “Song”), fontsize is the size of the font), fonttype is the type of font such as: normal (normal), bold (bold), italic (italic)

snowflakes

class Snow():    #snowflakes
def __init__(self):
self.r = 6 #Radius of a snowflake
self.x = ra.randint(-1000,1000) #Horizontal coordinates of snowflakes
self.y = ra.randint(-500,500) #Vertical coordinates of snowflakes
self.f = ra.uniform(-3.14,3.14) #Snowflakes move left and right as a sine function
self.speed = ra.randint(5,10) #Snowflake movement speed
self.color = ra.choice(colors) #The color of snowflakes
self.outline = 5 #Size of snowflakes

Paint a snowflake

    def snow(self):                #Paint each snowflake
x=self.r #Radius of a snowflake
t.pensize(self.outline) #Size of snowflakes
t.penup() #take up one's pen
t.goto(self.x,self.y) #random location
t.pendown() #put pen to paper
t.color(self.color) #snow color
for i in range(6): #Circular drawing of six snowflake petals
t.forward(x*5)
t.backward(x*2)
t.left(60)
t.forward(x*2)
t.backward(x*2)
t.right(120)
t.forward(x*2)
t.backward(x*2)
t.left(60)
t.backward(x*3)
t.right(60)

transfer function

  def move(self):                    #Snowflake shift function
if self.y >= -500: #While the snowflakes are still in the canvas
self.y -= self.speed #Setting the up and down movement speed
self.x -= self.speed * math.sin(self.f) #Setting the left and right movement speed
self.f -= 0.1 #It can be interpreted as a flag to change the direction of left and right movement
else: #When the snowflake has drifted off the canvas, regenerate a snowflake
self.r = 6
self.x = ra.randint(-1000,1000)
self.y = 500
self.f = ra.uniform(-3.14,3.14)
self.speed = ra.randint(5,10)
self.color = ra.choice(colors)
self.outline = 5

--

--