#### Using Turtle Graphics Under Turbo Pascal by Nathan Hull

A turtle is a graphics character which lives on the screen. The turtle can either be visible or not, depending upon the needs of the program, but he is most useful to us because he can drag a pen, leaving a trail behind him. The turtle can choose to draw with any of four pens, and the four pens can be chosen from among sixteen possible colors. There are some restrictions as to the combinations of colors, but we will see this in a bit. The turtle lives in a world bounded by the dimensions of the computer screen. His ``home" is the center point of the screen, and this is the point . Since the screen has 320 points going across it in the horizontal direction, and 200 points going down the vertical direction, the lower left-hand corner is point , and the upper right-hand corner is point :

The turtle can connect any of these points with any one of his four pens. Each point in the coordinate system represents a dot on the computer's screen, and is the smallest possible dot the computer can draw (in the color graphic mode). Each point is called a "picture element" or "pixel" for short. The turtle starts at the home position pointing north (that is, towards the top of the screen). The turtle is initially invisible, and remains so unless you tell him to "show" himself. There are several steps one must take in order to activate the turtle. The first of these involves a statement which is a directive to the Turbo Pascal compiler to include the graphic routines and the Turtle routines into your program. It reads these routines in from the disk, and places them inside of your program. So, the first statement in your program after the PROGRAM statement should be:

```USES GRAPH3;
```

Before you start issuing Turtle commands, you must put the system into color graphics mode, or it will freeze the computer. That statement is:

```          GraphColorMode;
```

You may also want to choose your colors before issuing Turtle commands. The system does choose some colors for you, but you have the option of changing these "default" colors. First, you can choose a background color out of any of the sixteen possible colors:

```Dark colors   Light Colors

0: Black          8: DarkGray
1: Blue           9: LightBlue
2: Green         10: LightGreen
3: Cyan          11: LightCyan
4: Red           12: LightRed
5: Magenta       13: LightMagenta
6: Brown         14: Yellow
7: LightGray     15: White
```
Notice that the colors are numbered 0-15, and not 1-16. To choose a background color, put the following statement in your program:

```          GraphBackground(color)
```
where color can either be a number from 0 through 15, or the actual name of the color. Thus, the following are equivalent:

```          GraphBackground(9);
GraphBackground(LightBlue);
```
Each of them causes the screen background to be painted a light blue.

The Turtle carries four pens at a time, and, indeed, there can only be four different colors on the screen at a time. The pens are numbered 0 to 3 (Do you notice that computers like to start there numbering at 0 rather than 1?) The background color is always pen 0. You actually might want to draw in the background color if you are drawing over something which has been already drawn in a different color. The other three pens, numbers 1 through 3, must be chosen from one of four predetermined sets of choices, called palettes. The four possible palettes, numbered 0 through 3 are:

```        Pen:      #1            #2             #3

palette:0        Green          Red            Brown

1        Cyan           Magenta        LightGray

2        LightGreen     LightRed       Yellow

3        LightCyan      LightMagenta   White
```
Thus, although the background can be any one of sixteen different colors, the other three pens have to be chosen from one of these predetermined palettes.

To choose a palette, use the following statement:

```          Palette(palettenumber);
```
where palettenumber is a number 0-3.

Turbo, of course, chooses a background and palette for you. The default choices are a Black background, with palette 0.

Before you start drawing with the Turtle, you may also want to choose the pen color. Again, Turbo chooses pen number 1 as the default. Thus, if you accept all of the defaults, you will be drawing with a green pen on a black background. To choose a pen, include the following statement:

```          SetPenColor(pennumber)
```
where pennumber is a number between 0 and 3.

Do NOT confuse pennumber with a color number. You are NOT choosing a new color for a pen, you are only choosing one of the four pens, all of which have already had their colors chosen!

The final command you may want to issue before drawing with the Turtle is the request for him to reveal himself:

```          ShowTurtle
```
The Turtle appears as a triangle on your screen.

The first two commands for the Turtle are rather straightforward:

```          Forwd(distance)
```

where distance is the number of pixels to move forward and

```          TurnRight(angle)
```
where angle is an angle in degrees 0-359.

We are now ready to write our first Turtle Pascal program, although it will be a very simple one to draw a Red box on a Green background:

```Program Box;
USES GRAPH3;
BEGIN
GraphColorMode;     {put screen in color graphics mode
in order to use the Turtle}
GraphBackground(Green);  {choose green background. Note
that we could have used (2)
Palette(0);    {Palette 0 has colors Green-Red-Brown}
SetPenColor(2);  {choose 2nd color of palette, which,
of course, is Red}
ShowTurtle;     {might as well see our hero}

Forwd(20);     {20 pixels long on each side}
TurnRight(90);     {90 degree corners are nice}
Forwd(20);     {notice the box stays within
the screen's boundaries}
TurnRight(90);
Forwd(20);
TurnRight(90)

END.
```

Summary of Turbo Turtle Commands (version 3.01a)

`USES GRAPH3;` Obligatory statement which includes both graphic and turtle commands within a program.

`Back(distance);` Moves the turtle back distance number of pixels from its present location.

`ClearScreen;` Clears the screen and moves the Turtle to the Home position (0,0)

`Forwd(distance);` Moves the turtle forward distance number of pixels from its present location. Note that in version 3.0, this command was called "Forward."

`Heading;` This is an integer function that returns the current course heading of the Turtle in degrees (0-359). An example of its use is: `TurHeading:=Heading;`

`HideTurtle;` This hides the Turtle from view. This is the initial setting for the Turtle.

`Home;` This moves the turtle to the Home position (0,0), and points it North (heading 0).

`NoWrap;` This stop the Turtle from wrapping around the screen. That is, if it hits any of the four boundaries of the screen, it will stop moving. This is the initial setting for the Turtle.

`PenDown;` This puts the currently selected pen down, ready to draw with. Note that this does not cause any lines to appear until the Turtle moves. This is the initial value at the beginning of a program.

`PenUp;` This causes the pen to taken up by the Turtle. Any movement after this will not cause a line to be drawn.

`SetHeading(Angle);` This causes the Turtle to be pointed toward the given Angle (0-359). Angle 0 is towards the North, with the other angles clockwise from North.

`SetPenColor(Pennumber);` This instruction is somewhat of a misnomer since it actually doesn't select a pen color. In actuality, it selects one of four pens, 0-3, which have already had their colors chosen.

`SetPosition(x,y);` This places the Turtle at the coordinates (x,y), leaving it pointing to the same angle or heading as it was before.

`ShowTurtle;` This causes the Turtle to reveal itself on the screen, and remain showing until a HideTurtle instruction.

`TurnLeft(angle);` This causes the Turtle to turn left angle degrees from its current position. Note that this is in terms of the Turtle's view, not the user. A negative value causes a turn to the right.

`TurnRight(angle);` Same as above, except to the right.

`Wrap;` Causes the wrap function to become active. Wrap allows the Turtle to cross the screen boundaries. That is, if it runs off the top, it reappears on the bottom. If it runs off the right side, it reappears on the left. "NoWrap" turns off the wrap feature.

`Xcor;` This is a function that returns the Turtle's present x coordinate. An example of use is: `CurrX:=Xcor;`

`Ycor;` Same as above for the y coordinate.

`GraphColorMode;` Puts the screen in four color graphics mode. This statement must be in every Turtle program before any Turtle command is issued. This also clears the screen.

`TextMode(CO80);` This statement returns the screen to its normal state. It also clears the screen.

`Palette(number);` This chooses a palette of number 0-3. See discussion for the colors of the palettes. The palette assigns colors to pens 1-3. Pen 0 is assigned to the background color.

`GraphBackground(color);` Turns the background to the color specified, and assigns pen 0 to color. Note that color can be a number 0-15, or the name of the color given in the table in the text.

`USES CRT;` Allows for the use of the SOUND procedures;

`Sound(pitch);` Starts the speaker screeching the desired pitch in Hertz, until it is blessedly turned off. See attached sheet for a listing of musical pitches and their associated frequencies. The following will produce concert A: `Sound(440);`

`NoSound;` Turns speaker off.

`Delay(milliseconds);` Causes a delay of milliseconds before the program resumes. Thus, to pause of one-half second, code:` Delay(500);`

Sam Marateck
Sat Jan 25 20:30:35 EST 1997