© 2018 - Analytics Link

Draw a Christmas Snowflake with Python & Turtle

December 12, 2018

I recently wrote about drawing Fractal Tree's in Python.  To do this, we used Turtle which is a graphics module within Python (it comes standard in all versions).  With it being Christmas, I thought I'd draw something a little more festive...

 

 

 The code for the snowflake is below:

 

import turtle

from turtle import *

 

def snowflake(l, d):
    screen = turtle.Screen()
    #screen.bgpic("my_image.jpg")
    screen.bgcolor("black")
    
    if d > 0:
        for i in range(6):
            speed("fastest")
            color("silver")
            width(5)
            forward(l)
            drawFlake(l // 3, d - 1)
            backward(l)
            left(60)

   

snowflake(200,4)

 

I'm not sure exactly why I need to import turtle, and import * from turtle, but it was giving me errors when I didn't so I just went with it...

 

Here, I've just used a background colour of black, but you can see in the commented out code above that you could also use an image as your background.

 

Even at a speed of 'fastest' it can take quite a while (I think about 2mins for this one to draw).  If you really want to speed things up try adding turtle.tracer(0, 0) and turtle.update() as per the below code.  It gives it a huge boost...

 

def snowflake(l, d):
    screen = turtle.Screen()
    #screen.bgpic("my_image.jpg")
    screen.bgcolor("black")
    turtle.tracer(0, 0)
    
    if d > 0:
        for i in range(6):
            speed("fastest")
            color("silver")
            width(5)
            forward(l)
            drawFlake(l // 3, d - 1)
            backward(l)
            left(60)
    turtle.update()

  

snowflake(200,4)

 

I hope you have fun playing around with the code!

 

 

 

 

Share on Facebook
Please reload

Please reload

Recent Posts