Turtle module is a part of the standard python installation which provides a drawing board so that we can draw all over it using turtle methods. It is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967.
Python’s turtle module lets us control a small image shaped like a turtle, just like a video game character. We need to give precise instructions to direct the turtle around the screen. Because the turtle leaves a trail wherever it goes, we can use it to write a program that draws different shapes.
Lets begin to understand turtle methods with following examples.
First import turtle module
import turtle as tt
For convenient I have imported turtle module as 'tt' throughtout the examples.
Example 1: Moving turtle
# importing turtle module import turtle as tt tt.forward(100)
Above we use forward function to move the turtle 100px forward while leaving a trail behind it. When we run the program we will see something like this output.
We will be noticing that window screen automatically fade out as it drew 100px arrow sign. To run the program unitll we exit we have to update above code as.
# importing turtle module import turtle as tt tt.forward(100) # to keep screen holding tt.getscreen()._root.mainloop()
Example 2: Creating square shape
import turtle as tt # change the shape of the default # arrow to turtle shape('turtle') for i in range(4): tt.forward(100) # change turtle direction # at angle 90 clockwise tt.right(90) tt.getscreen()._root.mainloop()
In the above example, we have used for loop in the range of 4 to move turtle 4 times in four different directions at 90º clockwise. As we already know forward functions moves the turtle, we have to used right function to change directions.
right() : turns the turtle clockwise
left() : turns the turtle counter clockwise
shape() : change the shape of the default arrow. Initially there are the following polygon shapes: “arrow”, “turtle”, “circle”, “square”, “triangle”, “classic”
Example 3: Creating circle of squares
# import turtle import turtle as tt def make_circle(sidelength): """ Draw circle from 30 consecutive squares each bent at 10° in clockwise directions. """ for i in range(30): for j in range(4): tt.forward(sidelength) tt.right(90) tt.right(10) # calling function make_circle(100) tt.getscreen()._root.mainloop()
Example 4: Creating a red flower
Before begin with code, let us review some functions.
setheading(to_angle) : This function sets the orientation of the turtle pointer to to_angle. Shortly we can call this function as seth(to_angel) like as seth(15), seth(50).
color(color_name) : Changes the color of the turtle’s pen
circle(radius, extent=None, steps=None) : Draw a circle with given radius. Other parameters extent and steps we can used form modification of circle.
# import turtle module import turtle as tt tt.shape('turtle') # Changes turtle’s pen to # color 'red' tt.color("red") for angle in range(0, 360, 15): tt.seth(angle) tt.circle(50) tt.getscreen()._root.mainloop()
Example 5: Creating star
import turtle as tt for i in range(5): tt.forward(100) tt.right(144) tt.getscreen()._root.mainloop()