Python turtle and tkinter

Build a Town

Write your own turtle commands, called functions, and use them to build a street of houses.

Day 3 50 to 60 minutes 8 steps Key Stage 3
Start step 1

Drawing a house takes a lot of code. Drawing ten houses would take ten times as much. A function is a command you make yourself: write the code once, give it a name, and use the name whenever you need it. Today you will make a rectangle, a triangle, a house, a tree and a sun, then build a town with them.

You will learn

  • Make your own commands with def
  • Give a function parameters, so it can draw different things
  • Use functions inside other functions
  • Plan a picture with x and y coordinates

You will need

  • A computer with Thonny or IDLE, or this page's Run it here button
  • Day 1's project, for goto and filling shapes

Words to know

function
A command you make yourself, with def. Write it once, use it many times.
parameter
A name in a function's brackets that takes a different value each time it is used.
call
Using a function: house(-250, -50, "tomato").
decomposition
Breaking a big problem into smaller ones: a town into houses, a house into shapes.

Step by step

  1. Step 1: Start a new file with a sky

    Click New, save the file as town.py, and type this. speed(0) makes the turtle draw at full speed, and the background is the colour of the sky.

    town.pyPython
    import turtle t = turtle.Turtle()t.speed(0)turtle.bgcolor("skyblue") turtle.done()
    An empty blue sky, with the turtle in the middle.
  2. Step 2: Make a rectangle command

    def defines a new function called rectangle. The names in brackets are its parameters: the width, the height and the colour, which are different every time you use it. The code indented under def is what the function does. It only runs when you call the function, as the last new line does.

    New: lines 7 to 18.

    town.pyPython
    import turtle t = turtle.Turtle()t.speed(0)turtle.bgcolor("skyblue") def rectangle(width, height, colour):    t.color("black", colour)    t.begin_fill()    for i in range(2):        t.forward(width)        t.left(90)        t.forward(height)        t.left(90)    t.end_fill() rectangle(100, 80, "tomato") turtle.done()
    rectangle(100, 80, "tomato") draws a red rectangle 100 wide and 80 high.
    Watch out The function must come before the line that calls it. Python reads from the top down.
  3. Step 3: Lay the ground

    Moving without drawing takes three lines, so make that a function too: move(x, y). Then use both functions to lay a wide green strip of ground at the bottom. Delete the red rectangle line.

    New: lines 17 to 23.

    town.pyPython
    import turtle t = turtle.Turtle()t.speed(0)turtle.bgcolor("skyblue") def rectangle(width, height, colour):    t.color("black", colour)    t.begin_fill()    for i in range(2):        t.forward(width)        t.left(90)        t.forward(height)        t.left(90)    t.end_fill() def move(x, y):    t.penup()    t.goto(x, y)    t.pendown() move(-400, -150)rectangle(800, 100, "green") turtle.done()
    A strip of green ground across the bottom of the sky.
  4. Step 4: Add a roof

    Add a triangle function, then draw the walls of a house standing on the ground, and a brown roof on top of the walls. The ground's top edge is at y = 50 below the middle, so the walls start at move(-250, -50).

    New: lines 22 to 29 and 33 to 37.

    town.pyPython
    import turtle t = turtle.Turtle()t.speed(0)turtle.bgcolor("skyblue") def rectangle(width, height, colour):    t.color("black", colour)    t.begin_fill()    for i in range(2):        t.forward(width)        t.left(90)        t.forward(height)        t.left(90)    t.end_fill() def move(x, y):    t.penup()    t.goto(x, y)    t.pendown() def triangle(size, colour):    t.color("black", colour)    t.begin_fill()    for i in range(3):        t.forward(size)        t.left(120)    t.end_fill() move(-400, -150)rectangle(800, 100, "green") move(-250, -50)rectangle(100, 80, "tomato")move(-250, 30)triangle(100, "brown") turtle.done()
    Walls and a roof: one house.
  5. Step 5: A house command

    Now wrap the walls, the roof and a door into one function called house. Its parameters say where the house goes and what colour its walls are. y + 80 is 80 above the bottom of the walls, the top of the walls, where the roof goes. Replace the four lines that drew the house with one call to house.

    New: lines 30 to 37 and 41.

    town.pyPython
    import turtle t = turtle.Turtle()t.speed(0)turtle.bgcolor("skyblue") def rectangle(width, height, colour):    t.color("black", colour)    t.begin_fill()    for i in range(2):        t.forward(width)        t.left(90)        t.forward(height)        t.left(90)    t.end_fill() def move(x, y):    t.penup()    t.goto(x, y)    t.pendown() def triangle(size, colour):    t.color("black", colour)    t.begin_fill()    for i in range(3):        t.forward(size)        t.left(120)    t.end_fill() def house(x, y, colour):    move(x, y)    rectangle(100, 80, colour)    move(x, y + 80)    triangle(100, "brown")    move(x + 40, y)    rectangle(20, 40, "saddlebrown") move(-400, -150)rectangle(800, 100, "green") house(-250, -50, "tomato") turtle.done()
    One call to house() draws the walls, the roof and the door.
  6. Step 6: Build a street

    This is where functions pay off: two more houses take two more lines.

    New: lines 42 and 43.

    town.pyPython
    import turtle t = turtle.Turtle()t.speed(0)turtle.bgcolor("skyblue") def rectangle(width, height, colour):    t.color("black", colour)    t.begin_fill()    for i in range(2):        t.forward(width)        t.left(90)        t.forward(height)        t.left(90)    t.end_fill() def move(x, y):    t.penup()    t.goto(x, y)    t.pendown() def triangle(size, colour):    t.color("black", colour)    t.begin_fill()    for i in range(3):        t.forward(size)        t.left(120)    t.end_fill() def house(x, y, colour):    move(x, y)    rectangle(100, 80, colour)    move(x, y + 80)    triangle(100, "brown")    move(x + 40, y)    rectangle(20, 40, "saddlebrown") move(-400, -150)rectangle(800, 100, "green") house(-250, -50, "tomato")house(-100, -50, "gold")house(150, -50, "violet") turtle.done()
    Three houses, each a different colour.
  7. Step 7: Plant some trees

    A tree is a brown trunk with a green circle on top. circle(30) draws a circle with the turtle on its bottom edge, so it sits on top of the trunk.

    New: lines 38 to 46, 53 and 54.

    town.pyPython
    import turtle t = turtle.Turtle()t.speed(0)turtle.bgcolor("skyblue") def rectangle(width, height, colour):    t.color("black", colour)    t.begin_fill()    for i in range(2):        t.forward(width)        t.left(90)        t.forward(height)        t.left(90)    t.end_fill() def move(x, y):    t.penup()    t.goto(x, y)    t.pendown() def triangle(size, colour):    t.color("black", colour)    t.begin_fill()    for i in range(3):        t.forward(size)        t.left(120)    t.end_fill() def house(x, y, colour):    move(x, y)    rectangle(100, 80, colour)    move(x, y + 80)    triangle(100, "brown")    move(x + 40, y)    rectangle(20, 40, "saddlebrown") def tree(x, y):    move(x, y)    rectangle(16, 40, "saddlebrown")    move(x + 8, y + 40)    t.color("darkgreen")    t.begin_fill()    t.circle(30)    t.end_fill() move(-400, -150)rectangle(800, 100, "green") house(-250, -50, "tomato")house(-100, -50, "gold")house(150, -50, "violet")tree(50, -50)tree(300, -50) turtle.done()
    Two trees between the houses.
  8. Step 8: Here comes the sun

    Add a sun function and put the sun in the sky. Hide the turtle at the end.

    New: lines 47 to 53, 62 and 64.

    town.pyPython
    import turtle t = turtle.Turtle()t.speed(0)turtle.bgcolor("skyblue") def rectangle(width, height, colour):    t.color("black", colour)    t.begin_fill()    for i in range(2):        t.forward(width)        t.left(90)        t.forward(height)        t.left(90)    t.end_fill() def move(x, y):    t.penup()    t.goto(x, y)    t.pendown() def triangle(size, colour):    t.color("black", colour)    t.begin_fill()    for i in range(3):        t.forward(size)        t.left(120)    t.end_fill() def house(x, y, colour):    move(x, y)    rectangle(100, 80, colour)    move(x, y + 80)    triangle(100, "brown")    move(x + 40, y)    rectangle(20, 40, "saddlebrown") def tree(x, y):    move(x, y)    rectangle(16, 40, "saddlebrown")    move(x + 8, y + 40)    t.color("darkgreen")    t.begin_fill()    t.circle(30)    t.end_fill() def sun(x, y):    move(x, y)    t.color("orange", "yellow")    t.begin_fill()    t.circle(40)    t.end_fill() move(-400, -150)rectangle(800, 100, "green") house(-250, -50, "tomato")house(-100, -50, "gold")house(150, -50, "violet")tree(50, -50)tree(300, -50)sun(250, 150) t.hideturtle()turtle.done()
    The finished town: three houses, two trees and the sun.

Test it

Tick each one when it works. If one does not, look back at the step it comes from.

Stretch challenges

Finished early? Try these. Each one has a hint if you get stuck.

  1. Windows

    Add two windows to the house function, so every house gets them at once.

    Show a hint

    Inside house, move to a spot inside the walls and draw a small rectangle(20, 20, "lightyellow"). Try move(x + 10, y + 45) and move(x + 70, y + 45).

  2. A long street

    Use a loop to draw a street of six houses, each a different colour.

    Show a hint

    Make a list of six colours. Then for i in range(6): and house(-380 + i * 130, -50, colours[i]) spaces the houses 130 steps apart.

    Show one answer
    town.pyPython
    import turtle t = turtle.Turtle()t.speed(0)turtle.bgcolor("skyblue") def rectangle(width, height, colour):    t.color("black", colour)    t.begin_fill()    for i in range(2):        t.forward(width)        t.left(90)        t.forward(height)        t.left(90)    t.end_fill() def move(x, y):    t.penup()    t.goto(x, y)    t.pendown() def triangle(size, colour):    t.color("black", colour)    t.begin_fill()    for i in range(3):        t.forward(size)        t.left(120)    t.end_fill() def house(x, y, colour):    move(x, y)    rectangle(100, 80, colour)    move(x, y + 80)    triangle(100, "brown")    move(x + 40, y)    rectangle(20, 40, "saddlebrown") move(-400, -150)rectangle(800, 100, "green") colours = ["tomato", "gold", "violet", "orange", "pink", "lightgreen"]for i in range(6):    house(-380 + i * 130, -50, colours[i]) t.hideturtle()turtle.done()
  3. Clouds

    Write a cloud(x, y) function from three white circles side by side, and put clouds in the sky.

    Show a hint

    Draw three filled white circles, each one 30 steps to the right of the last: move(x, y), move(x + 30, y) and move(x + 60, y).

The finished code

Show the whole program
town.pyPython
import turtle t = turtle.Turtle()t.speed(0)turtle.bgcolor("skyblue") def rectangle(width, height, colour):    t.color("black", colour)    t.begin_fill()    for i in range(2):        t.forward(width)        t.left(90)        t.forward(height)        t.left(90)    t.end_fill() def move(x, y):    t.penup()    t.goto(x, y)    t.pendown() def triangle(size, colour):    t.color("black", colour)    t.begin_fill()    for i in range(3):        t.forward(size)        t.left(120)    t.end_fill() def house(x, y, colour):    move(x, y)    rectangle(100, 80, colour)    move(x, y + 80)    triangle(100, "brown")    move(x + 40, y)    rectangle(20, 40, "saddlebrown") def tree(x, y):    move(x, y)    rectangle(16, 40, "saddlebrown")    move(x + 8, y + 40)    t.color("darkgreen")    t.begin_fill()    t.circle(30)    t.end_fill() def sun(x, y):    move(x, y)    t.color("orange", "yellow")    t.begin_fill()    t.circle(40)    t.end_fill() move(-400, -150)rectangle(800, 100, "green") house(-250, -50, "tomato")house(-100, -50, "gold")house(150, -50, "violet")tree(50, -50)tree(300, -50)sun(250, 150) t.hideturtle()turtle.done()
Notes for the teacher

Timing

  1. 10 minutes: steps 1 and 2. Spend time on the idea of parameters: the same function, different values.
  2. 25 minutes: steps 3 to 6.
  3. 15 minutes: steps 7 and 8, then the tests.

Mistakes to look out for

  1. Calling a function before its def: NameError: name 'house' is not defined. Functions go at the top.
  2. Forgetting the brackets on a call: house on its own does nothing at all.
  3. The wrong number of values in a call: TypeError: house() missing 1 required positional argument: 'colour'.
  4. Code indented under def by mistake, such as the calls at the bottom: then nothing is drawn, because the calls are now inside a function that is never called.

Going further

This is decomposition in action: a town is broken into houses, a house into rectangles and triangles. Ask students to sketch their town on squared paper first, with coordinates, before they code it.