Register Login

Turtle Graphics in Python

Updated Mar 12, 2022

We all have done Turtle-based programming with logo in our childhood days. That was a fascinating language that allows us to create graphics and shapes. Yes, Python can also help us perform that.

What is Turtle?

Turtle is a Python library that helps you to draw graphics by giving some commands like forward, backward, left, right, and so on, to the pointer. The pointer used to draw graphics is also known as pen. Using these commands you can draw some basic shapes like square, circle, rectangle, and also some complex graphics which you will learn in this document.

Following are some of the turtle methods used to draw objects in this document:

  1. forward(pixels): It moves the arrow forward by 10 pixels.
  2. right(angle): It moves the arrow in a specified angle in clockwise direction.
  3. left(angle): Directs the arrow to the left at the specified angle.
  4. backward(pixels): Directs the arrow to move backwards by specified pixels.
  5. circle(): It returns a circle of radius given as the argument.
  6. up(): Moves the pointer to some other place without drawing.
  7. down(): Starts drawing if the drawing is stopped due to up().
  8. setx(): Sets the first co-ordinate of the arrow to X.
  9. sety(): Sets the second coordinate of the arrow to Y where the first co-ordinate is set to X.
  10. bgcolor(): Sets the background colour of the graphics window.
  11. pensize(width): Increases or decreases the size of the pointer.
  12. pencolor(string-colour): Changes the colour of the pointer from black to specified colour
  13. penspeed(integer): Increases or decreases the speed of the pointer. The limit of speed ranges from 1-10, where 10 is the highest speed.
  14. goto(): Moves the pointer from (0,0) co-ordinates to an arbitrary position.
  15. title(string-title): Sets the graphics window name to the specified string.
  16. shape(string-option): Sets the pointer to a specified shape

Turtle graphics window:

The graphic window displays the result of the code. The graphic window by default has white as the background color, you can modify it to any colour of your choice using the bgcolor() method.

For example:

turtle.bgcolor(“blue”)

Output:

The default name of the graphics window is “Python Turtle Graphics”, you can change it to any name of your choice by using the title() method.

For example:

turtle.title("My Turtle Program")

Output:

Turtle pointer:

Turtle provides the pointer as an arrow in the graphics window, it also allows you to modify it to circle, or turtle as well.

For example:

t = Turtle()
t.shape("circle")

t.shape("turtle")

The pointer is of width 1 by default, however, you can increase or decrease the size of the pointer using the pensize() method.

For example:

t.pensize(5)
t.forward(100)

You can also change the color of the pointer from black to any color of your choice using pencolor() method.

For example:

t.pensize(5)
t.pencolor("blue")
t.forward(65)

Turtle co-ordinates

The pointer in the graphics window is initially at  0, 0 position. To move the pointer to any arbitrary position you can use the goto() method where you can specify the coordinates of the arbitrary position.

To move the pointer to anywhere in the first quadrant both coordinates must be positive (+, +).
To move the pointer to anywhere in the second quadrant coordinates must be negative, and positive (-, +).
To move the pointer to anywhere in the third quadrant both coordinates must be negative (-, -).
To move the pointer to anywhere in the fourth quadrant coordinates must be positive and negative (+, -).

For example:

t.goto(100,100)

Creating objects

Turtle pointer initially points to the right, so to create a line just use the forward(pixels) method.

For example:

t.forward(50)

Output:

Consider an example of a square, for this you will need forward(pixels) to draw the line and right(angle) to give the pointer correct direction:

For example:

import turtle
square=turtle.Turtle()
square.forward(100)
square.right(90)
square.forward(100)
square.right(90)
square.forward(100)
square.right(90)
square.forward(100)
turtle.done()

Output:

Similarly you can also draw an alphabet:

For example:

import turtle
t=turtle.Turtle()
t.left(90)
t.backward(100)
t.right(90)
t.forward(100)
turtle.done()

Now, let’s draw a circle of radius 20 cm using the circle() which requires radius as the argument:

import turtle
turtle_circle = turtle.Turtle()
turtle_circle.circle(50)

Similarly, we can draw nested circles also known as a concentric circle. Circles having common centres are known as concentric circles. circle(), up(), down(), and sety() methods are used for drawing concentric circles:

For example:

import turtle	
t = turtle.Turtle()
#setting initial radius as 5
r = 5
#printing 5 concentric circles
for i in range(5):
	#increasing radius on every iteration
	t.circle(r * i)
	# using up() to move from one circle to another circle without drawing
	t.up()
	t.sety((r * i) * (-1))
	# using down() to continue drawing
	t.down()

Output:

Creating a Rainbow Benzene

Rainbow Benzene is a Benzene structure like object having few Rainbow colours. You can create the structure using pen(), forward(), and left() methods.

For example:

import turtle
#creating list of colors to get colorful Bezene structure object 
colors = ['violet', 'indigo', 'yellow', 'green', blue, 'orange']
#using the pen() to change the color of the pointer
t = turtle.Pen()
for x in range(50):
	#changing the pen colourr based on the list of colours
	t.pencolor(colors[x%6]) 
	t.forward(x)
	t.left(59)
turtle.done()

Output:

Filling object

You can color your objects using begin_fill() and end_fill() methods. Before the object is created call the begin_fill() indicating a close object will be created which needs to be filled and after the object is created call the end_fill() method indicating the object can now be filled. By default, it fills the object with black color. To fill the object with a specific color use the pen() method with fillcolor argument having the desired color as value.

For example:

import turtle
t = turtle.Turtle()
t.pen(fillcolor="yellow")
t.begin_fill()
t.forward(100)
t.left(120)
t.forward(100)
t.left(120)
t.forward(100)
t.end_fill()
turtle.done()

Output:

Conclusion:

Turtles are good for designing basic graphics. In this article, we have learned about the turtle library which helps you to draw graphics. It is efficient and easy to implement for creating simple graphics and shapes. Software and apps for small kids can be drawn using this module. Also, providing software designs in GUI apps can also be provided using this module.

This module can also help learn about various methods which help to create basic and complex objects, and also how loops help to achieve creating visuals.


×