Turtle in Python

Introduction

Turtle is a module in Python that allows us to bring shapes, figures, and designs to life on a screen. The various functions within the module allow you to systematically draw any sort of figure with help of a “turtle” object which acts like a pen, following the commands given to it in terms of functions like forward(), backward(), and many more which will be discussed in the latter sections.

Functions in Turtle

The table below will give you an idea of basic functions available to kick start your journey in creating various designs

Method

 

 

Parameter

Functionality

Turtle()

None

Creates a Turtle object to draw

forward()

Distance (in pixels)

Moves the Turtle object by the specified amount of pixels in the forward direction

backward()

Distance (in pixels)

Moves the Turtle object by the specified amount of pixels in the backward direction

right()

Angle

Rotates the Turtle object in the clockwise direction by the specified angle

left()

Angle

Rotates the Turtle object in the anti-clockwise direction by the specified angle

penup()

None

Lifts the Turtle’s pen

pendown()

None

Places down the Turtle’s pen

color()

Color name

Changes the color of the Turtle’s pen to the specified color

fillcolor()

Color name

Changes the fill color of the Turtle’s pen

position()

None

Returns the coordinates of the current location of the object

goto()

x & y coordinate

Moves the Turtle object to the specified location

done()

None

Display the figure on the screen

Drawing in Turtle

In order to utilize Turtle and its features we need to import the Turtle library which comes with the Python package and does not require external installation. Any drawing in Turtle requires these 4 basic steps to be followed:

1. Import Turtle module

Code:

from turtle import *
or simply can be written
import turtle

2. Create a screen and Turtle object

We require a screen or window on which our figure can be displayed as well as a Turtle object through which we can draw anything of our choice

Code:

scr = turtle.Screen()
scr.bgcolor("orange")
scr.title("Board Infinity")
tur = turtle.Turtle()

3. Commands to draw the require shape

As an example, let us draw a line of 200 pixels

Code

tur.forward(200)

4. Display

Now that we are done, we must inform Python to display the figure which is drawn and this is achieved with the help of the done function

turtle.done()

Output:

Exercise

Let us now try to apply the above-taught functions as well as the algorithm to draw in constructing 3 basic shapes – Square, Rectangle, and any polygon

Shape  1: Square

Code:

import turtle
tur = turtle.Turtle()

for i in range(4):
        tur.forward(200)
        tur.right(90)

Output:

Shape  2: Rectangle

Code:

import turtle
tur = turtle.Turtle()

tur.forward(150)
tur.right(90)
tur.forward(100)
tur.forward(150)
tur.right(90)
tur.forward(150)
tur.right(90)
tur.forward(250)

Output:

Shape  3: Polygon

Code:

import turtle
tur = turtle.Turtle()

sides = 3  #Here we specify the number of sides of the polygon, let's take 3 as an example
length = 200    #Here we specify the length of the side of the polygon
angle = 360.0 / sides #Here we determine the angle of the polygon

for i in range(sides):
        tur.forward(length)
        tur.right(angle)

Output:

Additional Methods in Turtle

1. Undo Changes:

Sometimes, it becomes inevitable that we make mistakes while sketching out the required shapes. However, Turtle provides the undo method to revert to the previous changes made so that you can continue with drawing using turtle object

Syntax:

turtleobject.undo()

2. Clearing Screen:

The screen of the Turtle module can be cleared once and for all using the clear method of Python. This will clear the screen but the variables do not change and the turtle remains in the same position. However, if there are more turtles on your screen they will not be cleared unless it is explicitly called for

Syntax:

turtleobject.clear()

Conclusion

By the end of this section, you will have a good understanding of the Python turtle graphics module and implement basic figures using the above-taught functions