Python Snowflake

 

Python Snowflake (Computer Science Christmas Competition)

Below is some code that uses a turtle to draw a snowflake. See if you can edit the code to make a unique snowflake of your own.

Hint: the #screen.bgpic("my_image.jpg") line can be used to add a background picture.

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)
            snowflake(l // 3, d - 1)
            backward(l)
            left(60)

snowflake(200,4)

 

Other codes to try.....

import turtle
import random

# setup the window with a background colour
wn = turtle.Screen()
wn.bgcolor("#EFECCA")
wn.setup(width=250, height=250)

# assign a name to your turtle
turtle = turtle.Turtle()

colors = ["#7D8A2E", "#263248", "#FF8C00", "#F0C600"]

#
def snowflake(size, pensize, x, y):
    """ function that draws a snowflake """
    # turtle.pen(pensize=10)
    turtle.penup()
    turtle.goto(x, y)
    turtle.forward(10*size)
    turtle.left(45)
    turtle.pendown()
    turtle.color(random.choice(colors))

    for i in range(8):
        branch(size)
        turtle.left(45)


# create one branch of the snowflake
def branch(size):
    for i in range(3):
        for i in range(3):
            turtle.forward(10.0*size/3)
            turtle.backward(10.0*size/3)
            turtle.right(45)
        turtle.left(90)
        turtle.backward(10.0*size/3)
        turtle.left(45)
    turtle.right(90)
    turtle.forward(10.0*size)

snowflake(8, 6, 0, 0)

# leave the window open until you click to close
wn.exitonclick()

And another....

 

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)
            snowflake(l // 3, d - 1)
            backward(l)
            left(60)

snowflake(200,4)