Turtle is a simple toolkit that provides an enjoyable and straightforward way to draw pictures, figures, and different shapes on windows and screens. Originally the Turtle was built for kids to attract them towards programming. So if you ask why the name is a turtle? The name says the drawing process as imagining a turtle crawling on a piece of paper with a pen tied to its tail. And you can give a command like in which direction to move and straight or backward.
Table of Contents
- What is Python Turtle Module?
- Hands-on Guide working with Turtle Module
- How do change the shape and color of Turtle?
- How to change the background color of a turtle?
- How to move the turtle in any direction using the turtle module?
- Making different shapes using the Turtle Python module?
- Create full colorful Circle shapes with python turtle module
- Summary
What is Turtle library in Python and Why it is required?
The output till we get on the compiler is in the form of Numericals or text, but what if we want to draw shapes, figures, or images on the screen then How to do that? So this comes graphics and python has a vast set of libraries and operations to perform this type of task.
📌 Today in this tutorial we are going to learn the basic concept of the Turtle library, how to set turtle up on a computer programming using python turtle library. And also how you can draw amazing shapes and figures and compose them to result in an amazing drawing.
We can also define Turtle graphics as, which refers to controlling a graphical entity in a graphics window with x, y coordinates.
Getting Started with turtle
Turtle is a pre-installed library in python which is similar to canvas that we use to draw shapes. It basically provides a virtual on-screen pen that we can use to make amazing shapes and create pictures.
- Python Environment: you must be familiar with the basic concepts of python. you can use any Python IDE or Jupiter Notebook, even VsCode is also the best.
- Python Version: You must have Python 3 installed in the system. If not then download it from the python official website.
The Python turtle consists of all the important methods and classes required to create designs and images. Open any Python IDE like Pycharm, Spyder, Vs code, or whatever of your choice and make your hands dirty by experiencing Python Turtle library and its power in drawings.
Import Turtle
Now we import a Turtle library and create a dedicated window where we carry out each drawing command.
import turtle
t = turtle.Turtle()
t is the name of my turtle or you can say it as an object of Turtle class. Now, I can access each method using this object. And If you run this, you will be able to see the empty window with a classic label like a pointer of black color.
How to set the Shape of a turtle?
By default, the shape of the turtle is classic, as you will be able to see on screen. You can change this to turtle or any other shape like triangle, square, arrow. To change the shape simply write the following command.
t.shape("turtle")
How to Change the color of a turtle?
- if you want to see the default color use the following command
How to Change the Background Color of Turtle?
How to Move Turtle in Different Directions?
- Forward: to move the turtle forward use the forward() method and specify distance, how many pixels you have to move. You can also use the short form of the forward method as fd(), both are the same. distance is nothing but the number of pixels turtle moves.
- Backward: to move the turtle backward use the backward() method or bk() and specify the distance.
- Left: To move the turtle in the left direction use the left() method or lt() and specify the angle in degree in which direction you have to change its face. Note it is not the distance it's an angle in degrees.
- Right: To turn the turtle in the right direction use right() method or rt() and specify the angle at which you want the turn.
Draw a Square shape using turtle
Instead of writing again and again forward and left method, we can make use of for loop. As well we are seeing the turtle left in the diagram after completing so, we can also hide that, and we can also set the speed of the turtle as in the above figure turtle moves in speed, so we can slow down the speed with the help of speed() method.
To fill the color in our shape you can use the fillcolor() method and pass the color name.
Drawing Circle and various shapes.
we can draw a circle with a specific radius using the circle() method. You can specify various parameters but one is compulsory as the radius. If you give a negative radius then, it will draw in a clockwise direction and a positive then anticlockwise direction.
If you want to make a semi-circle or extent circle up to some specific dimension then, we can specify the extent parameter.
And you can make various shapes using the circle method by defining the steps parameter as an int value. if you give steps=3 then it will make a triangle. And thus it can be used to make various shapes like a pentagon, octagon, rhombus, etc.
To clear or reset the screen use reset() method.
💻 code:
Now, all the shapes we made it is built from the home position, but we want to make shapes from a different position in our window. To draw shapes from the different positions, we can make it in 2 ways as.- first is you first use a left or right command to reach a position from where you want to draw a circle, and after that, you make use of the circle method. But it will take you 4 to 5 lines of code.
- to do this in a single line we can use the goto() command.
Code:
Now we can see we have made a circle at 100 pixels far from the home position. But still, there is little problem in both the above methods, is we are getting an extra line which we do not want. so we can remove them using the up() and down() methods.
code:
This is how you can use various methods with many parameters and draw fantastic figures and drawings on your canvas using the python turtle library.
The goto command takes the x and y coordinate and takes the turtle at a defined coordinate position and starts drawing.
For making a turtle pen up or down we can also use the penup or pendown method.you can also change the size of the drawing pen using the pensize() method.
syntax: turtle.pensize(4)
Summary
We have finished with a basic understanding of the turtle library and are able to make various figures and shapes from any position with very good design. I will suggest you try different figures and we will also be drawing some simple figures as a turtle project like star shape, heart shape, and many more.
Thank you!..😊Keep learning, happy learning
- first is you first use a left or right command to reach a position from where you want to draw a circle, and after that, you make use of the circle method. But it will take you 4 to 5 lines of code.
- to do this in a single line we can use the goto() command.
Now we can see we have made a circle at 100 pixels far from the home position. But still, there is little problem in both the above methods, is we are getting an extra line which we do not want. so we can remove them using the up() and down() methods.
This is how you can use various methods with many parameters and draw fantastic figures and drawings on your canvas using the python turtle library.
The goto command takes the x and y coordinate and takes the turtle at a defined coordinate position and starts drawing.
Summary
We have finished with a basic understanding of the turtle library and are able to make various figures and shapes from any position with very good design. I will suggest you try different figures and we will also be drawing some simple figures as a turtle project like star shape, heart shape, and many more.