A cookie cutter
One cutter, many cookies. Each cookie can get a different icing.
13TURTLE DRAWING · ART
Put each drawing in a function. Then call them to build a house, a sun and trees.
IN REAL LIFE
Artists and builders reuse pieces all the time.
One cutter, many cookies. Each cookie can get a different icing.
Build a wall piece and a roof piece, then snap them into a house.
First draw the outline, then fill the inside with color.
LOOK FIRST
def square(size, fill) keeps the steps. square(50, "red") draws one.
color("black", "yellow"): black pen, yellow inside. begin_fill() and end_fill() wrap the shape.
penup(), goto(x, y), pendown() moves the turtle to a new spot without a line.
LOOK FIRST
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
Guess first. Then run real Python and check.
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?
PyBot's hint
Ready. Press Run Python.
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
Try each tiny question. A wrong answer becomes something to review.
Which one works like a drawing function?
def triangle():
for i in range(3):
t.forward(80)
t.left(120)What does the turtle draw?
square(30, "red")
square(90, "blue")Which square is bigger?
t.color("black", "yellow")
t.begin_fill()
t.circle(40)
t.end_fill()What does the circle look like?
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?
t.penup()
t.goto(100, 50)
t.pendown()Where does the turtle go?
t.forward(50)
t.goto(-80, 60)Does goto() draw a line?
turtle.bgcolor("navy")What changes?
move(-150, 0)
house()
move(50, 0)
house()How many houses appear?
The square should be painted yellow, but the paint never pours in. Add the missing line.
PyBot moved the turtle to the sky, but no sun appears. Make the sun show up.
Two trees should stand apart, the second one at x = 80. Both land in the same spot. Fix the second call.
Want a fresh start? Clear only this page.
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!