Python proves its abilities in a wide variety of fields. It is good for heavy computational jobs like Data Science and Machine Learning. It is great for creating computer networks and APIs. It is even great for creating graphics or games.
Today, we’re going to explore one of the most simple yet extremely powerful libraries of Python – Turtle. On our way, we’ll learn how to set the starting position of a drawing in Turtle and draw some simple shapes to get familiar with the syntaxes and functions of Turtle. So let’s get started with it.
What is Turtle?
Turtle is an animation and graphics development module that is a part of Python’s standard library. It helps in drawing complex shapes and animation with few simple functions. Python programmers often compare it with the famous Python library known as pygame. There are differences between pygame and turtle, but both have their own benefits. We’ll compare both at the end.
History of Turtle
In the year 1967, when computer programming was in its infancy, Wally Feurzeig, Seymour Papert, and Cynthia Solomon created a Logo programming language. The turtle was a part of this language and was created to introduce programming to kids. It had just two functions.
Consider a turtle sitting on your computer screen. Now this turtle wants to move. When you use the function, Turtle.forward(10), the turtle moves 10 pixels in the direction it was originally facing. On its way, it draws a line on the screen. Wherever the turtle moves, it keeps drawing a line. Consider it a pen that never lifts. Now eventually, the turtle will hit the end of the screen. So it needs to rotate at some point. So for that, there’s the
Turtle.right() function. You can rotate the turtle in the clockwise direction for 10 degrees by entering
By just these two simple commands, complex shapes and drawings can be created.
Python’s Turtle module is based on this Turtle and has functions similar to these with many more complex functions. It depends on the tkinter library. So it needs a Python version that has a tkinter installed with it. There was a different Turtle module till Python 2.7. The turtle module till Python 2.7 had uppercase “T” in its name – “Turtle”. The turtle module in the newer versions of Python has lowercase “t” making it “turtle”. The newer version of the turtle is almost completely compatible with the older version.
Functioning of the turtle module
turtle has both object-oriented and procedure-oriented methods to create turtle graphics. It has mainly two classes –
TurtleScreen() to create a window and
Rawturtle() uses a canvas to draw. A subclass
RawTurtle() known as
Turtle is used to draw on the
Screen. Both of these classes use Tkinter for basic graphics.
It has methods to control the motion of the turtle, the state of the turtle, control the pen, control the window and animation. There are more than 100 functions in the module to control every aspect of the graphic.
How to set up the turtle window
To set up the turtle window, first, we will import the turtle module.
After importing the turtle module, we’ll use the
Screen() method of the turtle module to create a playground for our turtle.
screen = turtle.Screen()
Now that we have our screen, we’ll set up the size of the screen, the color of the playground, and the title of the window using the
We would also need to exit the window at some point. So for that, we’ll use the
exitonclick() method of the
When we run the program, a turtle window will be launched with the mentioned configurations.
There we go! The [playground for our turtle is ready. Now let’s see to set the starting position of our turtle.
First, let’s check the current position of our turtle using the
So when we print the position of the turtle we get the coordinates (0.00,0.00) and an arrow appears on the turtle window facing towards the right and placed at the center of the window. This arrow is our turtle. That means if we use the turtle.forward() method right now, it will move towards the right the number of units mentioned from the center of the playground.
Now we can change the position of the turtle using the
sety() method or the
goto() method. But the problem with it is that if we use it right now so it will draw a line. So we will need to lift up our turtle first. Let’s see how we can set the starting position.
Set the starting position with the
We can set the starting position of our turtle by first lifting the turtle up with the
penup() method and then using the
goto()method to move it to a particular position.
So our turtle moved to the requested position. Now you can use the
pendown() method to drop the turtle and start drawing what you want. Another way of doing this is using the
setX() and the
sety() method to set the starting position
Using the setx and sety method is very simple. Here you set the x and y coordinates separately.
The difference between
sety() method and goto method is that setx sety method provides more flexibility than the goto method. You can see in the above clip that it first moves 100 pixels in the positive x direction and then 100 pixels in the positive y direction. But the final position of the turtle remains the same in both methods.
How to draw something using in turtle?
Now that we have a starting position let’s try to draw something.
To draw something, first, we will have to drop our turtle using the
Now we can either go forward or backward using the forward() and backward() methods or we can rotate the turtle using the right() and left() methods. Using these methods, let’s try to draw a square and a circle.
screen = turtle.Screen()
turtle. goto(-25, -25)
To draw a square, first, we moved 50 pixels towards the right, then rotated our turtle 90 degrees, and then repeated the same thing for 3 more times. Then we moved the turtle to a different location and made a circle using the
Related: Learn to draw a fish using turtle.
Exiting a turtle window
To exit the turtle window, we simply can click the cross button like any other desktop application or we can use the ctrl+C command in our terminal.
Pygame vs. Turtle – Which is better?
Pygame and Turtle are both created for developing graphics. Pygame provides more advanced and low-level access to graphics, whereas Turtle is a lightweight beginner-friendly graphic module used to introduce programming in a simple way.
If you want to draw some basic animation or graphic designs, turtle is awesome for that purpose, and if you want to create games or heavy GUI software, pygame is the best choice.
So if we talk about capabilities, pygame is better than Turtle, and if we talk about simplicity, Turtle is better. Using turtle has another benefit that you don’t need to install any libraries to use it, whereas you need to install pygame to use it.
Related: Python Pygame: An Easy Introduction.
Turtle is an awesome library for creating simple graphic designs. It has very simple functions and doesn’t need any separate installations. It shows how simple programming can be if we do it the right way. It is a great option for artistic programmers to display their skills. Even if you’re not quite an artist, give it a try and write a few lines of code to draw a simple shape or anything you want. Practice is an important part of programming.
Stack Overflow thread for the same question.