Python Turtle Graphics Overview
In this post, you will learn about the Python turtle graphics.
Turtle is a popular graphics feature of Python. It is like a drawing board on which we can command a turtle (or cursor) to draw all over it. It was developed by Wally Feurzeig, Seymour Parpet, and Cynthina Slolomon in 1967. It has many different methods that are used in turtle motion, drawing states, positioning, color filling, controlling animations, controlling the screen and much more.
The turtle graphics are basically used in the Logo Programming Language. Every movement of a turtle is based on a command. This is basically used to draw different shapes like squares, circles, rectangles, spiral draw and other composite graphs. Once we import Turtle, we can give commands like forward, backward, right, left, etc.
Implement Python Turtle
Turtle is pre-packed with standard versions of Python (>2.5). We only need to import this module in our python file.
import turtle
Next, the turtle needs to be initialised using the following commands-
turtle.Turtle()
Turtle Commonly Used Methods
These are the basic most commonly used turtle methods.
METHOD |
DESCRIPTION |
forward(distance) | To move the turtle forward to the given distance, in the turtle headed direction. |
backward(distance) | To move the turtle backward to the given distance, in the turtle headed the opposite direction. |
left(angle) | To turn turtle left to the given angle units. |
right(angle) | To turn turtle right to the given angle units. |
setx(x) | To set the first coordinate of the turtle. The "x" is either an integer or a float. |
sety(y) | To set the second coordinate of the turtle. The "y" is either an integer or a float. |
circle(radius) | To draw a circle with the given radius. |
speed(speed) | To set the turtle speed to a given integer value. The speed value is in the range from 1 to 10. |
stamp() | A copy of the turtle shape at the current location. It returns a stamp_id, which can be further use for modification. |
position() | To get the turtle current location (x,y). |
distance(x,y) | It returns the distance from turtle current position to the given coordinate (x,y). |
dot(size, color) | To draw a circular dot with given dimensional size and using color. |
up() | To pull the pen up. |
down() | TO pull the pen down. |
pencolor(colorstring) | The colorstring is an optional parameter. If it does not set then it returns the current pen color, otherwise it sets the given colorstring. |
shape(shapename) | Here, shapename is an optional parameter. It sets the shape with given shapename or, if shapename is not given, returns the current shapename. |
Draw different shapes using Python Turtle
Draw Square
For drawing a square, we probably need to turn at the right angle, or 90 degrees.
import turtle
t = turtle.Turtle()
for _ in range(4):
# forward turtle 50 units
t.forward(50)
# turn turtle by 90 degree
t.left(90)
Draw Rectangle
To draw a rectangle, we repeat the for loop two times and turn the edges at the right angle.
import turtle
t = turtle.Turtle()
for _ in range(2):
t.forward(150)
t.left(90)
t.forward(100)
t.left(90)
Draw Circle
import turtle
t = turtle.Turtle()
#circle radius
r = 100
t.circle(r)
Draw Triangle
To draw a triangle, we have iterated the loop three times and turned at 120 degrees.
import turtle
t = turtle.Turtle()
for i in range(3):
# forward turtle 100 units
t.forward(100)
# turn turtle by 120 degree
t.right(120)
Draw Polygon
import turtle
t = turtle.Turtle()
for i in range(5):
# forward turtle 100 units
t.forward(100)
# turn turtle by 72 degree
t.right(72)
Draw Spiral Circle
import turtle
t = turtle.Turtle()
for i in range(5):
# forward turtle 100 units
t.forward(100)
# turn turtle by 72 degree
t.right(72)
Draw Color Filled Star
Here, we have first drawn the star shape and then filled it with red color using fillcolor() method.
import turtle
t = turtle.Turtle()
t.fillcolor('red')
t.begin_fill()
for i in range(5):
t.forward(150)
t.right(144)
t.end_fill()
Turtle Pencolor Example
import turtle
t = turtle.Turtle()
t.fillcolor('red')
t.begin_fill()
for i in range(5):
t.forward(150)
t.right(144)
t.end_fill()
Turtle Draw With Speed Limit
The above examples are simple and easy to understand. Here, we have drawn a little more complex shape using a python turtle.
import turtle
t = turtle.Turtle()
t.pencolor("blue")
t.speed(10)
for i in range(100):
t.forward(50)
t.right(30)
t.forward(20)
t.left(60)
t.forward(40)
t.right(30)
t.forward(10)
t.left(10)
t.penup()
t.setposition(0, 0)
t.pendown()
t.right(2)
turtle.done()
Related Articles
Convert list to dictionary PythonDictionary inside list Python
Pandas string to datetime
Convert Excel to CSV Python Pandas
Python add list to list
Python Pandas Dataframe to CSV
Python compare two lists
Remove element from list Python
Python iterate list with index
Python program to sum all the numbers in a list
Python print without newline
Python iterate list with index
How to draw different shapes using Python Pygame
Python send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml