Lesson 2.1: Introducing Turtle
Turtle (sometimes called Turtle Graphics) is a drawing module that helps visualize programming concepts. Imagine a virtual turtle that you can command to move in the x-y plane. It starts at (0, 0), heading in the direction of x axis. You issue commands like forward(100) (go forward 100 steps) or left(60) (turn left 60 degrees). As the turtle moves around, it draws behind a line. Can you believe that the picture below was drawn using just these two commands (and a little bit of Python magic)? At the end of Lesson 2.3, you will be able to create pictures like these and more!
But let’s not get ahead of ourselves. First, we have to learn how to use Turtle! So let’s jump in …
First Turtle Drawing
Open the Python shell (remember that all you have to do is start a program called IDLE). Now type in the following few lines:
Let’s go over these lines one by one:
- The first line, from turtle import *, tells the Python shell to import everything from the module turtle.
- The second line, shape(‘turtle’) tells the turtle drawing tool to look like a turtle. Without this instruction, the drawing tool would look like a small arrow. Note also, that when you use the import statement, nothing visible happens. However, as soon as you issue a first turtle command, a window titled Python Turtle Graphics appears. This is the window where the turtle will do all its drawings.
- The third line, fillcolor(‘green’), makes the turtle look more real. It has other consequences as well, which we will discuss in later lessons. Both lines 2 and 3 are optional, the subsequent instructions will work the same way with or without them.
- As mentioned earlier, the command forward(100) instructs the turtle to go forward 100 steps.
- And command left(90) instructs the turtle to turn left 90 degrees.
- We repeat the instructions forward(100) and left(90) total of 4 times. That makes the turtle finish at the same position and same heading at which it has started. And, as the turtle moves, it draws a perfect square!
[cws-ad1]
Draw Your Own Shapes
Now it’s time for you to play with Turtle Graphics on your own. You can use the following set of instructions:
- clear(): Clear the drawing window
- home(): Return turtle back to its home position – coordinate (0, 0) and heading in the direction of the x-axis.
- forward(): Move forward given number of steps.
- backward(): Move backward given number of steps.
- left(): Turn left given number of degrees.
- right(): Turn right given number of degrees.
- penup(): Any moves after issuing this command will be “stealth”; i.e. turtle will move but nothing will get drawn until the next pendown() command.
- pendown(): Make turtle draw a line as it moves.
Things to Try
- Draw an equilateral triangle pointing up. Hint: Try turning left 120 degrees.
- Draw an equilateral triangle pointing down. Hint: Try turning right 120 degrees.
- Draw a right triangle. Hint: Draw two sides using two forward() instructions, draw the last side using the home() instruction.
Exercises
- Use Turtle and Python shell 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.
- Use Turtle and Python shell to draw a triangle that has one side of length 200, the other side of length 100, and the angle between these 2 sides is 75 degrees.
- Draw a five-point star. Hint: From home position, you should use 5 forward() instructions and 5 right() instructions. The angle at each point is 180 / 5 = 36 degrees.
Conclusion
We hope that you had fun playing and drawing with your turtle! Remember to show your turtle to friends and family! Maybe, they will want to try it too!
[prev-next-ad1 prev=”https://codewithsara.com/python-with-sara/python-101/u2-python-turtle-graphics/” next=”https://codewithsara.com/python-with-sara/python-101/u2-python-turtle-graphics/u2l2-turtle-programming/”]