Classic Computer Magazine Archive COMPUTE! ISSUE 29 / OCTOBER 1982 / PAGE 114

Part 2

Turtle PILOT: Including PILOT For Atari

Alan Poole
Loom is, CA

In Part I last month you were introduced to two Applesoft programs that create a powerful new language for the Apple. This language, called Turtle PILOT, combines PILOT, turtle graphics, and all of Applesoft's commands and functions. Turtle PILOT resembles Atari PILOT, allowing you to translate PILOT programs for the Atari to the Apple. The features of Turtle PILOT described last month were just the beginning. This month, along with the PILOT language for Atari, we will deal with the most interesting features of Turtle PILOT: the turtle graphics. Turtle graphics will add new commands to your computer which make high resolution graphics easier. At the end of this article you will find listings of three example programs in Turtle PILOT. These programs can be typed using the Editor and then translated to Applesoft or Atari BASIC with the Translator, as described in Part I.

Introduction To Turtle Graphics

Turtle graphics gives you control over an imaginary and invisible turtle that lives on the screen. This turtle has a set of colored pens. When you instruct the turtle to move, it will leave a trail with its pens. All turtle commands must be preceded by a G: instruction. If there are no commands in the object of a G: instruction, page one of Apple high resolution graphics will be turned on without clearing. (This would result in Graphics 7 on the Atari.) Below is a description of each of the 11 turtle commands. Each description is followed by several examples using the command.

CLEAR

The CLEAR command sets Apple's high resolution graphics page one and clears the screen. (Atari goes to full-screen Graphics 7 and clears the screen.) This is identical to Applesoft's HGR command.

G:CLEAR
GY:CLEAR

TURN

The TURN command is followed by an expression. The value of the expression is the number of degrees added to the angle the turtle is presently headed. A positive number turns the turtle clockwise, and a negative number turns it counter-clockwise.

G:TURN 90
G:TURN A
G(V < 10):TURN N + INT(X*RND(1))

TURNTO

This command will change the angle of the turtle. The turtle's angle will be set equal to the value of the expression following the TURNTO command. Zero degrees is north, 90 degrees is east, 180 degrees is south, and 270 degrees is west.

G:TURNTO 20
GN:TURNTO ABS(K) + 8

DRAW

The DRAW command will move the turtle at the angle it is presently heading, leaving a trail on the screen as it moves. The value of the expression following the DRAW command is the distance the turtle will move. If the turtle hits the edge of the screen, it will stay at the edge.

G:DRAW 25
G:DRAW D-6
GY(L <> 1):DRAW RND(1)*50

GO

The GO command is similar to the DRAW command, except the turtle will not leave a trail.

G:GO 50
GY:GO S1 + ABS(S2)

GOTO

This command moves the turtle to new coordinates on the screen without leaving a trail. The GOTO command is followed by the x and y coordinates separated by a comma. The coordinates the turtle uses are more like a normal graph than Applesoft's high resolution coordinates. The origin is in the middle of the screen instead of the upper left-hand corner. As you move up the graph, the y coordinate increases. As you move to the right, the x coordinate increases. The range of x coordinates is from -139 to 140, and the range of y coordinates is from -111 to 80.

G:GOTO 0, 10
GN:GOTO -50, -5
GY(W>5):GOTO X, Y*N + 2

PEN

The PEN command changes the color of the pen which the turtle uses to draw. It may be follows by the color names BLACK, GREEN, VIOLET, WHITE, BLACK2, RED, BLUE, or WHITE2. If the colors on your monitor are different, the color names can be changed in lines 5410-5445 of the Translator program. The PEN command may also be followed by UP or DOWN. Setting the pen to UP will cause all DRAW commands to move the turtle without leaving a trail. DOWN will return the pen to normal. A final option with the PEN command is to use the color ERASE. This will set the pen to the background color, which is normally black unless changed with a SCREEN command.

G:PEN RED
G:PEN ERASE
G(C = 1):PEN UP

SCREEN

This command will clear the entire screen to the color that follows the SCREEN command. The color names are listed above under the PEN command.

G:SCREEN BLUE
GN:SCREEN WHITE2

FULL

Sets full screen graphics mode with no text at the bottom of the screen.

G:FULL

MIX

Sets the mixed text and graphics mode with four lines of text at the bottom of the screen.

G:MIX

QUIT

The QUIT command turns off the high resolution graphics and returns to the text mode. It is identical to Applesoft's TEXT command.

G:QUIT

Multiple Turtle Commands On A Line

The object of a G: instruction can hold up to six turtle commands. The commands are separated by semicolons. Below are some samples of multiple turtle commands on the same line.

G:DRAW 20; TURN 90
G:CLEAR; SCREEN BLUE; PEN RED; TURNTO 10

Turtle Loops

Suppose you want to draw a square. You could use the following series of commands.

G:DRAW 50
G:TURN 90
G:DRAW 50
G:TURN 90
G:DRAW 50
G:TURN 90
G:DRAW 50
G:TURN 90

This seems like a lot of work to draw a simple square. Wouldn't it be easier if there were a simple way to loop turtle commands? This is one of the features included in Turtle PILOT. You can place up to six turtle commands between parentheses and put an integer in front of them for the number of times to loop them. For example, the following instruction would draw the same square as the eight instructions above.

G:4(DRAW 50; TURN 90)

There are limitations with these loops. A loop cannot be placed inside another loop. Also, a command cannot be outside a loop on the same line. The following instructions would not be legal in Turtle PILOT.

G:4(DRAW 30; 3(TURN 120; DRAW 10))
G:PEN WHITE; 6(DRAW 75; TURN 60)
G:10(DRAW L; TURN 36); QUIT

Using High Resolution Page Two

If you write a long Turtle PILOT program that uses turtle graphics, you may find that there isn't enough memory. An extra 8K in the Apple's memory can be used by drawing on page two of high resolution graphics instead of page one. To do this, use a B : HGR2 instruction instead of G : CLEAR. All other turtle commands will work normally on page two.

PILOT Variables

Last month I mentioned that variables beginning with Q cannot be used in a Turtle PILOT program. This is not exactly true, but you must know how to use them correctly. Q variables are used in the translated program to execute some of the PILOT instructions. Below is a description of each of the variables and some possible uses for them in programs.

QM: This variable holds the number of the item that successfully Matched last. If more than one item Matched, QM will equal the number of the first item. This variable can be very useful, especially with programs containing a question with multiple choice answers. Program 3 with this article illustrates this.

QC: Conditioner flag, 0 = N, 1=Y.

QR: Right margin, normally set to 40.

QI$: User's last response with an Accept instruction. The Match instruction normally uses the last response, but a string can be used instead by setting QI$ equal to the string immediately before the Match instruction. For instance, the following instructions would search for the word "TO" in R$.

C:QI$ = R$
M:TO

QX, QY: Coordinates of the turtle.

QA: Angle of turtle.

QL: Length of line drawn by turtle.

QP: Pen position, 1 = UP, 0 = DOWN.

QB: Number of background color. Changes after a SCREEN command.

Q$(25): List of items to be Matched.

Q(31): List of values for pitches of notes.

QT$: String to be Typed.

Ql, Q2, QT, QI, QK$: Temporary variables.

Next month will be the third and last article on Turtle PILOT. We'll translate an Atari PILOT program to Turtle PILOT, provide some documentation on the Editor and Translator programs, and include PILOT for Commodore machines. If you want more information on turtle graphics and PILOT, read the "Friends Of The Turtle" column in COMPUTE!.

Program 1.

1 * EXAMPLE 1
2 R : DRAWS INTERESTING PATTERNS
3 B : TEXT : HOME
4 T : TYPE AN ANGLE (BETWEEN 70 AND 150 IS BEST).
5 A : A
6 G : CLEAR;GOTO 0,-17; TURNTO 0; PEN WHITE; FULL
7 C : L = 1
8 * DRAW
9 G : DRAW L; TURN A
10 C : L = L + 2
11 J(L<125) : * DRAW
12 E :

Program 2.

1 * EXAMPLE 2
2 R : DRAWS STARS OF RANDOM SIZE
3 G : CLEAR; FULL; PEN WHITE
4 *START
5 C : X = RND(l) * 220 - 139 + 30
6 C : Y = RND(l) * 132 - lll + 30
7 G : GOTO X, Y
8 U : *STAR
9 C : STARS = STARS + 1
10 J (STARS<10) : * START
11 E :
12 *STAR
13 C : SIZE = INT(RND(l)*25 + 5)
14 S : SIZE, 75
15 G : 5(DRAW SIZE; TURN 144)
16 E :

Program 3.

1 * EXAMPLE 3
2 R : EXAMPLE OF USING QM
3 T : WHAT IS THE NAME OF THE LARGEST OCEAN
4 T :          ATLANTIC OCEAN
5 T :          PACIFIC OCEAN
6 T :          INDIAN OCEAN
7 *ANSWER
8 A :
9 M : ATLANTIC,PACIFIC,INDIAN
10 J(QM=1) : *ATLANTIC
11 J(QM=2) : *PACIFIC
12 J(QM=3) : *INDIAN
13 T : PLEASE TYPE ONE OF THE THREE ANSWERS.
14 J : *ANSWER
15 * ATLANTIC
16 T : NO, THE ATLANTIC OCEAN IS THE SECOND LARGEST OCEAN. TRY AGAIN.
17 J : *ANSWER
18 *PACIFIC
19 T : YES, THE PACIFIC OCEAN IS THE LARGEST OCEAN.
20 E :
21 *INDIAN
22 T : NO, THE INDIAN OCEAN IS THE THIRD LARGEST OCEAN. TRY AGAIN.
23 J : *ANSWER