How to draw shapes and pictures in python turtle?



Introduction

The python turtle module is an extended reimplementation of the same-named module from the Python standard distribution up to version Python 2.5. The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Therefore, the python turtle module is used to draw pictures and shapes be they simple or complex.

Turtle graphics is a popular way of introducing programming to kids. That’s why most educators start teaching python programming by first teaching turtle graphics. It makes explaining the code syntax easy and understandable since everything will be output in the form of graphics in its own separate canvas.

In this blog post, we will first look into some of the important and needy turtle drawing methods and then we will do some activities and mini-projects explaining the coding concepts such as variables, loops, and functions.

First, you should be a little familiar with the basic methods used to customize the turtle output window and turtle state. You can check here. However, it’s not gonna confused you in understanding the drawing concept but you would feel comfortable to work by customizing the canvas and turtle (acts like a pen for drawing) as per your requirements while drawing pictures and shapes.



Methods you should know to create shapes in turtle

To start drawing pictures and shapes in python turtle, we must first know the ten methods that we will be looking into it shortly. In this blog post, we are not going to do shape color control and beautification because I am going to write separately about it in the next blog post. However, I will try to cover the coding concepts you should know as a beginner by drawing different kinds of shapes and images.

Now let us look into 10 methods you should know to start creating arts:

1. forward(distance)

Moves turtle forward a specified distance

Syntax:

forward(distance)

Example:

from turtle import *
shape("turtle")
forward(200)

Output:



2. backward(distance)

Moves turtle backward a specified distance

Syntax:

backward(distance)

Example:

from turtle import *
shape("turtle")
backward(200)

Output:

backward method in turtle
3. pensize(width=None)

Set the line thickness (line_width). If no argument is given, the default pen size is used which is 1 pixel.

Syntax:

pensize(width=None)

Example:

from turtle import *
shape("turtle")
pensize(7)
forward(200)

Output:

pensize method in turtle


4. speed()

The speed() method is used to increase/decrease the speed of turtle movement while drawing graphics on the screen. Its value ranges from 0 – 10.

  • 0 – No animation(fastest)
  • 1 – Slowest
  • 3 – Slow
  • 6 – Normal
  • 10 – Fast

Syntax:

speed(speed=None)

Example:

from turtle import *
speed(1)
shape("turtle")
pensize(5)
forward(200)

Output:

speed method in turtle
5. circle(radius, extent=None, steps=None)

Tells turtle to draw a circle with a specified radius. To draw a certain portion of the circle, the extent parameter is used. Moreover, another parameter can be used to control the points in the shape. That is by setting the value to steps.

Syntax:

circle(radius, extent=None, steps=None)

Parameters:

  • radius – a of the circle
  • extent – a number (or None) to extend the point or pixel while drawing a circle
  • steps – an integer (or None) value to mention a point while drawing a circle

Example 1 (drawing a complete circle of radius 100 and the line thickness of 5 pixels):

from turtle import *
shape("turtle")
pensize(5)
circle(100)

Output:

circle method in turtle


Example 2 (drawing semi-circle of radius 100 px with extending parameter to 180 px):

from turtle import *
shape("turtle")
pensize(5)
circle(100, 180)

Output:

semi circle in turtle

Example 3 (Drawing triangle using circle method with step parameter):

from turtle import *
shape("turtle")
pensize(5)
circle(100, 360, 3)

Output:

Triangle using circle method in turtle
6. left(angle)

It is used to rotate the turtle in an anticlockwise direction by a specific degree or angle.

Syntax:

left(angle)

Example:

from turtle import *
shape("turtle")
shapesize(5)  # increase turtle size by 5
left(90)

Output:

left method in turtle


7. right(angle)

It is used to rotate the turtle in a clockwise direction by a specific degree or angle.

Syntax:

right(angle)

Example:

from turtle import *
shape("turtle")
shapesize(5)
right(90)

Output:

right method in turtle
8. penup()

Every time when you move a turtle for a specific distance, the turtle trail is left. In other words, the turtle trail is drawn. At some point, we have to move the turtle to a certain distance before drawing a graphic on the screen. So, in such situations we have to make use of the penup() method to move the turtle to a specified location without drawing/leaving the trail on the screen.

Syntax:

penup()

Example:

from turtle import *
shape("turtle")
pensize(5)
forward(100)
penup() # pen will be lifted
forward(100)

Output:

penup method in turtle
Turtle draws a line for the first 100 px forward and the next 100px forward will be drawn lifting a pen where we won’t be able to see the line


9. pendown()

It will put down the pen, leaving a trail when the turtle moves.

Syntax:

pendown()

Example:

from turtle import *
shape("turtle")
pensize(5)
forward(100)
penup() 
forward(50)
pendown()
forward(50)

Output:

pendown method in turtle
10. goto(x, y=None) | setpos(x, y=None) | setposition(x, y=None)

Moves the turtle to an absolute position of x and y coordinates. If the pen is down, she draws a line.

Before we set a position of the turtle, we have to know how to read a turtle grid square. Initially, a turtle will be at (0,0). To know about turtle grid squares carefully study the image attached below.

turtle grid square

Syntax (we can use any method that is shown below to set the position of the turtle):

goto(x, y=None)
setpos(x, y=None)
setposition(x, y=None)

Example:

from turtle import *
shape("turtle")
pensize(5)
circle(30)
goto(100, 0) # x=100, y=0
circle(30)

Output:

goto in turtle

Great! now you know all the methods that will help you to create different types of shapes and pictures. Let’s try to do some activity.



Activity

While we do some activities creating different kinds of shapes and pictures, we will also learn the coding concept which will help you to learn python programming at the beginner level.

1. Square in turtle

Question: Write a program that has the turtle draw a square with sides of 50px?

We can draw a square in two ways in a turtle ie. using for loop and without for loop. let’s look into it one by one.

Example (Square without for loop):

from turtle import *
shape("turtle")
pensize(5)
forward(50)
left(90)
forward(50)
left(90)
forward(50)
left(90)
forward(50)

Output:

square in turtle

Example (Drawing square using for loop):

In the above example, just to draw a square we have used 10 lines of code. However, we can minimize the lines of code using for loop. For loop are used to iterate over the items of any sequence by controlling the iteration using range function. We could see that the forward(90) and left(90) commands have been repeated four times. Therefore, we will take advantage of for loop and range function to make it repeat the execution of these two lines four times without writing the commands again and again.

from turtle import *
shape("turtle")
pensize(5)
for i in range(4):
    forward(50)
    left(90)

Output:

Square in turtle

Why we have used forward(50) and left(90) while drawing squares?

The forward(50) is to mention the size of the square. Since all four sides of the square should be equal we have kept 50px for all forward commands.

The left(90) is to turn the turtle 90 degrees once she moved drawing a forward line of 50px. We know that the angle for the square is exactly 90 degrees (hint to find the angle of the shape: 360/total number of sides). That’s why left(90) commands are used. However, you can also use right(90). The only difference is the square will be drawn in a clockwise direction.



2. Rectangle in turtle

Code:

from turtle import *
shape("turtle")
pensize(5)
for i in range(2):
    forward(100)
    left(90)
    forward(50)
    left(90)

Output:

rectangle in turtle
3. Triangle in turtle

Code:

from turtle import *
shape("turtle")
pensize(5)
for i in range(3):
    forward(100)
    left(120)

Output:

triangle in turtle


4. Pentagon in turtle

Code:

from turtle import *
shape("turtle")
pensize(5)
for i in range(5):
    forward(100)
    left(72)

Output:

pentagon in turtle
5. Hexagon in turtle

Code:

from turtle import *
shape("turtle")
pensize(5)
for i in range(6):
    forward(100)
    left(60)

Output:

hexagon in turtle


6. Octagon in turtle

Code:

from turtle import *
shape("turtle")
pensize(5)
for i in range(8):
    forward(100)
    left(45)

Output:

octagon in turtle
7. Asterisk in turtle

Question: Write a program that has the turtle draw an asterisk with lines 100 pixels long.

Code:

from turtle import *
shape("turtle")
speed(1)
pensize(5)
left(30)
for i in range(6):
    forward(100)
    backward(100)
    left(60)

Output:

asterisk in turtle


8. x-axis and y-axis

Question: Write a program that has the turtle draw an x-axis and y-axis on the screen with hash marks every 25 pixels.

Code:

from turtle import *
shape("turtle")
speed(0)
pensize(3)

def draw_hashed_axis():
    '''
    This function will draw a line
    with hash marks at every 25 pixels.
    '''
    pendown()
    for i in range(16):
        forward(25)
        right(90)
        forward(10)
        backward(20)
        forward(10)
        left(90)
       
# Move turtle to top of canvas and call draw hash marks function for y-axis.
# Then move turtle to left of canvas and call hash marks function for x-axis.
penup()
setposition(0,200)
right(90)
draw_hashed_axis()
penup()
setposition(-200,0)
left(90)
draw_hashed_axis()

Output:

drawing x-axis and y-axis in turtle

In this activity, we are including another concept called function to organize the code. Basically, functions are used to minimize the usage of blocks of code. To define a function we have to use a def keyword followed by the function name (draw_hashed_axis is a function name in the above code). Once you have defined a function then it should include the statements of the task that needs to be executed by the function whenever it was called. The for loop with certain lines of code are the function statements in the above example which will draw a hash line. So, when we have finished setting up the position of the turtle to draw the x-axis and y-axis, we are calling a draw_hashed_axis() function to execute its statements that will draw a hash line.

To understand more about the functions in python click here.



Mini project
1. Your first name

Question: Write a program that has turtle draw your first name

Your First Name project is quite simple but will be a lot of fun to do. I gave this as a mini-project to my students and I see a lot of joy when they were able to draw their first name in turtle graphics. You must try this too. So, in this mini-project, we will share with you how to draw the name “DAWA”.

We would challenge you to draw your first name using the goto() or setposition() command which we have not used in our code. Setting a position of the turtle while drawing your name will minimize the lines of code. You must give it a try.

Code:

from turtle import *

penup()
backward(200)
pensize(5)
shape("turtle")
pendown()

# Alphabet D
right(90)
pensize(7)
forward(100)
left(90)
forward(50)
left(45)
forward(15)
left(45)
forward(80)
left(45)
forward(15)
left(45)
forward(50)

# Alphabet A
penup()
right(180)
forward(130)
pendown()
pensize(7)
right(120)
forward(115)
backward(115)
left(60)
forward(115)
backward(50)
right(120)
forward(60)

#Alphabet W
penup()
backward(100)
left(90)
pendown()
forward(40)
backward(100)
forward(100)
right(220)
forward(50)
right(100)
forward(50)
right(220)
forward(100)
right(90)
penup()
forward(70)

# Alphabet A
pendown()
pensize(7)
right(120)
forward(115)
backward(115)
left(60)
forward(115)
backward(50)
right(120)
forward(60)

Output:

Your first name in turtle


2. Stupa

Question: Draw a stupa in python turtle graphics

To draw a stupa we have used a for loop and functions to make our code well organize and to minimize the usage of repeated lines of code.

Code:

from turtle import*
penup()
speed(8)
pensize(3)
def draw_rect(x,y,i):
    goto(x,y)
    pendown()
    for x in range(2):
        forward(300-i)
        left(90)
        forward(20)
        left(90)
    penup()
def draw_rect_top(x,y,i):
    goto(x,y)
    pendown()
    for x in range(2):
        forward(300-i)
        left(90)
        forward(10)
        left(90)
    penup()
draw_rect(-100,-200,0)
draw_rect(-80,-180,40)
draw_rect(-60,-160,80)

setpos(-40,-140)
pendown()
for x in range(2):
    forward(180)
    left(90)
    forward(100)
    left(90)
penup()

draw_rect_top(-50,-40,100)
draw_rect_top(-60,-30,80)
draw_rect_top(-70,-20,60)

draw_rect_top(-60,-10,80)
draw_rect_top(-50,00,100)

setpos(-20,10)
pendown()
forward(140)
left(70)
forward(100)
left(70)
forward(30)
left(40)
forward(165)
left(50)
forward(30)
left(60)
forward(96)
left(70)
penup()
draw_rect_top(-5,123,195)
setpos(10,133)
for i in range(2):
    pendown()
    forward(75)
    left(90)
    forward(20)
    left(90)
penup()
draw_rect_top(-5,153,195)
draw_rect_top(5,163,215)
draw_rect_top(8,173,221)
draw_rect_top(11,183,227)
draw_rect_top(14,193,233)
draw_rect_top(17,203,239)
draw_rect_top(20,213,245)
draw_rect_top(23,223,251)
draw_rect_top(26,233,257)
draw_rect_top(29,243,263)
draw_rect_top(32,253,269)
setpos(23,263)
pendown()
forward(49)
left(110)
forward(30)
right(60)
forward(15)
left(130)
forward(47)
left(130)
forward(15)
right(60)
forward(30)
penup()
setpos(48,302)
left(110)
pendown()
circle(20)
penup()
hideturtle()

Output:

stupa in turtle
Conclusion

Congratulations! now you know how to draw shapes and pictures in python turtle graphics. We would recommend you to try out drawing different creative pictures which will really help you to practice coding and think logically. In the next blog post, we will write about putting colors in your pictures. Can’t wait to help you make your image beautiful.