Creating Your Own
Python Holiday Greeting Card

Holiday Greeting Card

As you know the holiday season is coming up and we are all trying to find that perfect card
to send to our friends and family. Instead of purchasing a card, why not make your own!

To the parents:

It is often hard to see the immediate implications of learning Computer Science concepts like Variables, Loops and Functions. However, these core concepts are the building blocks of almost every piece of code and what better way to show case the power of these concepts than a beautifully animated holiday greeting card?

Share this article with your family and give them the opportunity to have fun and be creative whiles following along this quick refresher on the fundamental concepts of Computer Science.

Share on facebook
Facebook
Share on linkedin
LinkedIn
Share on whatsapp
WhatsApp
Share on email
Email

Tutorial:
How to make an Animated
Holiday Greeting Card

Decomposition is when you break down problems into smaller sub-problems (chunks) so that they are easier to solve. If we were to breakdown this card into smaller chunks, it would be a lot easier to complete. 

Question:
How many chunks do you think we can split this card into?

Answer:
We should be left with the following:

  1. The tree’s shape or leaves (3 triangles)
  2. The tree’s trunk (1 rectangle)
  3. The baubles on the tree (10 circles)
  4. The text
  5. The snowy ground (1 rectangle)
  6. The Sky (Background)
  7. The Snow (a lot of small circles)

We now have 7 different elements, however we can also take each element and further break it down. For example, the 3 triangles take make up the three leaves, you may ask, “How do we draw a triangle?”.

1. Create the triangle outlines, 2. Fill in the triangle.

Now we have outlined our project, lets start! First thing we have to do is import Turtle. Turtle is a module in Python that allows you to draw. You can draw using lines, dots, curves and lots of colours! Here are some examples:
Italian Trulli Italian TrulliItalian Trulli
We will also have to import Random. Python has a Random module to help us generate random elements. The Code:
import turtle
import random

The Code:
snow_size = 4
snow_speed = 3
draw_speed = 10
rate_of_snow_balls = 6
width = 600
height = 600
screen = turtle.Screen()

Let’s start making the card already! First, let’s create the tree leaves, the 3 triangles. Unfortunately, Python’s Turtle does not know how to make triangles on its own, so we have to teach it how to. Also, because we will be using multiple triangles, we will use a Function.

A function is a piece of code we can use again and again.

Question:
Can you spot what other shape would we need to use again and again in this card?

Answer:
Circles! Yes, we use circles for the baubles as well as the snow!

Now lets make a function for triangles.

The Code:

def make_triangle(x, y, size, outline, triangle):
        triangle.hideturtle()
        triangle.penup()
        triangle.setposition(x, y)
        triangle.pensize(3)
        if outline:
        triangle.pendown()
        if not outline:
        triangle.fillcolor("forest green")
        triangle.begin_fill()
        triangle.setposition(x + size, y - size)
        triangle.setposition(x - size, y - size)
        triangle.setposition(x, y)
        if not outline:
            triangle.end_fill()

 

If you are attempting the extra challenges, lets create a function for circles as well (Optional):

def make_ball(x, y, size, colour, ball):
        ball.hideturtle()
        ball.penup()
        ball.setposition(x, y)
        ball.color(colour)
        ball.dot(size)

Now we have a variables and functions set, we have to tell our code, where the pieces should go. Since we have 3 triangles, we should use a loop, so we do not need to repeat or code 3 times.

  •  A quick recap on Loops.

A loop is a program that repeats a sequence of instructions until a specific condition is met.

Question:
What kind of loop do you think we should use for the 3 triangles?

Answer:
Yes, it’s For Loops, because we know that we only need 3 triangles to create the tree.

The Code:

triangle_1 = turtle.Turtle()
triangle_1.speed(draw_speed)
outline = True
for repeat in range(2):
    make_triangle(0, width / 3, width / 6, outline, triangle_1)
    make_triangle(0, width / 4, width / 4, outline, triangle_1)
    make_triangle(0, width / 8, width / 3, outline, triangle_1)
    outline = False
screen.tracer(0)

To make the scene more alive we need to fill it with extra elements. We can start by drawing two simple rectangles, one smaller brown rectangle just under the tree leaves would be our tree trunk, and another larger white rectangle will make it appear as if the tree is on a snowy ground.

The Code: Snowy ground and Tree Trunk

stem = turtle.Turtle()
# white snowy ground
stem.penup()
stem.hideturtle()
stem.setposition(-width, -width / 3)
stem.color("white")
stem.begin_fill()
stem.setposition(width, -width / 3)
stem.setposition(width, -width / 2)
stem.setposition(-width, -width / 2)
stem.end_fill()
screen.update()
# tree trunk
stem.color("brown")
stem.setposition(-width / 30, -width / 4.8)
screen.tracer(1)
stem.pendown()
stem.begin_fill()
stem.setposition(width / 30, -width / 4.8)
stem.setposition(width / 30, -3 * width / 8)
stem.setposition(-width / 30, -3 * width / 8)
stem.setposition(-width / 30, -width / 4.8)
stem.end_fill()

The Code: Sky
We could also add a background colour for our sky:

screen.bgcolor("sky blue")

The Code: Message
Finally, our message “Happy Holidays” could be added, and of corse you can replace this with any message you would like!

text_1 = turtle.Turtle()
text_1.hideturtle()
text_1.penup()
text_1.setposition(0, 222)
text_1.color("white")
text_1.write("Happy Holidays",
             font=("Apple Chancery", 15, "bold"),
             align="center")

At this moment, you could complete the project by putting down


turtle.done()

and have a very classic holiday greeting card

Basic Holiday Greeting Card
However, you would like to further animate your card or take on a new challenge, you can try out Steps 7 and 8!

  • For the baubles we have already made a reusable function that can make a ball given the position, size, colour and the pen (turtle). We can then make two lists, one for colours we want to use for baubles and one for positions on the tree
  • We can then make two lists, one for colours we want to use for baubles and one for positions on the tree
The Code:
screen.tracer(2)
ball_colours = ["yellow", "red", "teal", "gold", "violet", "white"]
ball_positions = [(-20, 150), (45, 120), (-30, 100), (20, 66), (50, 20),
                  (120, -80), (-66, -30), (75, -40), (0, -100), (-100, -100)]

    • After which we could loop through the positions and use the bauble function with a random colour from the list to draw each bauble.
The Code:
for position in ball_positions:
        make_ball(position[0], position[1], 20, random.choice(ball_colours),
              turtle.Turtle())
        screen.update()
    • And now we have these beautiful randomly generated baubles decorating our tree!

Bauble Tree

Lets create two functions, the first is used to move each snow flake down

The Code:

def move_snow(snow):
    position = snow.position()
    snow.clear()
make_ball(position[0], position[1] - snow_speed, snow_size, "white", snow)
      • The second function creates the snowflakes at the top of the card. The function then keeps a track of the snow in a list whiles using the first function to move the snow as needed.
        If any of the snow moves past the base of the screen the snowflake is deleted from the list of ‘alive’ snowflakes and a new one is created. Now you have a looping snow generator!

    The Code:

    def snow_fall():
        rand_make_snow = random.randint(0, rate_of_snow_balls)
        if rand_make_snow == 0:
            snow = turtle.Turtle()
            snow.hideturtle()
            snow.penup()
            list_of_snow.append(snow)
            make_ball(random.randint(-width / 2, width / 2), width / 2, snow_size,
                      "white", snow)
        for snow in list_of_snow:
            move_snow(snow)
            if snow.position()[1] <= -width / 2:
                snow.clear()
                list_of_snow.remove(snow)
                del snow
    screen.update()
          • Now with the functions set up, you can just add them into your card by adding the following at the end of your code

    The Code:

    list_of_snow = []
    while True:
        snow_fall()

     

          • and now when you run your code, you have now created your own animated Python Holiday Greeting Card!


          Snowing Bauble Tree
          Congratulations and happy holidays from RoboBrain! You can now send this card to your family and friends and spread the holiday sprit.