Chapter 1: Beginning Turtle Graphics
Turtle graphics is a method of drawing on the computer screen by controlling the movements of a turtle. The Logo turtle is a special graphics cursor that can move forward or back, rotate left or right, change color, change size and even disappear. With just a few instructions, you can make interesting, exciting and colorful graphics with the turtle. You can’t learn all the instructions at once and you won’t remember much of what they do if you just read about them. Try the examples and then explore on your own. Fortunately, with many of the Logo commands, you can tell what they’re for by their name. This makes it easy to guess what a command is for and also easy to remember the name of a command for something you want to do.
Turtle Basics I
When Logo starts, the turtle is quietly sitting in the center of the Graphics window, patiently waiting for you to tell it what to do. You do that by typing commands in the Listener window and pressing the Enter or Return key. A command is a word that Logo understands - the word must be in Logo’s vocabulary. Typically, a command is the first word in an instruction line that you enter; sometimes it’s the only thing in an instruction line. Try moving the turtle forward:
FORWARD
That was not a mistake! It was a
learning experience. Many Logo commands require an input - a number or
some other value that helps the command do its job. In this case, the
FORWARD command was expecting a number to
tell it how far to move the turtle. See what happens when you type:
FORWARD 100
The turtle not only moved in the
direction that its head was pointing, but it also drew a line 100 turtle
steps long. A turtle step is roughly the same as a pixel on your screen;
that’s how turtles measure distance. Now, type:
RIGHT 90
You’ll see that the turtle has rotated
like it’s peeking around a corner. Turns are measured in degrees, but
they’re not called turtle degrees because that’s how turns are measured
by just about everything, from boats and planes to spaceships. The
turtle moves and turns very fast. To slow it down, you can type:
SLOWTURTLE
To make the turtle move fast again, type:
SETSPEED 1
The
SLOWTURTLE command does the same thing
as SETSPEED .5. The input to
SETSPEED can be from .1 (slowest) to 1
(fastest). Find a speed you like. “Full speed ahead” is not always the
best way to travel.
You can save yourself some typing by using FD
for
FORWARD and RT
for
RIGHT. In fact, you can put more than one
command on the same line before you press Enter.
FD 100 RT 90
Now, use the up-arrow key to move the
cursor onto the line you just typed and then press Enter. Repeating
commands in this way saves even more typing. Keep going until the turtle
has completed the square. Use the up-arrow key again, but this time,
change the number of degrees in the turn so that the turtle draws a
triangle; then draw a pentagon. After the
FD 100
command, the turtle
has to turn a little bit more for the triangle and a little bit less for
the pentagon.
If you want to clear off the screen and start over, you can type:
CS
That’s short for CLEARSCREEN. If you are tired of using the up-arrow key for repeating, you must be thinking that there’s got to be an easier way. You’re right!
Using REPEAT
There is a very simple way to repeat instructions without typing the same thing over and over again. It’s the REPEAT command, but before you can use it you need to learn a little bit about a special type of Logo data called a list. A list starts with [ and ends with ]. In between the brackets is where you put the instructions you want repeated. Lists are used a lot in Logo but that’s enough to know for now.
The REPEAT command needs two inputs: a number and a list. The number tells it how many times to repeat and the list tells it what to repeat. Here’s a sample REPEAT command you can fill in to make the square, the triangle, and the pentagon:
REPEAT number [instructions]
How many times did you run
FD 100
RT 90
to make a square? If you had trouble with the others, try turns
of 120 and 72 (degrees). Can you figure out the
REPEAT command needed to draw the star? It
uses a turn of 135 degrees. Play around with the
REPEAT command using different numbers of
repeated instructions and different numbers for
FORWARD and
RIGHT.
Turtle Basics II
The turtle can move back as well as
forward; it can also turn left as well as right. The commands are
BACK and LEFT;
the abbreviations are
BK
and LT
.
Type the following REPEAT command to make a square:
REPEAT 4 [FD 50 RT 90]
Now, change the
FD
to BK
and see if you can guess where the new square will be drawn before you
press Enter. (SLOWTURTLE might be
useful here.) Then, change the RT
to LT
to make another square. You
can draw four different squares with different combinations.
You may think the turtle doesn’t know its left from its right sometimes. When the turtle is heading up the screen, it’s easy to know what RIGHT and LEFT will do. Of course, they always do the same thing, it just may not seem like that when the turtle is heading toward the bottom of the screen. Remember, it’s the turtle doing the turning to its right or its left.
You can move the turtle
without drawing lines by telling the turtle to pick up the pen with the
PENUP command (
PU
for short). When you
want to draw lines again, tell the turtle to put the pen back down with
the PENDOWN command (PD
for short). Try
drawing one square inside of another square. Use the following sample as
a guide.
REPEAT 4 [FD 50 RT 90]
PENUP FD number RT 90 FD number LT 90 PENDOWN
If the pen didn’t go up and down, you’d have a hard time making your drawings look nice. Try it with a pencil and some paper and you’ll see what I mean.
You can make the turtle disappear with the
HIDETURTLE command (HT
for short).
Hiding the turtle makes drawing faster and gets the turtle out of the
way.
The SHOWTURTLE command (ST
for
short) makes the turtle appear again. Showing the turtle helps you see
which way it’s heading and where it is. If your turtle gets lost, type
HOME to quickly get the turtle back to the
center of the Graphics window. If you still don’t see it, type ST
.
If the pen is down when you type
HOME, the turtle will draw a line to the
center of the screen. HOME also turns the
turtle so it’s pointing straight up again. Try drawing half of a square
and then send the turtle home. Can you make two triangles at the same
time?
Adding Colors
Coloring with the turtle is simple and fun! There are three different ways to set the colors, but all of them use the same commands: SETPC changes the pen color; SETBG changes the background color. You can work with just 16 color numbers, with 22 color words or with an RGB color list that gives you over 16 million colors! Sometimes the numbers are convenient to work with but it’s hard to remember which color goes with which number. The color words are easy to remember but you have to type a little more.
What happens if the pen and the background are the same color? Watch out for that condition while you’re playing around. It’s like not drawing at all. Try each way of setting the colors. Do some of your drawings again, but this time in color. Remember to use the up-arrow key to avoid retyping the lines.
Simple Color Numbers
There are sixteen color numbers shown in the
chart (0 to 15). This is one of those times where the sequence starts
with 0 instead of 1. Type the following commands to change the pen to
red and the background to yellow:
SETPC 4
SETBG 14
Can you make each side of a square a different color? Can’t decide which colors to use? Type in the following commands.
SETBG 0
SETPC RANDOM 15
This sets the background to black which leaves the color numbers 1 through 15 available for the turtle’s pen. The RANDOM command outputs a number from 1 up to the number you give it for input. In this case, RANDOM outputs a number from 1 to 15 - just what you need for the turtle’s pen. The number is chosen at random which means it could be the same number two or three times in a row; that’s the nature of random numbers. Try this example and then use random color numbers inside other REPEAT commands.
REPEAT 180 [SETPC RANDOM 15 FD 100 BK 100 RT 2]
Color Words
There are 22 color words that Logo
understands. When you use a color word, you have to put a double
quotation mark (“) in front of it. Otherwise, Logo will try to run a
command by that name. You can see the list of color words at any time by
typing:
COLORS
Type the following commands to change the pen to red and the background to yellow:
SETPC "RED
SETBG "YELLOW
This is the same thing you did with the color numbers; it just required a little more typing. However, you can tell what the colors are much easier with words. If you can’t decide on which color to pick, let the PICK command do it for you.
SETBG PICK COLORS
SETPC PICK COLORS
The PICK command outputs a randomly selected item from its input. In this case, the input to PICK is the list of 22 color words that is output by the COLORS command. You may end up with the same colors for both the pen and the background. Just run one of the commands again.
RGB Color List
RGB colors are represented by a list of three numbers, each in the range from 0 to 255. The numbers are for the amounts of red, green and blue that are mixed together to make up the final color. This is called RGB color representation - an acronym for Red-Green-Blue. To set the pen to red and the background to yellow, type:
SETPC [255 0 0]
SETBG [255 255 0]
It’s easy to remember the color lists that make up red, green and blue - just set the other numbers to 0. The color white is represented by the RGB list [255 255 255]; the color black is [0 0 0]. With over 16 million colors to choose from, it’s hard to know which color to use.
The Color Picker
The Color Picker is a special tool for
selecting a color. Click on the Color Picker button in the button bar to
display the Color Picker window. When you click and drag around in the
upper area of the Color Picker, the mouse pointer changes to an eye
dropper shape. As long as you hold down the mouse button, you can drag
the eye dropper to any part of the screen - not just the Logo windows
(except under Mac OS X). The lower area of the Color Picker shows the
color being picked up by the eye dropper. When you have selected the
color you want, just release the mouse button.
To use the selected color, click and drag from the lower area of the Color Picker. As you drag the color around to places that can change color, the mouse pointer changes to a paint bucket with paint pouring out and a frame appears around the area to be colored. Release the mouse button to drop the color.
To see the RGB numbers for a selected color, put the mouse pointer on
the Color Picker and right-click (on a PC) or Control-click (on a Mac).
The first item in the Properties of COLORPICKER window shows the RGB
numbers. The pen color and the background color keep track of their own
RGB numbers, regardless of how you set the color. To see them, use the
PENCOLOR and
BACKGROUND commands. (PC
and BG
for
short.)
The Color Picker is fun to play with and it’s useful for learning about RGB colors. However, you can’t put the Color Picker in a REPEAT command!
Controlling the Turtle’s Pen
The turtle’s pen can become an eraser. The command is
PENERASE (PE
for short). Erasing in
the Graphics window is not the same as erasing pencil lines from paper,
but the effect is the same - what was there is now gone. To see this
happen quickly, type:
FD 100 PE BK 100
As the turtle moves over lines, it actually draws in the background
color, effectively erasing what was there. The turtle’s pen can work in
reverse, too. The command is
PENREVERSE (PX
for short).
PENREVERSE is a combination of
PENDOWN and
PENERASE. If a line is in the way, it is
erased like using PENERASE. If nothing
is in the way, lines are drawn like using
PENDOWN. Try this example:
PX REPEAT 80 [FD 100 RT 135]
This may go pretty fast, but you should see something and then nothing again and again. Try drawing with the different pen modes. You can get some interesting special effects. If you forget what the pen mode is, use the PEN command to find out. It reports (or outputs) the pen mode as PENDOWN, PENUP, PENERASE or PENREVERSE.
As the point of a pencil gets dull, the lines get wider. The turtle’s
pen never gets dull on its own, but you can set its width from 1 to 99.
Width 1 is the default. The command to set the pen width is
SETWIDTH (SETW
for short).
You
may not even see the turtle with a very wide pen! You can make the
turtle bigger or smaller with the
SETTURTLESIZE command (
SETTSIZE
for short). The normal turtle size is 1. Turtle sizes can be as small as
.1 and as big as you want. However, neither tiny nor gigantic turtles
will look like turtles! Be reasonable about the size.
The turtle’s FILL command makes for interesting effects because it depends on the pen mode, the pen color, the color under the turtle and whether or not the area is completely closed (or surrounded by other colors). If an area is not closed, the color “leaks out” and may fill most of screen. The most controlled way of using FILL is to draw a closed shape, like a square, then pick up the pen, move the turtle inside shape’s boundary, put the pen back down, and then run the FILL command.
Controlling the Screen
Have you tried drawing with very large numbers? This is a good time for “full speed ahead,” so make sure the turtle is moving as fast as it can. Type:
CS RT 45 FD 8778
There are three drawing mode commands for the
Graphics window: WRAP,
FENCE and
WINDOW. The default drawing mode is
WRAP. When the turtle moves beyond an edge
of the Graphics window, it reappears at the opposite edge and continues
moving. It’s as if the top of the window is connected to the bottom and
the sides are connected to each other. Most of the time, this is the
best drawing mode to use. The turtle is always somewhere inside the
window, even if you can’t see it. And, when drawing something like a
square, the turtle always ends up where it started, even if the lines
wrap.
The
FENCE mode is like keeping the turtle in a
cage. If you try to move the turtle beyond an edge of the Graphics
window, it stops. The turtle is always inside the window, just like in
WRAP mode. However, when trying to draw
something like a square, the turtle may not end up where it started and
your square may not look like a square at all.
In WINDOW
mode, the turtle can “move” well beyond the edges of the Graphics window
and get “lost in space.” You can always type
home
to get the turtle
back to the center of the Graphics window. When drawing something like a
square, the turtle always ends up where it started. However, you may not
see all of the drawing, any of the drawing, or even the
turtle.
To see the differences between the drawing
modes, try each of the following examples.
WRAP CS REPEAT 180 [FD 500 BK 500 RT 2]
FENCE CS REPEAT 180 [FD 500 BK 500 RT 2]
WINDOW CS REPEAT 180 [FD 500 BK 500 RT 2]
Cleaning up
There are three commands to erase turtle drawings: CLEARSCREEN, CLEAN and DRAW. They each reset the background color to white as well as erasing the screen.
CLEARSCREEN also runs a HOME command to move the turtle back to the center of the Graphics window, whether you wanted it moved or not.
CLEAN does much the same thing as CLEARSCREEN, except for the HOME command. It leaves the turtle where it is. DRAW does much more than CLEARSCREEN. It resets the turtle to the state it was in when you started Logo - the pen is put down, the pen color is set to black, the pen width is set to 1, the turtle size is set to 1, and the drawing mode is set to WRAP. Make sure this is what you want before using DRAW.
The Listener window can be cleaned up, too. It holds about 1000 lines of
instructions that save you a lot of retyping. To clean up the Listener,
use the CLEARTEXT command (CT
for
short).
What Now?
Tired of typing REPEAT 4 [FORWARD 100 RIGHT 90]
every time you want a
square of 100 turtle steps? The next chapter will show you how to add
new words to Logo’s vocabulary so you can type SQUARE
instead.