Courses / Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course

3.4 Turtle Graphics: Drawing Shapes with Python

Unit 3 – Programming Fundamentals · 70 min read · 9 MCQs to check yourself

Python Turtle graphics tutorial #1 for beginners

1. The idea

A turtle is a small arrow on a canvas. The screen centre is (0, 0); x grows to the right, y grows upwards. When the turtle moves with its pen down it draws a line. At the start it faces east (heading 0°).

2. Important methods

Category Method Action
Motion forward(d) / fd(d) Move forward d pixels
backward(d) / bk(d) Move backward
right(a) / rt(a), left(a) / lt(a) Turn clockwise / anticlockwise by a degrees
goto(x, y) Move to a point
setheading(a) Face a direction (0 east, 90 north, 180 west, 270 south)
circle(r) Draw a circle of radius r (circle(r, 180) draws a semicircle)
home() Return to (0, 0) facing east
Pen penup() / pendown() Stop / start drawing while moving
pensize(w), pencolor(c) Line width and colour
color(pen, fill), fillcolor(c) Set colours
begin_fill() … end_fill() Fill the shape drawn in between
Turtle state shape("turtle"), speed(0–10), hideturtle() Look and speed (0 = fastest)
write("text", font=("Arial", 16, "bold")) Write text on the canvas
Screen Screen(), bgcolor(c), title(t), setup(w, h) Window settings
done() / mainloop() / exitonclick() Keep the window open
Events onkey(func, "Up"), listen() Call a function when a key is pressed
onclick(func), onscreenclick(func) Call a function when the mouse is clicked
ontimer(func, ms) Call a function after a time

3. Simple shapes

Rule: to draw a regular polygon with n sides, repeat n times: forward, then turn by the exterior angle (360°)/n (square 90°, triangle 120°, hexagon 60°).

import turtle
t = turtle.Turtle()
for i in range(4):
    t.forward(120)
    t.left(90)
turtle.done()
import turtle
t = turtle.Turtle()
t.pencolor("orange")
for i in range(5):
    t.forward(200)
    t.right(144)
turtle.done()
import turtle
t = turtle.Turtle()
colors = ["red", "orange", "green", "blue", "purple"]
x = 0
for i, n in enumerate([3, 4, 5, 6, 8]):
    t.penup()
    t.goto(x, 0)
    t.setheading(0)
    t.pendown()
    t.pencolor(colors[i])
    for side in range(n):
        t.forward(70)
        t.left(360 / n)
    x += 140
turtle.done()

4. Compound shapes – a house

A compound shape is made of several simple shapes. Lift the pen (penup) to move to the start of the next part without drawing a line.

import turtle
t = turtle.Turtle()
t.pensize(3)
t.fillcolor("khaki")
t.begin_fill()
for i in range(4):          # walls
    t.forward(150)
    t.left(90)
t.end_fill()
t.penup(); t.goto(0, 150); t.pendown()
t.setheading(60)
t.fillcolor("salmon")
t.begin_fill()              # roof (triangle)
t.forward(150)
t.right(120)
t.forward(150)
t.end_fill()
t.penup(); t.goto(55, 0); t.setheading(90); t.pendown()
t.fillcolor("brown")
t.begin_fill()              # door
t.forward(70); t.right(90); t.forward(40); t.right(90); t.forward(70)
t.end_fill()
turtle.done()

5. Patterns with loops

import turtle
t = turtle.Turtle()
t.speed(0)
colors = ["red", "orange", "green", "blue", "purple"]
for i in range(60):
    t.pencolor(colors[i % 5])
    t.forward(i * 4)
    t.left(91)
turtle.done()
import turtle
t = turtle.Turtle()
for i in range(12):
    t.circle(60)
    t.left(30)
turtle.done()

6. Events – move the turtle with arrow keys

import turtle
screen = turtle.Screen()
screen.title("Move me with arrow keys")
t = turtle.Turtle(shape="turtle")

def up():
    t.setheading(90); t.forward(20)
def down():
    t.setheading(270); t.forward(20)
def left():
    t.setheading(180); t.forward(20)
def right():
    t.setheading(0); t.forward(20)
def jump(x, y):           # mouse click: move without drawing
    t.penup(); t.goto(x, y); t.pendown()

screen.onkey(up, "Up")
screen.onkey(down, "Down")
screen.onkey(left, "Left")
screen.onkey(right, "Right")
screen.onclick(jump)
screen.listen()           # start listening for key presses
turtle.done()

Python Turtle graphics tutorial #2 – shapes and fills

Short and long questions

1. Write Python turtle code to draw a square of side 100.

Model answer
import turtle
t = turtle.Turtle()
for i in range(4):
    t.forward(100)
    t.left(90)
turtle.done()

2. Explain any three turtle motion methods and two pen methods.

Model answer

Motion: forward(d) moves d pixels in the current direction; right(a)/left(a) turn by a degrees; goto(x, y) moves to a coordinate. Pen: penup() lifts the pen so moving does not draw; pencolor("red") sets the line colour (also pensize(w) for width).

3. How can you create a compound shape in turtle? Why are penup() and pendown() needed?

Model answer

A compound shape is drawn by combining simple shapes (square, triangle, circle), e.g. a house = square walls + triangle roof + rectangle door. Between parts, penup() lifts the pen so the turtle can move (with goto) to the next starting point without drawing an unwanted line; pendown() starts drawing again.

4. Write turtle code to draw a filled red triangle.

Model answer
import turtle
t = turtle.Turtle()
t.fillcolor("red")
t.begin_fill()
for i in range(3):
    t.forward(150)
    t.left(120)
t.end_fill()
turtle.done()

5. What is an event in turtle graphics? Give an example.

Model answer

An event is an action such as a key press, mouse click or timer that makes the program call a function (event handler). Example: screen.onkey(up, "Up") and screen.listen() call the function up() whenever the Up arrow key is pressed; screen.onclick(func) calls func(x, y) when the mouse is clicked.

6. Write a Python turtle program that draws a compound shape (a house with a square base, triangular roof and a door). Explain the turtle and screen methods used.

Model answer
import turtle
screen = turtle.Screen()
screen.bgcolor("lightblue")
t = turtle.Turtle()
t.pensize(3)
t.fillcolor("khaki")
t.begin_fill()
for i in range(4):
    t.forward(150)
    t.left(90)
t.end_fill()
t.penup(); t.goto(0, 150); t.pendown()
t.setheading(60)
t.fillcolor("salmon")
t.begin_fill()
t.forward(150); t.right(120); t.forward(150)
t.end_fill()
t.penup(); t.goto(55, 0); t.setheading(90); t.pendown()
t.forward(70); t.right(90); t.forward(40); t.right(90); t.forward(70)
turtle.done()

Methods: Screen() creates the window and bgcolor() sets its colour; forward()/left()/right() move and turn; penup(), goto(), pendown() move to a new starting point without drawing; setheading() sets direction; pensize()/fillcolor() style the drawing; begin_fill()/end_fill() fill shapes; done() keeps the window open.

This lesson ends with a quiz of 9 MCQs. Open the lesson on PrepOasis to take it and save your progress.

Previous: 3.3 Control Structures: Sequence, Selection and Repetition · Next: 3.5 Python Libraries: Using Code Others Have Written

All lessons in Computer Science XI (HSSC-I) – FBISE Complete SLO-Based Course