TURTLE IN PYTHON…

Ashish Sharma
4 min readJul 31, 2017

--

You all must be wondering what type of title is this, we started with python and now we have turtle, maybe later more animals will come (sorry I know it was a bad one). So don’t get confused, turtle is basically like a drawing board which lets you draw like we draw in paint. In paint, we had pencil to draw, here we will have a turtle which we can move in any direction to draw our respective shapes. This python interface is similar to graphics which we use in C. The syntax and handling of graphics in C is pretty confusing. But in python, we can see our turtle move as we give any command side by side. So I took this topic of turtle because, we will be making a small game in python like a 100 metre race between two players. It will be like throwing a dice, and depending upon the number player will move forward. The first one to reach the other end wins the race. This is our ultimate aim. So let us start,

For using the python interface you have to first import the turtle with the help of command:

import turtle

Now when we have successfully imported our python, we can use it. We know for drawing we need pen, hence we can get our pen in python with the help of following command:

z=turtle.Pen()

Here the syntax is case sensitive, remember the P should be in uppercase. As soon as you enter the above command, a python window will pop up.

In my PC it is showing as NOT RESPONDING in my python window, but it is working properly in IDLE shell window. So that is not a problem.

Hence we can now see a classic python shape in the middle of the screen, we can actually change the way we want to show our python with the help of shape command ( We can get an actual turtle too!!)

z.shape(‘ — ‘); — can be filled with classic, arrow, turtle or circle.

In the left hand side, we can see the various shapes of our turtle cursor. By default, we will be getting the classic cursor, if we want to change we can change it with the help of above mentioned command.

As this is just the introduction to turtle, today we will be discussing four basic commands in turtle which are used to move our turtle in any direction. There are n number of commands with the help of which we can change the width of turtle, color, background color, position etc which we will be seeing in the coming blogs. So the basic command used to move our turtle forward is as follows, as I clearly mentioned the easiness of python in my first blog, we can see the same here also as the command itself is ‘forward’:

z.forward(distance to be moved)

z.fd(distance to be moved)

Eg. z.fd(100) will move our turtle forward 100 units, these units are basically like 1 cm. Similarly we have command to move backward as:

z.backward(distance to be moved)

z.bk(distance to be moved)

z.back(distance to be moved)

Now we know how to move our turtle forward and backward, now what if we want our turtle to turn to certain angle, that is also possible with the help of two commands named left and right as follows:

z.left(angle to be moved)

z.right(angle to be moved)

These commands will change our python angle to certain degree which is specified in the command. So now we know the basics of movement of our python. Let us quickly draw a rectangle with the help of these commands:

IDLE command for drawing rectangle
Turtle window

So for today’s codework you have three things to do:

Draw the rectangle with the help of looping concept

Draw a square with and without looping concept.

Draw a hexagon with the help of looping concept.

This was just a introduction to turtle concept, as I just wanted to share how drawing is simple in python. We will start with our little game in the next blog, so try the above problems and keep coding…

--

--