## Learn Turtle in Python

Posted on 17th August, 2016 by Hetal Vinchhi

Welcome Guys,
To learn Turtle in Python is really a good fun. Today, lets see some concepts like how to set positions, move turtle forward and backward, draw and fill different shapes, use of pen etc.

## What is Turtle?

Like many other libraries, turtle is Python's pre-packaged library which provides drawing functionalities using Turtle object. The good thing is that turtle is really fun. Lets see in details how does the little turtle work! For basic understanding of Python, visit official website of https://www.python.org/

## First Program to Learn Turtle

Let's dive into Python and draw something with Turtle.

from turtle import *
tomy = Turtle()
tomy.shape("turtle")


What do the above lines of code say! The very first line from turtle import * indicates that we have imported the Python's module named turtle. The second line tomy = Turtle() indicates that we have created the turtle's object using the Turtle constructor. In another words we have created a turtle named tomy. The last is: tomy.shape("turtle"), which indicates that we have applied the shape of "turtle" for our tomy object so it looks like actual Turtle on the output screen. If you run the code, you will get the output similar to following:

## Turtle Positioning

From the output of the code, we can say that the default position of the turtle is center of the screen (0,0). You can imagine the screen as the graph paper with the axis and marks printed on it. Following figure provides information of x and y axis for the screen. Note that the initial position of tomy is center (0,0).

Following figures will help you to understand the axis and its positive-zero-negative values on the screen.

tomy.goto(0,100)

tomy.goto(100,0)

tomy.goto(0,-100)

tomy.goto(-100,0)

Now that you have basic idea of the axis and values, lets go on a ride holding turtle's tail ;)

from turtle import *
tomy = Turtle()
tomy.shape("turtle")
tomy.goto(0,100)
tomy.goto(0,-100)
tomy.goto(100,0)
tomy.goto(-100,0)
turtle.done()


The output of above code will be similar to following screen. But you will notice an animation showing how turtle follows the path:

## Turtle Movements

tomy.goto(x, y) (same as setpos(x, y))
This will move the turtle to specified location on the screen.

tomy.forward(pixels) (alias fd())
This will move the turtle forward for specified pixels from it's current position. The turtle will walk in the direction to which it is facing.

tomy.backward(pixels) (alias bk())
This will move the turtle backward for specified pixels from it's current position. It will walk in the reverse direction.

tomy.right(angle)
This will rotate the turtle (changes the face direction where turtle is looking at) to the right to given angle degree. This rotation is relative to current angle.

tomy.left(angle)
This will rotate the turtle (changes the face direction where turtle is looking at) to the left to given angle degree. This rotation is relative to current angle.

Okay. Let's play with these functions.

from turtle import *
tomy = Turtle()
tomy.shape("turtle")
tomy.forward(100)
tomy.right(90)
tomy.forward(100)
tomy.right(90)
tomy.forward(100)
tomy.right(90)
tomy.forward(100)
tomy.right(90)
turtle.done()

Oh!!! It just created a square. Notice that we have used the forward and right functions four times. So it created four sides. We can shorten this code using for loop as given below:

from turtle import *
tomy = Turtle()
tomy.shape("turtle")
for i in range(4):
tomy.forward(100)
tomy.right(90)
turtle.done()

The turtle can move its face upto 360 degrees. In the above code, you can notice that 90 = 360/4. As we wanted four sides, the loop runs four times. But what if we want to create more than four sides?

Here is a shortcut: we can replace the line tomy.right(90) with tomy.right(360/4) where 4 is the number of sides in a shape. We can draw many shapes with the above method. Lets draw a triangle.

from turtle import *
tomy = Turtle()
tomy.shape("turtle")
sides = 3
for i in range(sides):
tomy.forward(100)
tomy.right(360/sides)
turtle.done()

Now let's draw a polygon with 19 sides:

from turtle import *
tomy = Turtle()
tomy.hideturtle()
sides = 19
for i in range(sides):
tomy.forward(30)
tomy.right(360/sides)
turtle.done()

Here, in last example, what I want to show is: the shape remains open (unclosed). The reason is: in Python, integer divided by integer results in integer and fraction part is discarded. In our case, it is 360/19 = 18. Let's replace this with 360.0/19 = 18.9473. So that the fraction is getting considered. Now let's replace tomy.right(360/sides) with tomy.right(360.0/sides). And that's it! We got a perfect closed shape.

## Turtle Visibility

We can show and hide the turtle using the following methods.

showturtle() hideturtle()

Add the line tomy.hideturtle() at the end in the last sample code and it will hide the turtle when it is done with drawing the shape.

You can change the shape of a turtle with the following method.

shape(shapename)

This function can accept one of the values from arrow, turtle, circle, square, triangle and classic.

## Learn the Use of Pen

When you move the turtle from one position to another, the turtle leaves it's footprints or tail and draws a line. Checkout following code:

from turtle import *
tomy = Turtle()
tomy.fd(100)
tomy.goto(100,100) # moves the turtle at absolute position
tomy.setpos(200, 200) # same as goto
turtle.done()


You will notice a constant drawing on the screen while turtle moves. To remove this trail or footprint of tomy, there are some methods: penup() and pendown().

Following code says, "Hey Tomy!!! Lift your pen up and go to the given position and then keep down your pen".

from turtle import *
tomy = Turtle()
tomy.fd(100)
tomy.penup()
tomy.goto(100,100) # moves the turtle at absolute position
tomy.pendown()
tomy.setpos(200, 200) # same as goto
turtle.done()


The pen can be made more darker/thick or lighter/thin using the method pensize(point).

## Learn Shapes in Turtle

### Circle

circle(radius, extent=None, steps=None)
The circle is drawn counter clock wise by providing positive radius.
radius: radius of circle
extent: decides which part of circle to be drawn
steps: number of steps to use

from turtle import *
tomy = Turtle()

tomy.up()
tomy.goto(0,200)
tomy.down()

tomy.circle(20)
tomy.up()
tomy.goto(0,100)
tomy.down()

# circle with radius 30 and from 0 to 180
tomy.circle(30,extent=180)
tomy.up()
tomy.goto(0,50)
tomy.down()

# circle with radius 40 and with 6 sides
tomy.circle(40,steps=6)
tomy.up()
tomy.goto(0, -150)
tomy.down()

# circle with radius -40, clockwise drawn
tomy.circle(-40)
turtle.done()


And the output is:

## Playing with Colors and Filling Shapes

To change the color of turtle's pen color() or pencolor() method can be used. To change the background color of the window bgcolor() method can be used. Let's see an example to fill a shape:

from turtle import *
tomy = Turtle()
bgcolor("blue")
tomy.color("green")
# begins feeing a shape
tomy.begin_fill()
tomy.circle(50)
# ends feeing a shape
tomy.end_fill()
turtle.done()


And the output is:

## Bonus

### Difference between clear and reset methods

clear()
This method is used to clear the turtle window but it leaves the turtle to its current position.

reset()
This method clears the screen and also resets the turtle to its original position as well as resets its heading.

### Control Animation Speed

speed()
This method can be used with argument in range from 0 to 10. The lower the value, the slower the speed and vice-versa.

tracer()
This method turns animation speed on or off and sets delay to update the drawing.