Python Turtle library: Different Shapes

turtle library

Hello Techies,

In this blog, we will learn how to draw different shapes with the help of the Turtle Library.

In my previous blog, I covered programs based on the shapes of squares and triangles as well as various pattern programs.

Introduction

  • Turtle is a pre-installed Python library that provides easy way to draw pictures on Windows or screen.
  • Turtle graphics is about controlling the graphical entity in a graphics window with x, y index.

Turtle Methods

Method NameDescription
Turtle()Creates and retrieves new tutorial objects
forward()forward() method moves the turtle forward by the specified amount
backward()backward() method moves the turtle backward by the specified amount
right()This turtle turns clockwise
left()The turtle turns counter-clockwise
penup()It used to pick up the turtle’s Pen
pendown()It used to put down the turtle’s Pen
up()It used to pick up the turtle’s Pen
down()It used to puts down the turtle’s Pen
color()It changes the color of the turtle’s pen
fillcolor()It changes the color of the turtle will use to fill a polygon
heading()This returns the current title
position()This returns the current position
goto()This moves the turtle to position x,y
begin_fill()This used to remember the starting point for a filled polygon
end_fill()This closes the polygon and fills the current filler color
dot()Leaves a point in the current position
stamp()Leaves a turtle-shaped impression on the current location
shape()It should be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’

Different shapes using Turtle Library

#1. Python Turtle Graphics: Drawing Indian Flag

import turtle

t = turtle.Turtle()
t.speed(0)
turtle.setup(800, 500)

t.penup()
t.goto(-400, 250)

#orange rectangle
t.color("orange")
t.begin_fill()
t.forward(800)
t.right(90)
t.forward(167)
t.right(90)
t.forward(800)
t.end_fill()

t.left(90)
t.forward(167)

#green rectangle
t.color("green")
t.begin_fill()
t.forward(167)
t.left(90)
t.forward(800)
t.left(90)
t.forward(167)
t.end_fill()

#blue circle
t.penup()
t.goto(70, 0)
t.pendown()
t.color("navy")
t.begin_fill()
t.circle(70)
t.end_fill()

#white circle
t.penup()
t.goto(60, 0)
t.pendown()
t.color("white")
t.begin_fill()
t.circle(60)
t.end_fill()

#mini blue circle
t.penup()
t.goto(-57, -8)
t.pendown()
t.color("navy")
for i in range(24):
    t.begin_fill()
    t.circle(3)
    t.end_fill()
    t.penup()
    t.forward(15)
    t.right(15)
    t.pendown()
t.penup()
t.goto(20, 0)
t.pendown()
t.begin_fill()
t.circle(20)
t.end_fill()

#spokes
t.penup()
t.goto(0, 0)
t.pendown()
t.pensize(2)
for i in range(24):
    t.forward(60)
    t.backward(60)
    t.left(15)
turtle.done()

Output:


#2. Triangle Shape in Python turtle library

import turtle

t = turtle.Turtle()
t.color('blue')
t.pensize(5)
t.hideturtle()
def triangle():
    t.begin_fill()
    for i in range(3):
        t.fd(100)
        t.left(120)
    t.end_fill()
    turtle.exitonclick()
triangle()

Output:


#3. Triangle Shape in Python turtle library

import turtle

def triangle():

    TRIANGLE_SIZE = 200
    BORDER_SIZE = 3
    BORDER_SIZE1 = 2
    STAMP_UNIT = 20

    turtle.shape("triangle")
    turtle.hideturtle()
    turtle.penup()
    turtle.right(30)  # realign triangle
    turtle.fillcolor("#fff")
    turtle.shapesize(TRIANGLE_SIZE / STAMP_UNIT, TRIANGLE_SIZE / STAMP_UNIT, BORDER_SIZE)
    turtle.stamp()

    TRIANGLE_SIZE1 = 50
    turtle.fillcolor("#fff")
    y_offset = TRIANGLE_SIZE * 1 / 4
    turtle.goto(80, -y_offset)
    turtle.shapesize(TRIANGLE_SIZE1 / STAMP_UNIT, TRIANGLE_SIZE1 / STAMP_UNIT, BORDER_SIZE1)
    turtle.stamp()

    turtle.goto(-80, -y_offset)
    turtle.shapesize(TRIANGLE_SIZE1 / STAMP_UNIT, TRIANGLE_SIZE1 / STAMP_UNIT, BORDER_SIZE1)
    turtle.stamp()

    turtle.goto(0, 90)
    turtle.shapesize(TRIANGLE_SIZE1 / STAMP_UNIT, TRIANGLE_SIZE1 / STAMP_UNIT, BORDER_SIZE1)
    turtle.stamp()
    turtle.exitonclick()

triangle()

Output:


#4. Circle Pattern in Python turtle library

import turtle

t = turtle.Turtle()
list1 = ["yellow", "red", "blue", "green"]
t.up()
t.goto(200,0)
for i in range(4):
    t.down()
    t.color(list1[i])
    t.pensize(20)
    t.circle(100)
    t.up()
    t.bk(100)
turtle.exitonclick()

Output:


#5. Python Turtle Graphics

import turtle

t = turtle.Turtle()
list1 = ["purple", "yellow", "red", "blue", "green"]
for i in range(200):
    t.color(list1[i%5])
    t.pensize(i/10+1)
    t.forward(i)
    t.left(59)
turtle.exitonclick()

Output:


#6. Rectangle Shape in Python turtle library


import turtle

t = turtle.Turtle()
t.color('blue')
t.pensize(5)
t.hideturtle()
def rectangle():
    t.begin_fill()
    for i in range(4):
        t.fd(100)
        t.left(90)
    t.end_fill()
    turtle.exitonclick()
rectangle()

Output:


#7. Circle Shape in Python turtle library


import turtle

t = turtle.Turtle()
t.color('blue')
t.pensize(5)
t.hideturtle()
def circle():
    t.begin_fill()
    t.circle(50)
    t.end_fill()
    turtle.exitonclick()
circle()

Output:


#8. Hexagon Shape in Python turtle library

import turtle

t = turtle.Turtle()
t.color('blue')
t.pensize(5)
t.hideturtle()
def hexagon():
    t.begin_fill()
    for i in range(6):
        t.fd(100)
        t.left(60)
    t.end_fill()
    turtle.exitonclick()
hexagon()

Output:


#9. Spirograph in Python turtle library

import turtle

t = turtle.Turtle()
turtle.bgcolor('black')
turtle.pensize(1)
turtle.speed(0)

for i in range(6):
    for colors in ["red","blue","yellow", "cyan","white", "purple", "green"]:
        turtle.color(colors)
        turtle.circle(100)
        turtle.left(10)
turtle.hideturtle()
turtle.exitonclick()

Output:


#10. Star Shape in Python turtle library

import turtle

t = turtle.Turtle()
t.color('red', "yellow")
for i in range(10):
    t.forward(200)
    t.left(135)
turtle.exitonclick()

Output:


#11. Flower Shape in Python turtle library

import turtle

import math
t = turtle.Turtle()
t.color('red')
t.speed(10)

for i in range(100):
    t.forward(300)
    t.left(170)

turtle.done()

Output:


#12. Python Turtle Graphics: Drawing Heart Shape

import turtle

t = turtle.Turtle()
screen = turtle.Screen()
screen.bgcolor('black')
t.color('red')
t.begin_fill()
t.fillcolor('red')
t.left(140)
t.forward(180)
t.circle(-90, 200)
t.setheading(60)
t.circle(-90, 200)
t.forward(180)
t.end_fill()
t.speed(10)
turtle.done()

Output:


FAQ on Python Turtle Library

  1. What is turtle library in Python?

    Turtle is a pre-installed Python library that provides easy way to draw pictures on Windows or screen.

  2. Is Turtle part of Python standard library?

    Yes, Python’s standard library provides a turtle module for accessing graphics packages. We need to import this module to use its functionality and methods.

  3. Can Python do graphics?

    We can draw graphics using the Python Turtle Library.

  4. How do you change the speed of a turtle?

    turtle.speed() this method is used to change the speed of the turtle. This method takes the argument to change the speed.

These are some basic shapes draw using the Turtle library. Check their official site for more information about the Turtle Library.

Leave a Comment