Day 21 - Infinite Snowflakes Using Custom Functions and Random

We'll create a program that generates snowflakes with randomized color, size and placement. Each snowflake will carry a high level of detail, but we will keep the program simple through the use of nested custom functions.

Starter Code

# def makeDiamond():
#     left(30)
#     forward(flScale * 5)
#     right(60)
#     forward(flScale * 5)
#     right(60)
#     forward(flScale*2)
#     right(60)
#     forward(flScale * 5)
#     right(60)
#     forward(flScale * 5)
#     left(30)
#
# def makeEnd():
#     forward(20*flScale)
#     for x in range(5):
#         left(120)
#         forward(5*flScale)
#         makeDiamond()
#         forward(5 * flScale)
#     left(120)
#     forward(20 * flScale)
#
# def makeShortBump():
#     left(90)
#     forward(5*flScale)
#     right(90)
#     forward(2*flScale)
#     right(90)
#     forward(5 * flScale)
#     left(90)
#
# def makeArm():
#     forward(20*flScale)
#     makeShortBump()
#     makeEnd()
#     makeShortBump()
#     forward(20 * flScale)

Finished Code

from turtle import *
import random


def makeDiamond():
    left(30)
    forward(flScale * 5)
    right(60)
    forward(flScale * 5)
    right(60)
    forward(flScale*2)
    right(60)
    forward(flScale * 5)
    right(60)
    forward(flScale * 5)
    left(30)



def makeEnd():
    forward(20*flScale)
    for x in range(5):
        left(120)
        forward(5*flScale)
        makeDiamond()
        forward(5 * flScale)
    left(120)
    forward(20 * flScale)


def makeShortBump():
    left(90)
    forward(5*flScale)
    right(90)
    forward(2*flScale)
    right(90)
    forward(5 * flScale)
    left(90)

def makeArm():
    forward(20*flScale)
    makeShortBump()
    makeEnd()
    makeShortBump()
    forward(20 * flScale)

while True:
    flScale = random.randint(1,9) / 3
    if flScale > 0 and flScale < 1.5:
        strColor = "alice blue"
    elif flScale > 1.4 and flScale < 2:
        strColor = "light cyan"
    else:
        strColor = "pale turquoise"

    color(strColor)
    intArms = 6
    flAngle = 360/intArms + 180

    xLoc = random.randint(-400, 400)
    yLoc = random.randint(-400, 400)

    teleport(xLoc,yLoc)
    print(xLoc,yLoc)

    setheading(random.randint(1,360))

    begin_fill()
    for x in range(0,intArms):
        makeArm()
        right(flAngle)
    end_fill()

done()