Terrapin Resources

Terrapin Logo 4 has been discontinued! Our new Terrapin Logo version is much more powerful and modern; we strongly recommend that you update to our latest release of the Logo programming language.

Part Two

Meet the Turtle

You’ve already seen the turtle - quietly sitting in the center of the Graphics window - patiently waiting for you to tell it what to do. Well, the waiting is over! You’re about to enter the fascinating world of turtle graphics - a special way of drawing interesting, exciting and colorful graphics on your computer screen by telling a turtle what to do. Not a real turtle, of course. The Logo turtle is a special graphics cursor that can move forward or back, rotate right or left, change color, change size, change shape and even disappear. There’s even more, but let’s do something now.

Type FORWARD 100 and watch what happens. The turtle moved in the direction that its head was pointing; the line it drew is 100 turtle steps long (that’s how turtles measure distance).

Now, type RIGHT 90 and you’ll see 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.

If you type these two instructions again and again and again, you’ll get pretty tired of typing! Here’s a shortcut: type FD 100 RT 90 all on the same line before you press Enter. FD is short for FORWARD and RT is short for RIGHT. Next, just press the up-arrow key to move the cursor onto that line and then press Enter. What could be easier? You’ll see.

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 a number and a list. The number tells it how many times to repeat and the list tells it what to repeat. So, to repeat the earlier drawing, type REPEAT 4 [FD 100 RT 90]. That’s a lot easier than typing the instructions four times.

Don’t forget about the up-arrow key - it’s still worth using if you make a mistake or if you just want to change a previous line of instructions. Let’s say you want to change the REPEAT we just used so that the turtle does the opposite of what it did last time. The opposite of FD is BK (short for BACK) and the opposite of RT is ` LT ` (short for LEFT). See what happens when you change both instructions to their opposite and then change just one at a time. Is this a pane? It should be. Try this next: REPEAT 3 [PRINT “HA].

If your Graphics window is getting a little messy, just type CS to clean it up and start drawing again. (CS is short for CLEARSCREEN.) You can clean up the Listener window by typing CT (short for CLEARTEXT).

More About the Turtle

There are many Logo commands for the turtle. You can’t learn them all at once but it makes sense to learn them in pairs or groups. Commands that are opposites are easy to learn at the same time. You already know the FORWARD and BACK opposites and the RIGHT and LEFT opposites. Here are some others you can try out:

PENUP or PU - the turtle won’t draw lines now
PENDOWN or PD - the turtle will draw lines again

Where’s the pen? It’s right in the middle of the underside of the turtle. 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. Does your pencil have an eraser? The turtle’s pen does.

PENERASE or PE - draws in the background color

Erasing in the Graphics window is not exactly 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, try FD 100 PE BK 100 but don’t forget to put the pen back down when you’re done! Here’s one more pen trick.

PENREVERSE or PX - inverts the colors under the pen

This special pen flips colors when it passes over them. In computer speak, the color’s bits are inverted; black becomes white, red becomes yellow, and more. Try to create a drawing, set the turtle’s pen width to, say 10, and draw over your drawing to see interesting color effects!

If the turtle is moving too quickly to follow, type SLOWTURTLE to slow it down. Type SETSPEED 1 to restore normal speed.

The Turtle Continued

When you finish a drawing, do you leave your pencil in it?

HIDETURTLE or HT - the turtle disappears
SHOWTURTLE or ST - the turtle reappears

Hiding the turtle makes drawing faster and gets the turtle out of the way. 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.

When the point of your pencil gets dull, the lines get wider. The turtle’s pen never gets dull but you can change its width from 1 to 99. Width 1 is the default. You may not even see the turtle with a really wide pen!

SETWIDTH or SETW - changes the pen width (1-99)

With all the commands you know so far, you can make some interesting special effects. The star illustration looks like a really complicated thing to create. In fact, the star was drawn with SETW 10 and then erased with SETW 4.

See what you can do. Just play around.

Add Some Color

Coloring with the turtle is simple and fun! You can choose from 16 colors for now. Let’s keep it simple.

SETPC - sets the pen color (0-137)

SETPC needs a number from 0 to 137. Not only does the pen color change, but the turtle color changes too, so you can tell which color it will use to draw. The table of color numbers is shown at the right. Try any color you want. Pick your favorite.

What about SETPC 15? Try it. When Logo starts up, the Graphics screen is white. If you draw with the same color as the background, it’s like not drawing at all. What if you want to draw in white?

SETBG - sets the background color (0-137)

SETBG needs a number from 0 to 137 just like SETPC. If you set the background color to the same color as the turtle, the turtle will disappear but you can’t get it back with ST - you have to make sure the background and the turtle colors are not the same.

Do you want a green square? Or, a blue star? Just change the pen color and then use the up-arrow key to move the cursor back to a previous command line and press Enter.

Can you make a red diamond? A diamond is really just a square that’s turned sideways a little.

Teaching Logo Something New

Wouldn’t it be great if you could just type SQUARE instead of REPEAT 4 [FD 100 RT 90]? Go ahead and try it but expect an error message because Logo doesn’t know what SQUARE means. Yet. With your help as the teacher, Logo can learn new commands! Then, you can use them just like the built-in commands to save time, typing and typos. To teach Logo how to square, type TO SQUARE and you’ll see a special window - think of it as your chalkboard where you’ll write today’s lesson. May I have your attention, please? Read carefully.

There are three text boxes in the ”Define a Procedure“ window but we only need two of them for now. The ”Procedure Name“ text box is already filled in and the cursor is in the ”Commands“ text box. This is where you type the instructions that make a square, just like you did in the Listener window. Go ahead and type REPEAT 4 [FD 100 RT 90] and then press Enter. Nothing happens.

Instructions you type in the ”Commands“ text box are remembered for later - not run immediately like they are in the Listener window. Click the Define button and you’ve just taught your first lesson! You deserve a raise!

Now, type SQUARE.

More Teaching Fun

A square is four lines and four turns. But, what is four SQUARE and four turns? Type REPEAT 4 [SQUARE RT 90] and see what a square of squares looks like. Change the numbers for REPEAT and RIGHT to see what other interesting things you get. How about a pinwheel? How many squares are in that?

Of course, all of your squares are 100 turtle steps on a side because that’s how you defined it. How can you change it? If you type TO SQUARE again, you will see the procedure on the chalkboard just as you left it. You could change the FD 100 to FD 50 but then, what if you wanted bigger squares again later? You’d have to change it back to 100 again. This would get old in a hurry. Just remember, you’re a teacher!

Why not teach Logo how to make a smaller square? Call it BOX. Just type TO BOX, press Enter, type REPEAT 4 [FD 50 RT 90], click on the Define button and there you have it! Now you can draw a big square with SQUARE and a small square with BOX.

Try defining a procedure to draw a triangle - it’s just three lines and three turns. What about the star? Once you have your new commands defined, see what you can make. Here are some suggestions.

Your Input Matters

If you want to draw squares of a zillion different sizes, do you need to define a zillion different procedures with a zillion different names? That would be silly. The only difference between SQUARE and BOX is the number after the FD command but how can you replace that number with any number you want, any time you want, without changing it over and over again in the ”Define a Procedure“ window? The answer is simple - use an input to your SQUARE procedure so you can just type SQUARE 100 or SQUARE 50 or use any number as the actual input.

What’s an actual input?

An actual input is the input you give to a command or procedure when you actually run it, like the number 100 in FD 100. An actual input is also called an actual parameter or actual argument.
What other input is there?

A formal input is used in the definition of a procedure. It is a special container with a name chosen by you. A formal input is also called a formal parameter or formal argument. It is also called a variable because its contents can change or vary.

You will find that just the word input is often used for both types of input. Don’t let that confuse you. It’s just easier to say input.

Type TO SQUARE to bring up the chalkboard again. Click in the ”List of Inputs“ text box. Since we are going to draw squares of any size we want, let’s use the name SIZE for our input to SQUARE. Type SIZE and then press Tab to move the cursor into the ”Commands“ text box. Replace the 100 input to FD with SIZE. When you run SQUARE later on, SIZE will be automatically replaced with the actual input you give to SQUARE. Click the Define button to save your new SQUARE.

Now, you can draw squares of any size you want with just the SQUARE command in the same way you draw lines of any length with the FD command. Just give SQUARE an input. An actual input, that is. Add an input to other procedures in the same way. It’s simple, now.

Terrapin Logo allows for ‘Relaxed syntax’, which does not require the colon in front of names. If you turn off this option, you will need to use :SIZE instead of SIZE in your procedure.

Need More Input?

Your procedures can have more than one input, just like the REPEAT command. Suppose you want to draw a stack of boxes. How would you do that? Of course, you would use the SQUARE procedure to draw the box. But then what? How about moving the turtle to the top of the box it just drew? Then, it would be ready to draw the next box. Now how many boxes do you want in your stack? How about a short stack of 3 big boxes? Try REPEAT 3 [SQUARE 40 FD 40].

If you change the 3 to 7, you get a tall stack. If you change the 40’s to 12’s, you get smaller boxes. You can make stacks of boxes with one procedure called STACK and give it two inputs - one for how many boxes and another for how big to make them. Let’s name them HOWMANY and HOWBIG.

Type TO STACK to bring up the chalkboard. Click in the ”List of Inputs“ and type the names for the two inputs. Press Tab to move into the ”Commands“ window and type in the instruction to draw a stack of any number of boxes of any size.

- Where will you put HOWMANY?
- Where will you put HOWBIG?
- How about a star made from stacks?