Drawing shapes with the python turtle module

our basic understanding of the Python language is to know that we can perform operations, add Iterations with the help of Python Loops, and decide which program to execute with the help of conditional statements like this if, elifagain else. But Python is much more powerful than that; It can import and process files, create exciting games, simulate scientific concepts, process big data, generate machine learning models, and much more!
In this article, we will use Python to create graphical effects by using the Python Module Turtle. This is an introductory tutorial that teaches you how to draw diagrams and program diagrams using Python. Moving on, it's important to have a thorough understanding of Python basics
Python's Turtle module
This page turtle A module in Python is a module that allows the output of an image in code. The module provides a pointer, which can be customized like a turtle (hence the name), and the movement of the turtle leaves a trail behind, drawing visual patterns on the screen. The module is included with the Python Standard Library, which means we don't need to install the module itself. Its classes and functions can be easily found next import Statement:
from turtle import *
Now, let's dive deeper into this module with the official Python documentation:
Basic Turtle commands
As can be seen from the documents linked above, the basic commands we can give our turtle are the following:
forward()This function tells the turtle to move forward a certain distance. It takes an integer as an argument.backward()This function tells the turtle to move back a certain distance. It also takes an integer value as an argument.left()This tells the turtle to turn left at a certain angle that is passed to the function as an argument.right()This tells the turtle to turn right at a certain angle that is passed to the function as an argument.color()This will change the color of the turtle according to the string passed to this function as an argument (eg, “Pink”). Color names can be found here.width()This will change the width of the pointer by an integer valueexitonclick()This will allow us to close the resulting screen in our code, just by clicking on the screen.
Run the following code, and modify it according to your needs to understand what Tudu does when the other information above is called.
from turtle import *
color("pink")
width(5)
forward(100)
left(45)
color("blue")
width(4)
forward(100)
left(45)
forward(20)
color("red")
width(4)
forward(60)
left(45)
backward(50)
color("yellow")
width()
forward(100)
right(45)
exitonclick()
It uses OoP Turtle graphics
Now that we have seen the basic functions in a module and how it is generated, we will use the concept of object-oriented programming described in the documentation to continue our understanding. The concept of classes and objects, created in 2, makes it easier to organize large complex programs with variables that have the same parameters but different values. This is where oop comes in handy, and its inclusion in the module will allow us to use more than one turtle at a time.
In order to move forward, it is important that you have a basic understanding of the level of objects and objects, especially how objects can be created with their KireButes and methods.
Let's create our turtle objects and screens:
from turtle import Turtle, Screen
my_turtle = Turtle()
print(my_turtle)

As can be seen from the screenshot above, a turtle object has been created, and its location is defined. Now we will use the documentation to customize our turtle.
We will define the shape, color, and width of our turtle using the following code:
my_turtle.shape("turtle")
my_turtle.color("coral1")
my_turtle.width(4)
Let's redefine the screen object and customize it.
screen = Screen()
screen.title('Drawing Shapes with Turtle Module')
screen.bgcolor("white")
screen.exitonclick()

The screen element is defined above, and its title and background colors are set accordingly. Now, let's move on to the main part of this lesson!
Drawing shapes
Now we will learn to draw different shapes with our custom turtle!
He is a triangle
The first shape we will draw is a triangle. This can be drawn using the basic functions we discussed above: forward and right. We know that a triangle consists of 3 sides, and in an equilateral triangle, the angle between each side is 60. We can draw this field using the following lines of code:
my_turtle.forward(200)
my_turtle.right(120)
my_turtle.forward(200)
my_turtle.right(120)
my_turtle.forward(200)
my_turtle.right(120)

As can be seen, we have successfully created a triangle with the help of our turtle. Note that we set the angle when the turtle moves forward to 120, so that would mean the remaining angle that would be the interior angle of the Triangle would be 180 – 120 = 60. This was our goal. We will work similarly with the next situation.
The yard
Now we will use our turtle to draw a square. Since the square has 4 sides, we will use the forward path 4 times, with the angle set as 360 / 4º whenever we use the right path. Let's change the turtle color again to dark turquoise (turtle colors)
Here is our code to draw a square:
my_turtle.color("dark turquoise")
my_turtle.forward(200)
my_turtle.right(90)
my_turtle.forward(200)
my_turtle.right(90)
my_turtle.forward(200)
my_turtle.right(90)
my_turtle.forward(200)
my_turtle.right(90)

The pentagon
Next, we will build a pentagon, which is a 5-square angle between each side equal to 108. We will include our code above. We will also change the color of our turtle.
my_turtle.color("spring green")
my_turtle.forward(150)
my_turtle.right(72)
my_turtle.forward(150)
my_turtle.right(72)
my_turtle.forward(150)
my_turtle.right(72)
my_turtle.forward(150)
my_turtle.right(72)
my_turtle.forward(150)
my_turtle.right(72)

As you can see in the Code Block above, we reduced the forward movement from 200 to 150 so that the Pentagon is drawn inside the screen.
Creating an algorithm
We used the turtle module to draw a triangle, square and pentagon. As we can see from the above, we can easily find a pattern. The turtle moves forward and to the right just as it is. By drawing a triangle, so a three-dimensional structure, the turtle moves forward, then forward, then forward, then forward again, a total of 3 sets forward and to the right. To get a square, the same set is made four times, that is, as many sides as the structure has. The same with the Pentagon. Therefore, we can find a pattern of repeated operations forward and to the right. We can set a fixed amount of distance covered whenever the turtle moves forward. As for the angle given in the right way, as the number of times the statements are repeated depending on the number of sides in the shape, similarly, the angle is determined by the number of sides. This exterior angle can be easily calculated by the following formula:
Exterior Angle = 360 / Number of sides
The exterior angle of a triangle will be 360/3 = 120. The exterior angle of a square will be 360/4 = 90, and so on. This will be used to feed the right path.
Defining the task
We will now describe a general function that takes the number of sides of a shape to draw a shape. If we give 3 as an argument, it will create a triangle. If we give 8 as an argument, it will create an octagon, etc.
def draw_shapes(num_sides):
angle = 360 / num_sides
for i in range(num_sides):
my_turtle.forward(50)
my_turtle.right(angle)
The function takes the number of sides, calculates the exterior angle, given the right path, and subtracts the front and right paths the number of times they have sides. So, let's say we want to draw an octagon, we'll call a function and give the number 8 as an argument to that function. We can define a state color:
my_turtle.color("pale violet red")
draw_shapes(8)

Drawing shapes within a circle
Now we will use the function above that we described, and use it for loop to input a range of numbers, each side by side. We will start with 3 for a triangle, and our turtle will draw as many shapes as we like. So, let's say we would like to draw a triangle, square, pentagon, etc. Coming to the Decagon, we will extract the numbers from 3 to 11, as 11 is included in the distance of the loop.
Let's also add a provision to paint each state in a different color. For that, we will create an array of colors, and the loop will loop through the colors in the array.
my_colors = ["dark gray", "hot pink", "midnight blue", "orange", "indigo", "dark sea green", "tan", "pale violet red", "sky blue", "spring green"]
Once we've created our color array, we'll modify our function to include a color modifier, and then move into the shape drawing range.
def draw_shapes(num_sides):
angle = 360 / num_sides
my_turtle.color(my_colors[3-num_sides])
for i in range(num_sides):
my_turtle.forward(75)
my_turtle.right(angle)
for shape_size_n in range(3,11):
draw_shapes(shape_size_n)

Lasting
In this tutorial, we explored the turtle module, understood its basic functions and the concept of oop, and used it to create shapes. This was an intermediate level Python course that required a basic understanding of classes and objects, defining and calling functions, and custom colors with the help of trinkets. This is a basic level example of what can be done with the turtle module. We can test the module again and learn to write more about it!



