Kodewick
Start learning →

Python Turtle Animated Christmas Tree

Blinking lights, falling snow and a twinkling star. It is an animation: drawn frame by frame with tracer and ontimer. Click a number to change it, then take it with you.

Python Turtle Animated Christmas Tree
The full code (105 lines)
import turtle
import random

screen = turtle.Screen()
screen.setup(600, 600)
screen.bgcolor("midnightblue")
screen.tracer(0)
# two turtles: tree draws the tree (once), pen draws what moves (wiped and drawn again every frame)
tree = turtle.Turtle()
tree.hideturtle()
pen = turtle.Turtle()
pen.hideturtle()
pen.penup()


def triangle(bottom, width, color):
    # one layer of leaves: its bottom edge at `bottom`, `width` wide
    tree.penup()
    tree.goto(-width / 2, bottom)
    tree.pendown()
    tree.color(color)
    tree.begin_fill()
    tree.goto(width / 2, bottom)
    tree.goto(0, bottom + width * 0.6)
    tree.goto(-width / 2, bottom)
    tree.end_fill()


# the trunk
tree.penup()
tree.goto(-22, -210)
tree.color("saddlebrown")
tree.begin_fill()
for side in [44, 60, 44, 60]:
    tree.forward(side)
    tree.left(90)
tree.end_fill()

# three layers, each smaller than the one below
layers = 3
for i in range(layers):
    triangle(-150 + i * 95, 340 - i * 85, "forestgreen")

# where the lights are: hung inside each layer
lights = []
for i in range(layers):
    bottom = -150 + i * 95
    width = 340 - i * 85
    for k in range(8):
        h = random.uniform(10, width * 0.6 - 12)
        half = width / 2 * (1 - h / (width * 0.6))
        lights.append([random.uniform(-half + 8, half - 8), bottom + h])

# each snowflake: [x, y, size, how far it falls each frame]
snow = []
for i in range(60):
    snow.append([random.randint(-300, 300), random.randint(-300, 300), random.randint(3, 6), random.uniform(1, 3)])

colors = ["red", "gold", "deepskyblue", "orange", "violet"]
top = -150 + (layers - 1) * 95 + (340 - (layers - 1) * 85) * 0.6
frame = 0


def star(size, color):
    # the star on top
    pen.goto(-size / 2, top - 6)
    pen.setheading(0)
    pen.color(color)
    pen.begin_fill()
    for i in range(5):
        pen.forward(size)
        pen.right(144)
    pen.end_fill()


def step():
    global frame
    pen.clear()
    # the lights: every 6 frames each one takes the next color
    for i in range(len(lights)):
        pen.goto(lights[i][0], lights[i][1])
        pen.dot(12, colors[(i + frame // 6) % len(colors)])
    # the star twinkles: bigger, then smaller
    if frame // 10 % 2 == 0:
        star(36, "gold")
    else:
        star(42, "yellow")
    # the snow falls; at the bottom a flake goes back to the top
    for s in snow:
        s[1] -= s[3]
        if s[1] < -300:
            s[1] = 300
        pen.goto(s[0], s[1])
        pen.dot(s[2], "white")
    pen.goto(0, -275)
    pen.color("white")
    pen.write("Merry Christmas!", align="center", font=("Arial", 28, "bold"))
    screen.update()
    frame += 1
    if frame < 300:
        screen.ontimer(step, 50)


step()
turtle.done()

How it is drawn

The tree is drawn once and what moves is drawn again every frame, so there are two turtles: tree draws the trunk and the three layers; pen draws the lights, the star and the snow. Each frame pen.clear() wipes only what pen drew, and the tree stays.

An animation is drawn one frame at a time: screen.tracer(0) turns automatic updates off, the whole frame is drawn, and screen.update() shows it all at once. screen.ontimer(step, 50) calls step again 50 milliseconds later: 20 frames a second.

The lights blink because of colors[(i + frame // 6) % len(colors)]: frame // 6 goes up by one every 6 frames, so each light takes the next color in the list; % wraps around to the first color at the end.

Each snowflake is a list [x, y, size, how far it falls each frame]. Every frame its y goes down a little; below -300 it is put back at the top (s[1] = 300), so the snow never runs out.

Questions

How do I change the greeting or add a name?

Click Edit, change the text inside write(...) in the playground and run it again.

Does it run on my own computer?

Yes. With Python 3 installed, save it as tree.py and run python tree.py: a window opens and plays it (turtle comes with Python). The last line, turtle.done(), keeps the window open.

How do I make it blink forever?

The line if frame < 300 stops the animation after 300 frames (about 15 seconds). On your own computer, remove that if and call screen.ontimer(step, 50) every time: it then plays until you close the window. On this page an animation plays for 30 seconds at most.

Why tracer(0) and update()?

Otherwise the screen redraws after every single dot: the picture flickers and crawls. With updates off, a whole frame is drawn and shown at once, so the animation is smooth.

Try another