Animated Christmas Card with Python Turtle

Animated Christmas Card

Create Animated Christmas Card with Python Turtle

This Christmas, why not send a card that is both festive and fun? Or why not show your friends and family how much you love them with an animated card created all by yourself through an animation design course? Here is a tutorial to create an animated Christmas card with python Turtle to add some fun to your holidays.

The Animated Christmas Card

So before we start let’s see how our animated Christmas card will look like-

Animated Christmas Card with Python Turtle
Animated Christmas Card with Python Turtle

Prerequisite

We will use Python Turtle to make this card. The turtle module is a python module that lets you draw interesting shapes and build games. Refer Turtle Documentation to know more about the python turtle module.

So let’s first see the steps to create the card.

Steps to create an Animated Christmas Card with Python Turtle

  1. Import the modules required
  2. Do the turtle screen setup for card body
  3. Add an image of a Christmas tree on the turtle screen
  4. Write a greeting message
  5. Draw Santa hat
  6. Build snowflakes
  7. Animate the snowfall with snowflakes
  8. Animate the background color change of the Christmas card

Understood the steps?

So now let’s get started.

Code to create an Animated Christmas Card

Import the modules required

We will need a turtle and random module to create this animated Christmas card.

So let’s import these modules-

import turtle, random

Do the turtle screen setup for card body

Let’s now create the turtle screen which will be the card body.

card=turtle.Screen()

Setup the card size to full screen-

card.setup(1.0,1.0)

Also let’s give a title to the card-

card.title("Christmas Greetings!")

As you must have observed the card has a random background color. So for this, we will make a list of our favorite colors and choose a random color from the list-

colors=['#92b6f0','#d95d78','#5cdbb5','#5ccde0','#e0d758','#ed9277']

These are my favorite ones but you can choose your favorite colors too.

Now let’s set the background color using the choice function from random module-

card.bgcolor(random.choice(colors))

The card body is now ready. Let’s move to the next step.

Add an image of a Christmas tree on the turtle screen

Here is the python code-

tree=turtle.Turtle()
turtle.register_shape("xmas.gif")
tree.shape("xmas.gif")
tree.pu()
tree.goto(100,0)

We have used the Turtle module to create an image of a Christmas tree.

  • The first line creates a new turtle object.
  • The second line tells Python that we are going to use a custom image for our Christmas tree, and the file name is “xmas.gif”.
  • The third line tells Python to use this image as the shape of the turtle.
  • The fourth line tells Python to put the pen up as we want to move the turtle without drawing anything.
  • The fifth line tells python to go to (100,0) position and this is where we want to place our tree.

With this, our Christmas tree is complete! Add turtle.done() at the end of the code and execute your python file to check the card.

This is how our card should look now-

Card with random color and Christmas tree
Card with random color and Christmas tree

Let’s now move on to the next step.

Write a greeting message

To write a greeting message we need a pen. So let’s first create another turtle object for pen-

pen=turtle.Turtle()

We will hide this turtle from the screen as we don’t want it to be visible while writing-

pen.hideturtle()

Now let’s set its color to black and move it to a position where we want to display the greeting message-

pen.color("black")
pen.pu()
pen.setx(-500)

So the pen is ready. Now let’s write the greeting message-

pen.write("Merry\nChristmas!!",font=("ravie",40,"italic"),align="left")
pen.setheading(-90)
pen.fd(100)
pen.write("TO ALL OF YOU",font=("Courier",30,"bold"),align="left")

Here-

  • The first line writes the texts “Merry” and “Christmas!” in separate lines. We use ‘\n’ to show line break. We used font called “ravie” of size 40 with italic style for the text.
  • The second line sets the pen to point downward.
  • The third line moves the pen ahead in the downward direction by 100px.
  • The fourth line writes the text “TO ALL OF YOU” with a font called “Courier” of size 30 and makes it bold.

And our greeting message is ready! Here’s how our card is looking now-

Card with text written with turtle object
Christmas Card with text written with turtle object

Let’s now move to the next step.

Draw Santa hat

Firstly, we will position the pen where we want to draw the Santa hat-

pen.setheading(0)
pen.fd(360)
pen.left(145)
pen.fd(40)

Here-

  • The first line sets the pen to point straight.
  • The second line moves the pen ahead by 360px in horizontal direction.
  • The third line sets the turtle to left side of the screen with a distance of 145px.
  • The fourth line moves the pen ahead by 40px in horizontal direction.

Now let’s define a function called drawHat() which will draw the Santa hat on turtle screen-

def draw drawHat():
    pen.color('red')
    pen.fillcolor('red')
    pen.begin_fill()
    for i in range(3):
        pen.fd(50)
        pen.right(120)
    pen.end_fill()

Here we are drawing a triangle filled with red color.

  • The first line of function says that we are using red color.
  • The second line tells Python to fill the shape with a ‘red’ color.
  • With pen.begin_fill(), the pen will start drawing the shape and filling it with the red color.
  • For loop will draw a triangle.
  • pen.end_fill() ends the color fill operation.

Also let’s add 6 white-colored small pom-poms at the base of the hat using a for loop –

    for i in range(6):
        pen.dot(10,'white')
        pen.fd(10)

Let’s also add a big pom pom at the top of the Santa hat-

pen.right(130)
pen.fd(55)
pen.dot(10,'white')

Now that we have defined the function let’s call it to draw the Santa Hat-

drawHat()

Look how cute the Santa hat is looking on the card-

Card with Santa Hat
Christmas Card with Santa Hat

Build Snowflakes

Its’ snow time!

Let’s first add the following step-

card.tracer(False)

This will hide the turtle trace so we will not see the steps of drawing each snow.

Let’s now make an empty list to store snowflakes-

snowlist=[]

Let’s create a function to draw the snow on the screen-

def makeSnow():
    for i in range(50):
        snow=turtle.Turtle()
        snow.pu()
        snow.color("white")
        snow.shape("circle")
        snow.speed(0)
        snow.goto(random.randint(-700,700),random.randint(-700,700))
        snow.dot(7,'white')
        snowlist.append(snow)

This function will create 50 white snow circles scattered across the screen.

  • The first line of the function- snow=turtle.Turtle() creates a new turtle object.
  • The next three lines tell Python to put the pen up, set the color to white, and shape to a circle.
  • With speed(0) we are telling python that the snow turtle will move with the fastest speed.
  • With snow.goto(random.randint(-700,700),random.randint(-700,700)) we ask Python to move the snow turtle randomly. This will move the turtle inside a window ranging from -700px and 700px at a random position.
  • We then tell python to put a dot at each of these random positions with white color using snow.dot(7,'white') function.
  • The last line appends the created snow object to the list snowlist.

Now let’s call this function to draw all the snowflakes on our card-

makeSnow()

Let’s check our card-

Card with snowflakes
Christmas Card with snowflakes

Wow! It’s starting to look like a real winter wonderland scene!

Animate the snowfall with snowflakes

Let’s add a nice snowfall animation to finish our card!

To do that we will first define a function as follows-

def snowfall():
    for i in snowlist:
        i.goto(random.randint(-700,700),random.randint(-700,700))
        i.dot(7,'white')

This function will iterate through all the snow objects in our list and move them to a new random position.

The dot(7,'white') will add a white dot at each new position to add those tiny snowflakes.

And now let’s call this function in a forever loop to generate the snowfall animation-

while True:
    snowfall()

Animate the background color change of the Christmas card

Finally, we want the background color of our card to keep on changing.

So add the following step in your while loop-

card.bgcolor(random.choice(colors))

And our animated Christmas card is finally ready!

Here’s how the Animated Christmas Card looks-

Animated Christmas Card Screenshot
Animated Christmas Card Screenshot

Isn’t that a beautiful animated Christmas card? We made it using a python turtle! Hope you enjoyed making it.

Want to explore more fun projects with python turtle? Then at SkoolofCode, we offer coding classes for kids that are project-based learning modules where students use live code to find logical and inventive solutions to problems. So, why wait and Book a FREE trial class today. Check out the Turtle Race game tutorial, a simple and fun game built with a python turtle.

Merry Christmas and happy holidays!

Source Code for Animated Christmas Card with Python Turtle

Here’s the final source code to create the animated Christmas Card with python turtle-

import turtle,random

def drawHat():
    pen.color('red')
    pen.fillcolor('red')
    pen.begin_fill()
    for i in range(3):
        pen.fd(50)
        pen.right(120)
    pen.end_fill()
    for i in range(6):
        pen.dot(10,'white')
        pen.fd(10)
    pen.right(130)
    pen.fd(55)
    pen.dot(10,'white')

def makeSnow():
    for i in range(50):
        snow=turtle.Turtle()
        snow.pu()
        snow.color("white")
        snow.shape("circle")
        snow.speed(0)
        snow.goto(random.randint(-700,700),random.randint(-700,700))
        snow.dot(7,'white')
        snowlist.append(snow)

def snowfall():
    for i in snowlist:
        i.goto(random.randint(-700,700),random.randint(-700,700))
        i.dot(7,'white')

card=turtle.Screen()
card.setup(1.0,1.0,0,0)
card.title("Christmas Greetings!")

colors=['#92b6f0','#d95d78','#5cdbb5','#5ccde0','#e0d758','#ed9277']
card.bgcolor(random.choice(colors))

tree=turtle.Turtle()
turtle.register_shape("xmas.gif")
tree.shape("xmas.gif")
tree.pu()
tree.goto(100,0)  

pen=turtle.Turtle()
pen.hideturtle()
pen.color("black")
pen.pu()
pen.setx(-500)

pen.write("Merry\nChristmas!!",font=("ravie",40,"italic"),align="left")
pen.setheading(-90)
pen.fd(100)
pen.write("TO ALL OF YOU",font=("Courier",30,"bold"),align="left")

pen.setheading(0)
pen.fd(360)
pen.left(145)
pen.fd(40)
drawHat()

card.tracer(False)
snowlist=[]
makeSnow()
card.tracer(True)
while True:
    snowfall()
    card.bgcolor(random.choice(colors))