Lesson 2.2: Turtle Programming
In Lesson 2.1, we learned how to command Turtle from inside Python Shell. It is really cool to send instructions one-by-one and see them executed right in front of your eyes. But, as you may have already realized, typing instructions one-by-one is slow and impractical. If you make a mistake, you need to start from the beginning; if you want to show your drawings to another person, you need to retype (or at least copy-and-paste) everything one more time.
Thankfully, there is a better way of doing things – instead of typing instructions directly into Python Shell, we can code an entire file of instructions and send it to Python Shell. We can execute the same program over and over, we can tweak, modify, and execute it again, or we can write one set of instructions and reuse them in several different programs!
In this lesson, we will combine Python programming (introduced in Lesson 1.3) and the Turtle module (introduced in Lesson 2.1) and show you how to write simple drawing programs. In the next lesson, we will take our drawing and programming skills to the next level and finally start creating some impressive pictures.
First Turtle Program
Open IDLE and then open a new Python file. Save the file as equilateral_triangle.py in the folder Documents\CodeWithSara\Python101\Unit2. Please refer to Lesson 1.3 for step-by-step instructions on how to do it.
Type the following lines into equilateral_triangle.py and then execute the program by pushing the F5 key.
forward(100) left(120) forward(200) left(120) forward(200) left(120)
Oh no, we got an error! It seems that Python Interpreter does not understand the instruction forward(100)! How is that possible?
Well, the problem is, that forward(100) is a function from the Turtle module. But we have not informed Python Interpreter that we want to use that module! To fix that, let’s add the line from turtle import * to the beginning of the file and hit F5 again.
Now that looks much better! We are not quite done yet, but at least we got some drawing on the screen and it looks almost like a triangle!
Clearly, the first side of our triangle is not long enough. Let’s make it the same length as the other two sides. Also, we all love the turtle shape, so let’s add that to the beginning of our program.
from turtle import * shape('turtle') fillcolor('green') forward(200) left(120) forward(200) left(120) forward(200) left(120)
This looks (almost) perfect!
How can we make this even better? Notice, that the entire drawing is not centered inside the Python Turtle Graphics window. Let’s try to fix that by moving the turtle to a different starting location. For that, we will use a new instruction, setposition(x, y) that moves turtle to a position given by the coordinates (x, y). Remember that (0,0) is the starting point for the turtle and also the center of the canvas. Also keep in mind that anytime turtle moves, it draws a line unless we tell it not to (by using the penup() command).
So the final version of our program (and its output) will look something like this:
Now this really is perfect!
Notice how we wrote our first Turtle program in several steps. We kept improving it using small changes and testing it after every step until we were satisfied.
This is a great and highly recommended way of writing small and large programs alike – implement a first version of the program that sort of works and keep iterating on it and testing it until it is (close to) perfect.
The other option would be to think through every step, foresee all the issues (like the fact that our drawing was not completely centered), and then write the final version of the code. While this might still be possible for drawing a triangle, it is impossible for larger and more complicated programs.
So our advice to all programmers out there – start with a small prototype and keep iterating and improving it until you have your final program!
These exercise are a repeat of the exercises in Lesson 2.1. This time, however, you should not type the solutions instruction-by-instruction in Python Shell. Rather, you should solve each exercise by coding a Python program. Store each program in a separate file in the Documents\CodeWithSara\Python101\Unit2 directory. Create each program in several steps, executing it repeatedly and fixing issues as they arise.
- Write a program two_squares.py that uses the Turtle module to draw two squares of size 100. Draw them in such a way that they are next to each other 100 steps apart. Make sure that the drawing does not contain any other lines. As the last development step, make sure that the drawing is in the center of the Python Turtle Graphics window.
- Write a program triangle_75.py that uses the Turtle module to draw a triangle that has one side of length 200, the other side of length 100, and the angle between these two sides is 75 degrees. Make sure that the drawing is (approximately) in the center of the Python Turtle Graphics window.
- Write a program star.py that draws a five-point star. Make sure that the star is in the center of your canvas.
If you’ve made it this far, you are doing great! Hopefully, you are having lots of fun and finding at least some of the problems challenging! The next lesson is where the real programming truly begins![prev-next-ad1 prev=”https://codewithsara.com/python-with-sara/python-101/u2-python-turtle-graphics/u2l1-introducing-turtle/” next=”https://codewithsara.com/python-with-sara/python-101/u2-python-turtle-graphics/u2l3-turtle-stars/”]