Christmas is approaching and this makes it a great time to write a festive tutorial to help you learn Python. In this tutorial you will learn how to draw a simple Christmas Tree using Python's turtle module. We are going to do this in an efficient way though, using some of Python's tools to avoid having to repeat ourselves.
This tutorial includes both:
— the text which you can read and follow, and
— a video that will allow you to go through the same steps as in the text.
You can use either, or both, depending on your preferred way of learning.
I will assume you are already familiar with Python coding and that you have used the turtle module before. In this tutorial I will use some intermediate level coding too, but I will explain everything that you need to understand so that you can master this tree-making method.
This tutorial uses:
— the turtle module
— function definitions with input parameters
— for loops with two variables in the for loop statement instead of one
Ready to go?
You can start by setting up the scene:
import turtle window = turtle.Screen() window.bgcolor("sky blue") turtle.done()
You have just created and named the Screen() and changed its background colour to sky blue. This seems like an appropriate colour for the sky, as we are going to place this Xmas tree outdoors.
Next, create the Turtle() that will draw the tree:
tree = turtle.Turtle() tree.color("forest green")
Now the fun starts. Our aim in this tutorial is to write tidy and efficient code that is readable. Notice how I called the turtle tree and not t or some other obscure variable name that doesn't tell the person reading the code what this variable does. These are small things which make a huge difference to your code, especially when your code gets longer.
Our Christmas tree will be made up of a number of green triangles drawn in different places and with different sizes that will give us the classic Christmas tree shape.
When we think about writing efficient code, we want to think of what parts of the code are identical, or very similar, so we can avoid repeating code.
Drawing A Tree Segment
So, start by drawing one of the triangular sections of your tree:
tree.setposition(50, -50) tree.setposition(-50, -50) tree.setposition(0, 0)
You have just drawn a triangle by choosing the three vertices of the triangle. Remember that the turtle starts off in the (0, 0) position, where x=0 and y=0. The tree turtle first moves to the bottom right when x=50 and y=-50. Then it goes left to the other end, where x=-50, and finally it goes back to the top vertex where it started at (0, 0) again.
Next, you can fill in this shape by 'sandwiching' the lines you have just written with begin_fill() and end_fill():
tree.begin_fill() tree.setposition(50, -50) tree.setposition(-50, -50) tree.setposition(0, 0) tree.end_fill()
You should also add a penup() when you first create the tree turtle to make sure that when we move the turtle around from now on, it doesn't draw a line. Since we are filling in the shape, the lines are no longer needed.
This is the code that makes one segment of the tree. Since you will need more than one segment to make a Christmas tree, you are going to have to repeat this code, or something very similar to it.
This is a perfect time to create your own function, or your own command, that draws a tree segment. To create a function, we define the function name and what the function does:
def make_tree_segment(): tree.begin_fill() tree.setposition(50, -50) tree.setposition(-50, -50) tree.setposition(0, 0) tree.end_fill() make_tree_segment()
Remember also that when you define a function using def, the code will not run until you call the function, which is what you are doing on the last line of the code above. You are using the function name followed by brackets on a line that has no indent. This tells the program to run the function.
This is good, as it means that whenever you want to create a tree segment you can use this function and you don't need to repeat any code. Remember DRY = Don't Repeat Yourself. Or to put it another way, we programmers like to be lazy!
Making The Function More Flexible
However, you don't want the tree segments to all be in the same place and to have the same size. So let's start improving our function by changing the size of the triangle:
def make_tree_segment(size): tree.begin_fill() tree.setposition(size, -size) tree.setposition(-size, -size) tree.setposition(0, 0) tree.end_fill()
In the line in which you define the function name, you have now added the parameter size in the brackets. You are telling the computer program that when you use, or call, this function, you will be passing on some extra information that you will put in the brackets. To call the function you will now need to write:
Notice that when you call the function you have used the number 50. When the program runs the function, wherever there is the variable size in the function definition, this will be replaced with the number 50. Try calling the function with different numbers to see what happens. You can now create tree segments of any size you want.
The next thing you want to be able to control is the position of the tree segment. You are likely to always want these to be in the middle of the image horizontally, so all you need to be able to change is the vertical position, or the y-value.
At the moment, the starting position of the turtle is at y=0, since that's where the turtle starts off when it is created. You can therefore add another parameter to your function definition:
def make_tree_segment(size, top_position): tree.begin_fill() tree.setposition(0, top_position) tree.setposition(size, top_position - size) tree.setposition(-size, top_position - size) tree.setposition(0, top_position) tree.end_fill()
The parameter top_position is the y value for the top of the triangle for the tree segment you wish to draw. All the setpositions now also include the parameter top_position. Notice how it is being used. If top_position is 0, then this will give you exactly the same numbers you had before. But you can now also put a different value that will shift all the points up or down.
You may also have noticed that we added another setposition at the top since we want to put the tree turtle at the correct starting position before we start drawing the triangle.
To call this function you will now need two numbers in the brackets, the first showing the size of the tree segment, and the second the y-value for the top of the triangle:
You can now draw a tree segment wherever you want, and whatever size you want it to be.
Finishing Off The Christmas Tree
You're almost done. One way of creating the Christmas tree is to call this function a number of times with different values for the size and the position of the segments. To get a classic shape, each segment needs to be bigger as you move down, for example:
make_tree_segment(50, 20) make_tree_segment(80, 0) make_tree_segment(120, -30) make_tree_segment(150, -60)
But I promised you we're going to use practices that make our coding really efficient. Even if in this case, calling this function four times is not too bad, let's look at a way of making this even more professional-looking, ready for when you need to repeat something like this more than four times.
You can use a for loop to repeat the function call four times, but you will need to find a way to use different numbers each time. And you want to be able to control what these numbers are so that you can make your Christmas tree look exactly the way you want it to.
This next part uses some more intermediate tools in Python, so don't worry if for now you don't fully understand every small detail of what's going on.
Start by creating a variable that has several pairs of numbers:
tree_segments = ((50, 20), (80, 0), (120, -30), (150, -60))
This may look a bit strange initially, but look closely and you'll see that there are four pairs of numbers. Each pair includes the the size of a segment and the position of the top part of the triangle, in that order. You can see that these are the same numbers you used earlier when you called the make_tree_segment function four times. The structure we have stored in tree_segments is called a tuple in Python, and inside this tuple are four more tuples.
You can now use a for loop, although you'll use a for loop in a manner that may be a bit different to how you're used to:
for size, top_position in tree_segments: make_tree_segment(size, top)
The loop asks the program to repeat as many times as there are items in tree_segments. Since tree_segments has four pairs of numbers, the for loop will repeat four times. The for loop statement is also saying that each time the loop repeats, size should be equal to the first number in the pair, and top_position should be equal to the second number.
So, the first time the loop runs, size=50 and top_position=20. Therefore the function that will be called is make_tree_segment(50, 20). When the loop repeats the second time however, size=80 and top_position=0, so the function will be called with different numbers and therefore a tree segment of a different size and in a different position will be drawn.
And you now almost have the Christmas tree ready. In the video above, you can see me go through how we build this program, and I also add a trunk for the Christmas tree.
Try it out, and then you can add your own finishing touches. Let me know how it goes in the comments below!
Merry Christmas, and enjoy coding in Python during the Christmas holidays.
Enjoyed this and want to learn more Python for free? Check out our sister site The Python Coding Book, for in-depth guides on all things Python, or follow us on Twitter @codetoday_ and @s_gruppetta_ct