... "the turtle." You can think of this as a drawing instrument... Imagine that you are looking at a computer screen. On it you see a small turtle, which moves when you type commands in a language called "turtle talk," leaving a line as it goes. The command "Forward 50" causes the turtle to move straight ahead a certain distance. "Forward 100" will make it move in the same direction twice as far. You soon get the idea that the numbers represent the distance it moves; they can be thought of as turtle steps. Now if you want to make it go in a different direction, you give it a command like "Right 90." It stays in the same place but turns on itself, facing east if it had previously been facing north. With this knowledge you should easily be able to make it draw a square. If that's easy for you, you can think about how to draw a circle, and if that's easy you can try a spiral. Somewhere you will meet your level of difficulty, and when you do I'll give you this piece of advice: Put yourself in the place of the turtle. Imagine yourself moving in a square or a circle or a spiral or whatever it may be.
And the original turtle:
From "The Children's Machine," Seymour Papert, HarperCollins, 1993.
|
| Figure 2.1 |
If you look closely, you'll notice that the origin for angle measurement is different. In TurtleGraphics, 0 degrees is aligned with the positive Y axis instead of the positive X axis. The other difference is that angle measurement in positive amounts measure clockwise rotation, the opposite direction that you've learned in math classes.
| Command | Inputs | Description | Example |
|
FD FORWARD |
number | Moves the turtle forward, in the direction it is facing, by the specified number of turtle steps. | FD 100 |
|
BK BACK |
number | Moves the turtle backward, i.e., exactly opposite to the direction that it's facing, by the specified number of turtle steps. | BACK 150 |
|
LT LEFT |
number | Turns the turtle counterclockwise by the specified angle measured by a number of degrees (1/360 of a circle). | LEFT 180 |
|
RT RIGHT |
number | Turns the turtle clockwise by the specified angle, measured in degrees (1/360 of a circle). | RT 90 |
| Table 2.1 | |||
Notice that these TurtleTalk commands must include a number, an input. As an example, when you type in a forward command, you must specify some number of turtle steps. This makes sense; if you didn't specify a distance, how would the turtle know when to stop? If you forget to provide the number, you will be reminded, e.g.,
forward
will result in the error message: "forward must be followed by a
number."
I've written a Java applet which understands TurtleTalk. I've named it TG, an acronym for TurtleGraphics. It's an interpreter. Here it is.
The turtle (the hexagon with head, legs and a tail) is in the middle of the graphics area. It is heading north. It has its black pen down, ready to draw a line as it moves. Click the left mouse button in the box below the turtle. This is the TG terminal - an interaction area. A cursor (small, thick, black vertical bar) to the right of a "? " prompt should start to blink indicating it is ready for you to type.
| TurtleGraphics Applet |
To get started, try typing the following instructions which will draw a square:
forward 100
right 90
forward 100
right 90
fd 100 rt 90 fd 100 rt 90
OK. Here are a few more TurtleTalk commands.
| Command | Inputs | Description | Example | ||||||||||||||||||||||||||||||||||||||||
| HOME | Moves the turtle to the center of the graphics area, i.e., coordinates 0,0. | ||||||||||||||||||||||||||||||||||||||||||
|
PENUP PU |
Lifts the turtle's pen up so that it leaves no trace when it moves. | ||||||||||||||||||||||||||||||||||||||||||
|
PENDOWN PD |
Puts the turtle's pen down so that it leaves a trace when it moves. | ||||||||||||||||||||||||||||||||||||||||||
|
CG CLEAN |
Erases (cleans) everything that the turtle has drawn on the graphics area (ClearGraphics). The turtle's state (position, heading, pen color, etc.) is not changed. | ||||||||||||||||||||||||||||||||||||||||||
|
SETC SETPC SETPENCOLOR |
number |
Sets the turtle's color - the color of
its pen. Color is expressed as a number.
|
SETC 10 | ||||||||||||||||||||||||||||||||||||||||
|
HT HIDETURTLE |
Hides the turtle, makes it invisible. | ||||||||||||||||||||||||||||||||||||||||||
|
ST SHOWTURTLE |
Makes the turtle visible. | ||||||||||||||||||||||||||||||||||||||||||
| Table 2.2 | |||||||||||||||||||||||||||||||||||||||||||
Experiment with all of the commands. Draw
Have fun. Make each of them a different color with the "setpencolor" command. You will need to make them different sizes so that you can fit them into the graphics area.
Finally, make sure you have tried out all of the commands in Table 2.1.
Writing software, computer programs, is a lot like writing down the steps to do something.You've learned some of the the TurtleTalk language. If you got the turtle to draw a triangle, a square, etc..., for you, you got experience instructing it to do things in a specific order. And, the order is very important. In programming we refer to this order of steps as a sequence. In our programs, we moved the turtle and then turned it, moved it, then turned it, moved it then turned it,... If you put all of the movement commands first followed by all the turn commands, you would not get polygons - you would get a straight line. So, writing a program is all about putting together instructions in a specific order.
Since by now the TG applet has scrolled off your screen and you probably want to look at the figures while you are trying to get the turtle to draw them... Here is a link to a pop-up web page containing a tiny version the TG applet. Resize the window so it appears in so that it's just big enough to hold the applet. It you do this, it should only take up a quarter of the screen leaving lots of room for this web page.
But, as our TurtleTalk programs get bigger and more complicated, it would be nice if we had a TurtleGraphics program that could read a file containing TurtleTalk commands and execute them as it reads the file. It would even be nicer if you could change the TurtleTalk instructions and/or add more source code to your program, and then save what you've done to a file.
Java applications can provide this functionality.
I've written a Java application I've named TG (the acronym for TurtleGraphics). It does everything that the applet you've been using does and more. But, for you to run it, you need to have a copy of it and a Java Virtual Machine (JVM) on your computer.
Hopefully you or your instructor has installed them on your system. Appendix H covers installation of all of the software used in this series of lessons.
If you have a JVM and the TG application on your system, you should be able to start it up with a "java" command:
java -jar TG.jarThere is a "Help->Commands" menu item to get a full list of what you can tell the turtle to do. Read the Help->Commands stuff and play with all of the new stuff that's documented.
If you don't have Java on your computer or you would just prefer to continue using the TG applet, Here is a link to a pop-up web page containing only the applet and its documentation.