The Turtle’s Pen
PENUP PENDOWN PENDOWN? PEN SETWIDTH WIDTH PENERASE PENREVERSE
In this section, you will learn about commands to control the turtle’s pen.
PENUP
PU
PENUP
lifts the turtle’s pen so that it does not draw when it moves from place to place.
» Things to Try
Can you use PENUP
to draw one square inside the other?
You will also need to use PENDOWN
. See the next command.
PENDOWN
PD
PENDOWN
lowers the turtle’s pen so it can draw.
It’s easy to forget to put the pen back down! If the turtle isn’t drawing, type PD
and see if that solves the problem.
» Things to Try
Now you can write your name, leaving spaces between letters.
Just use PU
and PD
to get from one letter to the next.
PENDOWN?
Do you want to know if the turtle’s pen is down? Just use PENDOWN?
.
PENDOWN?
is a reporter that gives you information.
PENDOWN?
Result: TRUE
PU
PENDOWN?
Result: FALSE
» Things to Know
PENDOWN?
also reports TRUE if you have used PENERASE
or PENREVERSE
(see below).
You can use PENDOWN?
in programs you write. That way, you can find out if the pen is up or down and write instructions depending on the result. You would use IF
or TEST
to check the result. See the section on Program Flow to learn how.
PEN
PEN
is a reporter that tells you the mode the pen is in. The four possibilities are:
PENDOWN
PENUP
PENERASE
PENREVERSE
» Things to Know
Because PEN
is a reporter, you can use it in programs you write. You would use IF
or TEST
to check the result. See the section on Program Flow to learn how.
SETWIDTH number
SETW number
Use SETWIDTH
to change the width of the turtle’s pen.
SETWIDTH
takes a number from 1 to 999 as input.
If you use a larger or smaller number, you will see an error message.
If you are using multiple turtles, all turtles follow that command. However, there are commands to talk to individual turtles, so you can set the width of each turtle’s pen individually. See the section on Using Multiple Turtles to learn more.
» Things to Try
In this hexagon, each side uses a different penwidth, starting from 1 and going to 6.
You can enter individual commands to do this. Or, see the next entry, for WIDTH
to see how to do it more easily!
WIDTH
WIDTH
is a reporter. It tells you the width of your turtle’s pen.
WIDTH
Result: 3
» Things to Try
Here’s a more efficient way to draw the hexagon. The instruction uses both SETWIDTH
and WIDTH
.
REPEAT 6 [FD 20 RT 60 SETWIDTH WIDTH + 1]
Do you see how that works? Every time the turtle moves and turns, it checks uses WIDTH
to find out the penwidth, and then sets the width to be one more than that. You could also write the instruction like this, to make it clearer.
REPEAT 6 [FD 20 RT 60 SETWIDTH (WIDTH + 1)]
Because WIDTH
is a reporter, you can use it in programs you write.
PENERASE
PE
PENERASE
turns the turtle’s pen into an eraser. When the turtle moves,
it appears to erase lines by drawing in the current background color.
Some remnants of the lines may be left when PENERASE
is used. For best results, increase the width of the turtle’s pen using SETWIDTH
.
To stop PENERASE
, use PENREVERSE
, PENDOWN
, PENUP
, or SETPEN
.
» Things to Try
Try drawing some lines and then setting the pen to PENERASE
.
Using REPEAT
, try some different experiments.
This set of instructions, which uses PENERASE
, has a very different outcome from the same commands using PENREVERSE
. (See below.)
DRAW SETWIDTH 20 PENERASE REPEAT 8 [FD 50 WAIT 150 RT 45]
The WAIT
command adds a short pause between the FD
and RT
commands. Read more about WAIT
in the Program Flow section.
PENREVERSE
PX
PENREVERSE
inverts the drawing. That means it erases anything already drawn, and draws if there is no drawing. Some remnants of the lines may be left when this command is used. For best results, increase the width of the turtle’s pen using SETWIDTH
.
PENREVERSE
does not change the colors of the lines.
To stop PENREVERSE
, use PENERASE
, PENDOWN
, PENUP
, or SETPEN
.
» Things to Try
Try this: Draw some lines and then set the pen to PENREVERSE
.
Using REPEAT
, try some different experiments.
For example, this sequence of instructions produces a very interesting result. Did you expect this design?
DRAW SETWIDTH 20 PENREVERSE REPEAT 8 [FD 50 WAIT 150 RT 45]