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.
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.
How many chunks do you think we can split this card into?
We should be left with the following:
- The tree’s shape or leaves (3 triangles)
- The tree’s trunk (1 rectangle)
- The baubles on the tree (10 circles)
- The text
- The snowy ground (1 rectangle)
- The Sky (Background)
- 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.
We will also have to import Random. Python has a Random module to help us generate random elements. The Code:
import turtle import random
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.
Can you spot what other shape would we need to use again and again in this card?
Circles! Yes, we use circles for the baubles as well as the snow!
Now lets make a function for triangles.
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)
Yes, it’s For Loops, because we know that we only need 3 triangles to create the tree.
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:
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
and have a very classic 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!
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)]
for position in ball_positions: make_ball(position, position, 20, random.choice(ball_colours), turtle.Turtle()) screen.update()
Lets create two functions, the first is used to move each snow flake down
def move_snow(snow): position = snow.position() snow.clear() make_ball(position, position - snow_speed, snow_size, "white", snow)