←Previous page

13TURTLE DRAWING · ART

Functions paint a picture.

Put each drawing in a function. Then call them to build a house, a sun and trees.

IN REAL LIFE

Make it once, use it often.

Artists and builders reuse pieces all the time.

A cookie cutter

One cutter, many cookies. Each cookie can get a different icing.

Building blocks

Build a wall piece and a roof piece, then snap them into a house.

Coloring a page

First draw the outline, then fill the inside with color.

LOOK FIRST

Three tools for art.

A function draws

def square(size, fill) keeps the steps. square(50, "red") draws one.

Fill it in

color("black", "yellow"): black pen, yellow inside. begin_fill() and end_fill() wrap the shape.

Jump with goto

penup(), goto(x, y), pendown() moves the turtle to a new spot without a line.

LOOK FIRST

Read it line by line.

A painted square on the left, a jump on the right.

def square(size, fill):A function with two parameters: how big, and which paint.
t.color("black", fill)Black pen. The inside gets the color in fill.
t.begin_fill()Starts the shape that will get painted.
for i in range(4):The square loop from the last page.
t.forward(size)Each side is as long as size.
t.left(90)One corner.
t.end_fill()Pours the paint inside the square.
def move(x, y):A helper to jump to any spot.
t.penup()Lift the pen: no line while jumping.
t.goto(x, y)Go to x (sideways) and y (up or down). The middle is (0, 0).
t.pendown()Pen down, ready to draw.
move(-50, 20)Jumps to 50 left and 20 up.
square(60, "pink")Draws a pink square there.

The screen: It is 400 steps wide and 400 tall. x goes from -200 to 200, and y too.

RUN IT

PyBot paints a little scene.

Guess first. Then run real Python and check.

1

Guess the result

import turtle t = turtle.Turtle() turtle.bgcolor("lightblue") def move(x, y): t.penup() t.goto(x, y) t.pendown() def square(size, fill): t.color("black", fill) t.begin_fill() for i in range(4): t.forward(size) t.left(90) t.end_fill() def roof(size, fill): t.color("black", fill) t.begin_fill() for i in range(3): t.forward(size) t.left(120) t.end_fill() move(-150, -120) square(120, "orange") move(-150, 0) roof(120, "red") move(100, 50) t.color("orange", "gold") t.begin_fill() t.circle(45) t.end_fill()

What will the turtle draw?

art.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Add two lines at the end: move(-110, -120), then square(40, "brown") for a door.

YOUR TURN

Build your scene.

Try each tiny question. A wrong answer becomes something to review.

0Completed
13Not tried
0Review
01

Like a function in real life

A function keeps drawing steps so you can use them again.

Which one works like a drawing function?

02

Only the def

def triangle(): for i in range(3): t.forward(80) t.left(120)

What does the turtle draw?

03

Two sizes

square(30, "red") square(90, "blue")

Which square is bigger?

04

Pen and fill

t.color("black", "yellow") t.begin_fill() t.circle(40) t.end_fill()

What does the circle look like?

05

A missing line

t.color("black", "green") t.begin_fill() for i in range(4): t.forward(60) t.left(90)

There is no end_fill(). What do you see?

06

Jump to a spot

t.penup() t.goto(100, 50) t.pendown()

Where does the turtle go?

07

goto with the pen down

t.forward(50) t.goto(-80, 60)

Does goto() draw a line?

08

A new background

turtle.bgcolor("navy")

What changes?

09

How many houses?

move(-150, 0) house() move(50, 0) house()

How many houses appear?

10

Fix PyBot's code

The square should be painted yellow, but the paint never pours in. Add the missing line.

Goal drawingYour drawingOUTPUT

11

Fix PyBot's code

PyBot moved the turtle to the sky, but no sun appears. Make the sun show up.

Goal drawingYour drawingOUTPUT

12

Fix PyBot's code

Two trees should stand apart, the second one at x = 80. Both land in the same spot. Fix the second call.

Goal drawingYour drawingOUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

Functions turn drawings into building blocks.

Make each piece once, give it parameters for size and color, and place it with goto. Next, a new zone: robots made from a mold!