Professional Documents
Culture Documents
Turtle Fun LOGO For The Spectrum 48K
Turtle Fun LOGO For The Spectrum 48K
Turtle Fun LOGO For The Spectrum 48K
Serafim Gascoigne
M
MACMILLAN
© Serafim Gascoigne 1985
Gascoigne, Serafim
Turtle Fun : LOGO for the Spectrum 48K
1. Sinclair ZX Spectrum (Computer-Programming)
2. LOGO (Computer program language)
001 64' 24 QA 76.8 S625
Preface Vll
Acknowledgement Vlll
Index 92
v
Preface
vii
Acknowledgement
viii
PART 1
TURTLE FUN
1
Section 1
Introducing LOGO
What is LOGO?
A personal language
----------~-~~~~----
Making friends
An ideas book
Types of LOGO
Although this book is written especially for the Sinclair Spectrum, the
ideas are the same for any computer that uses LOGO. LOGO is a fairly
standard language and the version used here can be used with Tandy,
Apple, Commodore, Terrapin, Atari, Amstrad, IBM and BBC com-
puters. If your machine has a different kind of LOGO, it does not
necessarily mean that you cannot use this book. The syntax (use of
words) may need modifying, the ideas do not.
Section 2
Before you begin to explore the ideas in this book, you will need to
know how to use some basic commands. If you already know how to
move the turtle, you may skip this section.
Having loaded your program as described in the User Manual, you
are ready to begin. To call up the turtle, you type Showturtle (ST). You
will now see a turtle or cursor-shape at the centre of the screen. This is
the LOGO screen turtle.
To move the turtle you use the following commands
FORWARD RIGHT
BACK LEFT
FD 25 BK 25 LT 90
FD 10 RT 90 FD 10
Walkabout
This section is about screen doodling. How you do it, and what you do,
is up to you. But you might like to play around with the following
ideas.
SETBG REPEAT
SErpC RANDOM
PU RT HT LOGO words
PD LT ST
FD BK WAIT
Take some of the LOGO words in the box and explore. You will have
to do a bit of research for yourself. For example, you will need to look
up the LOGO words in the user manual.
Remember SETBG and SETPC need inputs such as I for blue, 2 for
red .. .
What about RANDOM? This word chooses your inputs at random.
It is like throwing dice - any number can come up, well almost any
number.
Try
9
10 Turtle Fun: LOGO for the Spectrum 48K
How many different ways can you use RANDOM? Can you for
example use RANDOM to change the colours on the screen?
Have you ever held a mirror up to another mirror? If you haven't, have
a go! If you have, then the next program may remind you of what
happens.
SETBG 1 SETPC 7
REPEA T 5 [L T 45 PU FD 30 PD REPEAT 5 [RT
72 REPEAT 5 [FD 20 LT 72]]]
Section 4
Writing a procedure
SETBG 1 SETPC 7
REPEA T 5 [LT 45 PU FD 30 PO REPEAT 5 [RT 72
REPEAT 5 [FD 20 L T 72]]]
END
Now press ENTER and wait. After a few seconds the message
CRYSTALS defined
11
12 Turtle Fun : LOGO for the Spectrum 48K
should appear on the screen. Now for the big moment. Type
CRYSTALS and sit back. Provided that you have typed the commands
correctly, you should have the pattern shown below.
You have taught the computer a new word called CR YST ALS. Let's
teach it some more.
D
TO GREGORY
REPEA T 4 [FD 50 RT 90]
END
TO LOUISE
REPEAT 3 [FD 60 LT 120]
END
L
The procedures CRYSTALS, GREGORY and LOUISE are now
words that the computer understands in the same way that it
understands FD, BK, LT and RT etc.
You can now use these new words to create other procedures and so
on.
TO PATTERN
GREGORY
LT 45
GREGORY
LT 45
LOUISE
Writing a procedure 13
Note You can use capital letters such as TO CR YSTALS or you can use
small letters such as - to crystals. It's up to you which you use. You
can 't, however, mix small and capital letters.
More doodles
Your doodling can produce some spectacular results if you use what is
called recursion. Recursion is the ability of a procedure to call itself.
Let's take a few commands
L T 46 FD 50 RT 11 0 FD 20 LT 46 FD 10
(press ENTER)
REPEA T 10 [L T 46 FD 50 RT 110 FD 20 LT 46
FD 10]
The result on the screen is not very interesting. Let's omit the REPEAT
10 and use recursion instead. For this we will need to write a procedure.
TO DOODLE
L T 46 FD 50 R T 110 FD 20 LT 46 FD 10
Now tell the procedure to call itself by putting the name of the
procedure DOODLE in the next line. The final procedure looks like
this
TO DOODLE
L T 46 FD 50 RT 110 FD 20 LT 46 FD 10
DOODLE
END
14 Turtle Fun: LOGO for the Spectrum 48K
TO STARBURST TO STAIRS
SETBG 6 SETPC 2 FD 20 RT 90
FD 100 FD 20 LT 90
BK 120 STAIRS
RT 21 END
LT65
STARBURST
END
TO TWIRLS
FD 7
REPEA T 8 [FD 4 RT 45]
TWIRLS
END
Play around with the use of recursion yourself. Type any commands
you can think of and see what happens.
Using subprocedures
TO TWIRLS}
TWIRLS
LT 80
TWIRLS}
END
To run TWIRLS} you will have to delete the recursive line in the
subprocedure TWIRLS, otherwise you won't be able to get out of the
subprocedure. You can fill your procedures with subprocedures like
building blocks.
What happens if you put all the procedures together? Nothing!
Unless you remember to delete the recursive line in each individual
procedure, you will find that the turtle is trapped inside the first
subprocedure!
TOTHE.LOT
STARBURST
STAIRS
TWIRLS
THE. LOT
END
This new procedure will not work until you have removed all the
recursive lines in each individual subprocedure.
Section 5
Objects OUTPUT
Primitives Variables
Brackets and Parentheses
Objects
The input for COLOURS could be either a word or a list. You could
type, for example, COLOURS "RED or COLOURS [RED BLUE
GREEN] . Note that a single word is preceded by quotation marks (").
Numbers could also be preceded by quotation marks, but in practice
we do not use them in this case. For TEAM.A we can type TEAM.A
[Anatoly Jason Beryl Floella].
If you are using words (as opposed to lists), the words themselves can
be made from letters or figures , or a combination of both. You can also
use symbols
TO ALIEN. LANGUAGE :X
PR:X
END
16
Some turtle terms 17
Instead of using letters, you could use symbols to make your word or
list of words.
Try typing ALIEN. LANGUAGE followed by the word £*/-.
Objects as lists
TO GARDEN :VEG
PR:VEG
END
TO ALIEN. LANGUAGE :X
PR:X
END
TO NEWS :ITEM
PR [TODA Y'S NEWS IS ]
PR :ITEM
END
Objects as words
Primitives
Parentheses ( ) allow you to use more than two inputs with certain
LOGO primitives
Variables
x + 5 = 20
that is, something plus five equals twenty. The 'something' represented
by x is called a variable.
Again we read in the newspaper "Mr X leaves a million pounds to
local cats' home". Who is Mr X?
At the supermarket you are asked by a smiling TV celebrity which
Some turtle terms 19
TO SQUARE :SIZE
MAKE "SIZE 60
But why use :SIZE and "SIZE? The (") before SIZE represents the
name of this variable, while the (:) before SIZE gives you the value of
the variable.
When you use MAKE with ("), you are creating a LOGO word.
MAKE "SIZE 60
This tells LOGO to create a word called "SIZE which has the value 60.
Now, whenever you type PRINT :SIZE, you get the answer 60, which is
the value of "SIZE.
PRINT :SIZE therefore tells LOGO to print the value of "SIZE.
TO PATTERN
MAKE "SIZE 60
FD :SIZE RT 73
PATTERN
END
TO PATTERN :SIZE
FD :SIZE RT 73
PA TTERN :SIZE
END
20 Turtle Fun: LOGO for the Spectrum 48K
This time the input is not fixed (for example, 60 steps), but can be given
any value. By typing PATTERN 60, the same pattern is produced. But
if you type PATTERN 70, :SIZE now has the value 70.
Remember
When you create a variable using TO, this variable only belongs to the
procedure in which it appears. Thus in
TO TRI :SIDE
Some turtle terms 21
TO TRIANGLE :SIDE
the :SIDE of TRIANGLE is not the same as the :SIDE of TRI. All
variables are separate and totally exclusive to their own procedures.
You cannot transfer, swap or exchange them. They are local. The
:SIDE of TRI is local - it is only interested in its own procedure. The
:SIDE of TRIANGLE behaves in exactly the same way. They may look
the same on paper, but in practice they are not.
However when you use MAKE to create a variable such as
MAKE "FRED 22
FARMYARD "COCKEREL
TO WORLD
MAKE "ANIMAL "EAGLE
PR :ANIMAL
END
You can use any word you like to make a variable. You cannot,
however, use a LOGO primitive, which is a word that is part of
LOGO's basic vocabulary.
Here are a few procedures that either use one or two variables.
TO ASTRA :SIZE
REPEAT 5 [FD :SIZE L T 144]
END
TO SPANGLE :ANGLE
FD 30
RT:ANGLE
SPANGLE :ANGLE
END
TO MILKY. WAY
MAKE "ANS RANDOM 150
REPEAT 5 [FD :ANS RT 144]
PU MOVE PD
MILKY. WAY
END
What does this program do? Nothing, until you have defined the
subprocedure MOVE.
TO MOVE
FD RANDOM 100 RT RANDOM 360
END
?•
FD 50 + RANDOM 15
FD RANDOM 100
But this time we are asking the turtle to limit its choice to between 50
and 65.
24 Turtle Fun : LOGO for the Spectrum 48K
RT 90 + RANDOM 20
This time we are asking the turtle to choose a number between 90 and
110. Instead of choosing a random number of steps, the turtle is
choosing a random number of degrees . We can go on to redefine
MOVE as MOVEl.
TO MOVEI
REPEAT 20 [FD 50 + RANDOM 15 RT 45 +
RANDOM 90]
END
In MOVEI the turtle goes forward between 50 and 65 steps and turns
RT between 45 and 135. Try this new subprocedure MOVEI with
MILKY. WAY and compare the result with the use of MOVE as a
subprocedure.
OUTPUT
TO MESSAGE
OP [I LOVE LOGO]
END
TO PAGE
REPEAT 4 [PRINT MESSAGE]
END
TO NAMES
MAKE "ANS RANDOM 3
Some turtle terms 25
Don't worry about the use of IF for the moment. It will be explained at
the end of this section.
Again
TO PAGEl
PR SE [MY NAME IS] NAMES
PR SE [MY NAME IS] NAMES
PR SE [MY NAME IS] NAMES
END
MY NAME IS CELIA
or
MY NAME IS HAROLD
To program the keys you will need two new LOGO words. These are
IF and READCHAR. IF is used with conditional statements. That
means that you can use IF followed by certain conditions. In the
language of logic we can say: if something is true then do so and so,
otherwise do something different. Let's look at the main procedure to
see how IF works.
26 Turtle Fun: LOGO for the Spectrum 48K
TO PLAY
MAKE "REPLY READCHAR
IF :REPL Y = "F [FD 10]
IF :REPL Y = "B [BK 10]
IF :REPL Y = "R [RT 45]
IF :REPL Y = "L [LT 45]
PLAY
END
You can of course add other commands to this program, which can
include your own procedures. For the moment however let us look at
what the procedure PLAY does.
Outer space
You can use your turtle like a space shuttle to explore outer space. You
can launch it off the screen into an invisible world where it will
continue to carry out your commands. How does it do this? The turtle's
range is much greater than the size of your TV screen. The turtle can
move up to 32767 steps in any direction from the centre of your screen.
Normally if you give the turtle a large forward or backward input, it
will disappear off the screen to reappear on the opposite side. This is
called 'wrap around'. It's rather like tying a parcel with a very large
piece of string. The turtle's trail, like a piece of string, is wrapped round
and round the screen. But unlike a piece of string for tying a parcel, the
turtle and its trail can also disappear, like the famous Indian rope trick,
into thin air!
The turtle does not really disappear but continues to draw out of
sight. What you see on your screen in fact is only a small portion of the
turtle's world.
To allow the turtle to escape from your screen you need the LOGO
27
28 Turtle Fun: LOGO for the Spectrum 48K
You have opened the window and allowed the turtle to crawl freely
into outer space. To prevent the turtle from disappearing all you have
to do is close the window by typing WRAP or FENCE. (I will leave you
to find out what is the difference between WRAP and FENCE. See if
you can find out without looking up the answer in the User Manual.)
The launching ofthe turtle into outer space can also be controlled by
other LOGO words called operators
TO FIND
OP TOWARDS [00]
END
PRINT FIND
and the answer on the screen will tell us in which direction to head the
turtle towards home . You can, of course, head the turtle towards any
position in the turtle's world .
We can play with this idea by writing another procedure FIND 1
which uses FIND as a subprocedure and SETHEADING (SETH).
TO FINDI
REPEAT 4 [FD 20 RT RANDOM 360]
SETH FIND
FD 100
END
This procedure sends the turtle randomly around the screen, then sets
the turtle's heading towards the centre of the screen by using the
subprocedure FIND. The turtle is then sent FD 100. In FIND, OP
stores the answer to the operator TOWARDS until it is required .
We can extend this idea further by a more advanced procedure
which uses a conditional statement.
TO FIND2
IF FIND < 180 [MOVE] [HT]
END
If the direction indicated by TOWARDS is less than 180 run the
procedure MOVE, otherwise HIDETURTLE. An alternative to HT
could be another procedure or command.
The operator POSITION tells you where the turtle is at any time on
or off the screen. The answer, or output, is given as two numbers . If you
type
PRINT POS
when the turtle is in the home position the answer will be 0 O. Move the
turtle about on the screen and use PRINT POS to find out where it is.
Using these operators together with the command WINDOW, you
can now launch your turtle into outer space. Here is a game that you
can play with your friends .
30 Turtle Fun: LOGO for the Spectrum 48K
Turtle launch
Send your turtle into outer space and then bring it gently back to earth.
FD 3000
PRINT TOWARDS [0 0]
This asks the turtle what heading it requires to aim back for home. The
required heading appears on the screen. It could be
263.04738
SETHEADING 263.04738
and REPEAT many times [FD 2]. Press ENTER and you will see a
perfect re-entry, and if you get it right a perfect landing as well. You can
also play this game by writing a procedure.
TO LAUNCH
FD 5000
RT 180
REPEA T 250 [FD 20]
END
Outer space 31
TO LOOK
RT 80 - RANDOM 180 FD 5
SETH TOWARDS [0 0]
LOOK
END
o
SOUND effects
for example
Team members
TO NUMS:Z
TS
IF MEMBERP 3 :Z [PR [TRUE]] [PR [FALSE]]
END
Now enter
NUMS [357]
The answer will be TRUE since 3 does belong to the list :Z which
consists of the numbers [3 5 7].
But if you enter
NUMS [456]
the answer will be FALSE. The number 3 does not in this example
belong to the list [4 5 6].
A simple database
Before you can ask this question you must have information about
Team 1 in the database.
Here is an idea you might be able to use for your own programs.
TO MACHINE :Z :COUNT
TS
IF MEMBERP :COUNT:Z [PR [EUREKA!]] PR
:COUNT]
MACHINE :Z :COUNT + 1
END
MACHINE [357] 10
Section 7
Pretty Poly
You may have enjoyed drawing polygons at school using a ruler and
pencil. Even if you did not enjoy drawing them, using the turtle to draw
polygons can be an exciting experience. The power of the turtle allows
you to produce some spectacular effects at the press of a button.
Figures that would require hours of precision drawing with a pencil
can be produced on the screen instantly.
Here is a simple procedure to get you started. You can draw
polygons of all shapes and sizes. The procedure, which I have called
POL Y, is as follows
By typing POL Y, followed by an input for the sides and an input for the
angle, you can produce some amazing figures on the screen. The last
line in the procedure is recursive. It tells the turtle to start again.
34
Pretty poly 35
Try
POLY 1090
POLY 60144
POLY 30108
POLY 50 160
You might like to experiment with the ANGLE input. Can you make
any predictions as to what kind of shape the turtle will produce? How
many sides will it have? How many points or vertices? Will the lines
cross each other?
You can also use subprocedures inside the POLY procedure. Instead of
FD, for example, use the name of a procedure such as TRIANGLE or
SQUARE.
To demonstrate this, let's write a new version of POLY and call it
POLYI
If you have not already got a procedure called TRIANGLE, here is one
for you to experiment with.
TO TRIANGLE :SIZE
REPEAT 3 [FD :SIZE RT 120]
END
You can of course invent your own program to draw a triangle. Try
POLYI 60 144
36 Turtle Fun: LOGO for the Spectrum 48K
Polyspirals
From the simple POL Y procedure you can go on to produce what are
called polyspirals. For this let's create a new procedure and call it
POLYSPI.
This new procedure is really the POLY procedure again . However the
last line
Here you can see that the increment is not written as a variable but is
included in the variable :SIZE. In the recursion line I have written
:SIZE + :SIZE/IO
This simply means, add the value of :SIZE to :SIZE divided by 10.
You can carry out any arithmetical operations you like on inputs.
For example, you can use +, -, * and I. (* is used for multiply and 1 is
used for divide.) Have a go at using different operators inside your
procedures. Here are just a few examples.
:SIZE * 4 (multiplication)
FD :SIZEI2 (division)
FD :SIZE + 10 (addition)
You can even use operators with the inputs to your procedures. You
can type, for example
POLYSPI23360/7
where you are asking the turtle to divide the angle of 360 degrees by 7.
Here is another program that uses BK as well as FD and uses + and 1
as operators.
I have used some cosmetics here! You can set your own background
and pencolour. As you can see, the size inputs are divided by 2, except
in the last line.
38 Turtle Fun: LOGO for the Spectrum 48K
POLYSPI3 20 162 5
20 160 1
20 119 3
3 110 3
721
3 75
3 360/7 2
You will have noticed that POLY and POLYSPI do not stop. This is
because we have used recursion. To stop a procedure that is recursive
you need a conditional statement.
This tells the turtle to stop if the size of the figure is greater than 100.
This conditional statement using IF can be used with any procedure in
which you are using recursion.
Using RANDOM
The inspiral
What does :DEC stand for? What does this procedure do? Could you
think up your own name for :DEC?
More spirals
Instead of the POLYSPI procedure you can also draw spirals by using
variables with REPEAT and a MAKE statement. Here are two
examples for you to try
MAKE "SPIRAL 1
REPEAT 250 [FD :SPIRAL RT 71 MAKE "SPIRAL
:SPIRAL + 3]
In the first example we use the variable called SPIRAL for the number
of steps that the turtle moves forward . This is followed by another
MAKE statement that tells the turtle to increase its steps by 3 each
time. All the commands inside the brackets are repeated 250 times.
Pretty poly 41
MAKE "SPIRO 0
REPEA T 100 [FD 10 RT :SPIRO MAKE "SPIRO
:SPIRO + 10]
This time it is the angle that changes. The variable SPIRO for the angle
is also followed by another MAKE statement that tells the turtle to
increase the number of degrees by 10. These commands are repeated
100 times. What kind of shape do these commands produce?
TO RED.BUCK
MAKE "SIZE :SIZE +1
FD :SIZE L T 45
BLUE.BUCK
END
TO BLUE.BUCK
FD :SIZE RT 90
IF :SIZE > 100 [STOP]
RED .BUCK
END
Section 8
Spirolaterals
Turtle trip
- -- - "'-
"\
In this section we will explore a new order of patterns called \
spirolaterals. Consider this line of commands
\
FD 10 RT 90 FD 20 RT 90 FD 30 RT 90
Nothing in itself, but let's put this line inside a procedure and use
recursion as well .
TO TRIP
FD 10 RT 90 FD 20 RT 90 FD 30 RT 90
TRIP
END
TO TRIPI :ANGLE
FD 10 RT :ANGLE FD 20 RT :ANGLE FD 30 RT
:ANGLE
TRIPI :ANGLE
Try some for yourself. Try some spirolaterals with more than 3 steps.
How can we get the turtle to stop when it has completed or closed the
shape on the screen? Does the turtle in fact always produce a closed
shape? Explore for yourself and see if the turtle draws a closed shape
every time.
44 Turtle Fun: LOGO for the Spectrum 48K
TO TRIP2 :ANGLE
FD 10 RT :ANGLE FD 20 RT :ANGLE FD 30 RT
:ANGLE
IF HEADING = 0 [STOP]
TRIP2 :ANGLE
END
In the next procedure we have not only varied the angle but the length
as well. We have also used a maximum of 4 steps.
TO TRIP3 :LENGTH :ANGLE
FD :LENGTH RT :ANGLE FD :LENGTH * 2 RT
:ANGLE FD :LENGTH * 3 RT :ANGLE FD
:LENGTH * 4 RT :ANGLE
IF HEADING = 0 [STOP]
TRIP3 :LENGTH :ANGLE
END
The procedures TRIP, TRIPI, and TRIP2 are called order-3 spiro-
laterals, since there is a sequence or pattern of 3 steps each time. TRIP3
is an order-4 spiro lateral. Try TRIP3 5 116 and 20 168
Turtle travel
Not only can you vary the length and the angle but you can also choose
the numbers of the steps, the order of the spirolateral, by using an
additional variable. Let's call this variable MAX. You no longer need
to type in all the steps, you can just include them in the variable MAX.
[3 5 7] 1 10 100 3
[1 2] 1 10 135 11
[2] 1 10 120 11
[3 4 7] 1 5 120 11
[3 4 6 7] 1 5 120 11
[4 8] 1 10 90 8
46 Turtle Fun: LOGO for the Spectrum 48K
Section 9
Looking at circles
How many ways can you draw a circle? You can use a pencil and a
compass. You can use a dinner plate, a coin or a dustbin lid. You can
use a piece of string that is tied to a stick in the ground. This method of
drawing circles is used to mark out sports pitches etc. If you are
familiar with coordinate geometry, then you will know that you can
draw a circle by plotting points on a grid and then join them together to
produce a circular graph. Another way to draw a circle is to repeat a
number of defined movements or use recursion.
This method of drawing a circle is unique to the turtle, or is it? You
can trace the outline of a circle by walking it yourself. But what do you
do? Can you talk yourself through the procedure for a circle. Can you
describe in simple turtle commands what you are doing?
Turtle geometry
Drawing circles with the turtle is part of a new geometry called turtle or
differential geometry. I wonder what the famous mathematician Isaac
Newton would have done, had he had a LOGO turtle? Perhaps you
could find out about Newton and think of ways he might have used
LOGO.
Drawing a circle by recursion.
TO CIRCLE
FD 5 RT 10
CIRCLE
END
47
48 Turtle Fun: LOGO for the Spectrum 48K
The immediate problem with this procedure for drawing a circle is that
the turtle does not stop! To stop the procedure you need an extra line.
IF HEADING = 0 [STOP]
How many units does it take to make a full circle, that is before the
turtle stops? You can count the number of units by putting in aWAIT
command.
TO CIRCLEl
WAIT 20
FD 5...
Using variables
Find your favourite circle and repeat the number of units that the turtle
used to draw your circle. For example
TO CIRCLE3
REPEAT (units) [FD ? LT?]
END
My favourite circle is
TO CIRCLE4 :SIDE
REPEA T 36 [FD :SIDE RT 10]
END
Looking at circles 49
By drawing your own circles you may have discovered that the turtle
turns 360 degrees. Half a circle must therefore be 180, and half again
must be 90.
To draw a right arc you can use
TOARCR
REPEAT 18 [FD 5 RT 10]
END
TO ARCL
REPEA T 9 [FD 10 LT 10]
END
Using pi (17")
To vary the radius ofthe circle you need 17". The next procedure uses the
formula 217"/360 = 0.0174. The circumference of the circle is 360
degrees and therefore the radius (R) must be 360 divided by 217". To
draw a circle, the turtle must move forward by 217"R/360 for each step,
and turn by 1 degree.
TO CIRCLE5 :RADIUS
REPEAT 360 [FD :RADIUS * 0.0174 RT 1]
END
Circular patterns
TO KREIS :SIDE
REPEAT 10 [FD :SIDE RT 36]
END
TO KREISI :SIDE
FD :SIDE LT 72 FD :SIDE LT 72
KREIS :SIDEI2 RT 72
BK :SIDE RT 108
IF HEADING = 0 [ST0P]
KREISI :SIDE
END
50 Turtle Fun: LOGO for the Spectrum 48K
TO PEACOCK :SIDE
FO :SIDE LT 72 FD :SIDE
LT 72 KREIS :SIDE/2 KREIS :SIDE RT 72
BK :SIDE RT 108
IF HEADING = 0 [STOP]
PEACOCK :SIDE
END
TO RINGS :SIDE
CIRCLE4 :SIDE RT 36
IF HEADING = 0 [STOP]
RINGS :SIDE + :SIDE/4
END
Looking at circles 51
A squashed circle
Can you draw this shape accurately with a pencil? You cannot use a
compass or a dustbin lid. Drawing a squashed circle or an ellipse, as it
is officially called, is easy with the turtle.
TO ELLIPSE
REPEA T 2 [REPEAT 45 [FD 1 RT 2] REPEAT 45 [FD
2 RT 2]]
END
TO EGG
REPEAT 2 [REPEAT 45 [FD 0.5 RT 2] REPEAT 45
[FD 3 RT 2]]
END
TO ELLIPSEl
SETBR 0 SETBG 0 SETPe 1
SETSeR [100 100] EGG L T 45
SETseR [90 90] EGG L T 45
SETseR [80 80] EGG L T 45
SETSeR. ..
SETseR [30 30] EGG L T 45
END
Section 10
Pot-pourri
TO CALMSEA
PU SETPOS [-126 -30]
PD SETH 0 RT 90 SETPC 5
SETBG 1
WAVE 1
END
53
54 Turtle Fun: LOGO for the Spectrum 48K
This procedure draws a picture of the sea at the bottom half of the
screen [-126 -30] by calling the subprocedure WAVE.
TO WAVE :STRIPE
FD 200 RT 90
PU FD :STRIPE LT 90 PD
IF :STRIPE > 10 [STOP]
WAVE :STRIPE + 1
END
TO SUN-SET
PUHTHOMEPD
SETBG 1 SETPC 3
POLY 0.1200.014
PU HOME PD SETPC 2
POLY 0.1 20 0.1 10
END
TO MOUNTAINS
RT45
FD 5 + RANDOM 10
IF OR YCOR > 50 YCOR < 0 [SETH 20 +
RANDOM 170- HEADING]
MOUNTAINS
END
TO TREE :SIZE
IF :SIZE <2 [STOP]
FD :SIZE
RT 15
TREE (3 * :SIZE/4)
Pot-pourri 55
LT 30
TREE (3 * :SIZE/4)
RT 15
BK :SIZE
END
TO TILE :SIDE
REPEAT 4 [FD :SIDE LT 45 FD :SIDE BK :SIDE
RT 135]
END
56 Turtle Fun: LOGO for the Spectrum 48K
TO TILEI :SIDE
REPEAT 4 [FD :SIDE LT 45 FD :SIDE LT 45 TILE
:SIDE RT 45 BK :SIDE RT 135]
END
TO TILE2 :SIDE
REPEAT 4 [FD :W LT 45 FD :SIDE LT 45 TILE!
:SIDE RT 45 BK :SIDE RT 135]
END
Artificial Intelligence
We begin by dividing the screen into two halves. The top half is the
LAND and the bottom half is WATER. The plan is simple. If the turtle
is at the top half of the screen then do such and such. If the turtle is at
the bottom half of the screen do something else. In the meantime, move
about the screen in a random fashion. In this program our beasty runs
on the land, leaving a random trail all over the top half of the screen
and whenever it moves into the area called WATER it begins to 'swim'
with its pen up at the bottom half of the screen.
To write this program we need three procedures, WATER, LAND
and BEASTY.
TO WATER
FD 25 RT 90 - RANDOM 180
END
TO LAND
PD FD 15 WAIT 50 RT 45 - RANDOM 90 PU
END
TO BEASTY
IF YCOR > 0 [LAND] [WATER]
BEASTY
END
Treacle-pops
TO TREACLE
FD 5 LT RANDOM 360
IF (AND XCOR > 10 XCOR < 20 YCOR > 10 YCOR
< 20) [SETCUR [16 10] FLASH PR
[TREACLE-POPS] STOP]
TREACLE
END
TO JAR
PU SETPOS [10 10] PD
SETPOS [20 10] SETPOS [20 20]
SETPOS [20 10] SETPOS [10 10]
PU FD 20 + RANDOM 80
TREACLE
END
To start this program you type JAR. The beasty flies about the screen
until it lands in the jar of treacle.
Pot-pourri 59
Sandhopper's picnic
TO SANDHOPPER
SETBG 6 SETPC 0
IF (AND YCOR > -20 YCOR < 20 XCOR > -20
XCOR < 20) [PD SOUND [0.05 -22] FD 5
RT RANDOM 360] [PU FD 50
RT 90 - RANDOM 180]
SANDHOPPER
END
otherwise
[PU FD 50 RT 90 - RANDOM 180]
TO MOUSE
SETBG 0 SETPC 7
FRAMES
MOVE
IF (AND XCOR < -10 XCOR> -127
YCOR < 87 YCOR > 10) [CS RECIPES]
IF (AND XCOR < -10 XCOR > -127
YCOR < -10 YCOR > -88) [CS ADDRESSES]
IF (AND XCOR < 127 XCOR > 10 YCOR < 87
YCOR < 10) [CS BILLS]
IF (AND XCOR < 127 XCOR > 10 YCOR <-10
YCOR> -88) [CS LEISURE]
MOUSE
END
TO FRAMES
SETCUR [3 5] FLASH PR [RECIPES]
SETCUR [22 5] FLASH PR [BILLS
SETCUR [3 15] FLASH PR [ADDRESSES]
SETCUR [22 15] FLASH PR [LEISURE]
END
62 Turtle Fun: LOGO for the Spectrum 48K
Ireelpes I
TO MOVE
PU MAKE" ANS RC
IF :ANS = "F [FD 30]
IF :ANS = "R [RT 45]
IF :ANS = "L [L T 45]
PD
END
TO RECIPES
SETBG 2
FRAMESI
IF (AND XCOR < -10 XCOR > -127
YCOR < 87... [CS CHINESE]
IF (AND XCOR < -10 XCOR > -127
YCOR < -IO... [CS CAKES]
IF (AND XCOR < 127 XCOR > 10
YCOR < 87 ... [CS FISH]
IF (AND XCOR < 127 XCOR > 10
The LOGO mouse 63
TO FRAMESI
SETCUR [35] PR [CHINESE]
SETCUR [22 5] PR [FISH]
SETCUR [3 15] PR [CAKES]
SETCUR [22 15] PR [VEGETARIAN]
END
TO CHINESE
SOUND [1 11] SOUND [1 13] SOUND [1 15]
SOUND [1 18] SOUND [0.5 20] SOUND [0.5 23]
SOUND [0.5 20] SOUND [0.5 18] SOUND [1 15]
SOUND [1 13]
TS PR [EGG-FLOWERSOUP PAO-TZU
SNOW-MOUNTAIN]
CHINESE
END
Again the Chinese recipes are subdivided into individual dishes. Can
you write a procedure that will allow you select one dish at a time?
TO EGG-FLOWER-SOUP
PR [Egg Flower Soup]
PR [1 egg]
PR [1 - 2 pints high broth]
PR [1 tablespoon chopped chives]
PR [ teaspoon sesame oil]
END
64 Turtle Fun: LOGO for the Spectrum 48K
TO PAO-TZU
PR [Pao-Tzu]
PR [lIb flour]
PR [I teaspoon dried yeast]
PR [ pint water]
PR [I tablespoon sugar]
END
TOSNOW.MTN
PR [Prawns on the snow mountain]
PR [6 oz fresh peeled prawns]
PR [10 egg whites]
PR [2 teaspoons cornflour]
PR [ teaspoon salt]
PR [I oz minced smoked ham]
PR [2 tablespoons chopped coriander leaves]
PR [oil for deep frying]
END
PART 2 WORD FUN
65
Section 12
Looking at lists
LOGO not only offers you turtle graphics; it can also handle words.
This ability to handle words is called list processing.
Everyday lists
Lists are an everyday feature of our lives. We have shopping lists, club
membership lists, lists of things to do, lists of information such as
birthdays and other important dates, lists of addresses and telephone
numbers and, if you are a very busy person, lists of lists! The list is
endless. Information of all sorts can be written down as a list. The same
applies to LOGO. You can store information or data in the form of
lists, which in turn can be manipulated (processed) by the computer.
LOGO lists
go a stage further, you can have a list (or lists) within the main list.
In everyday terms this could be in the form of
Milk, egg and butter now belong to the list of items that are to be
purchased from Superstores, while fruit, veg and fish are to be
purchased from the Market.
In LOGO you can write this as
The outer brackets represent the shopping list, while the purchases
from the different shop/market are written as separate lists.
Things to do
Letter to Aunt Mabel
Collect shoes
Return library book
The outer brackets represent the note for the milkman, while each
separate item is written as an individual list
Again the outer brackets represent the list 'Things to do', while each
thing to be done is written as an individual list.
By the way, how many items are there in this list? The answer is 3.
68 Turtle Fun : LOGO for the Spectrum 48K
How many items in this list? The answer this time is 5! There are no
individual lists inside the main list, but only items, that is five words.
Try some more!
This is a list of names which contains three items. It is made of one list
[Captain Tee] and two words - Selina and Doc.
1. [A BCD I 2 3]
2. [[A B] C D [1 23]]
3. [Ash Beech Fir Lilac]
4. [Clive [Jill Ball] David]
5. [Manchester [York] Cambridge [Camberley]]
6. [ ]
There are no prizes for correct answers, but hopefully your answers
should be
1. 7
2. 4
3. 4
4. 3
5. 4
6. 0
By now you may have realised that anything placed inside brackets is
in LOGO terms - a list. And what is more you can use as many lists as
you want. You can have lists inside lists inside lists inside lists ... If you
like playing around with brackets (that is, if you are a parenthetico-
phile) you can have a whale of a time thinking up all sorts of
combinations and variations. This one I call 'maximum security'
[[[[[[[[0]]]]]]]]
fA
~
Is
You have already used lists with Turtle graphics, for example
where [STOP] is a list that contains one item, the command word
STOP.
In the procedure PLAY, the line
TO POSTCARD
PR SE [DEAR] :TITLES o
PR :MESSAGE
PR :ENDINGS
PR [JO]
POSTCARD
END
For this you can use MAKE statements. Could you use subprocedures?
In theory yes, but unfortunately the Sinclair version of LOGO, like
some other LOGOs, does not allow you to do this. You have to write
the MAKE statements outside of the procedure POSTCARD. Having
written your random phrases using MAKE statements, there is
therefore no point in putting them inside procedures.
Autopoet
This program goes a stage further and expands the use of random
phrases by teaching your computer to write poetry! The lines of poetry
(borrowed from Shakespeare) are included in three separate MAKE
statements.
TO PICK :PHRASE
PR ITEM (1 + RANDOM COUNT :PHRASE) :PHRASE
END
ITEM and COUNT are both primitives, which you will find explained
in the User Manual. Briefly, ITEM outputs an item from a list, while
COUNT tells you how many items there are in a list. So in PICK we ask
the computer to make a random COUNT and pick out one of the
phrases in the MAKE statement and print it.
The variable :PHRASE is used to put the lines of poetry into the
procedure PICK. You could call this variable any name you like.
The final program is called AUTO. POET and is as follows
TO AUTO. POET
PICK :A
PICK :B
PICK :C
END
Let's now put some real phrases from Shakespeare's sonnets into the
MAKE statements A, B, and C.
MAKE "A [[When I do count the clock that tells the time]
[And see the brave day sunk in hideous night] [Not from the
stars do I my judgement pluck][When I consider every thing
that grows]]
MAKE "B [[Then of thy beauty do I question make] [Since
sweets and beauties do themselves forsake] [Or say with
princes if it shall go well] [Thy end is truth's and beauty's
doom and date]]
MAKE "c [[Much like than your painted counterfeit]
[Neither in inward worth, nor outward fair] [Can make you
live your self in eyes of men] [And you must live, drawn by
your own sweet skill]]
Master Mind
TO MASTER.MIND
TUNE
FLASH PR [What is your subject?]
MAKE HANS RL
76
Master Mind 77
The quiz begins with a jingle called TUNE that you can write yourself.
The uses of FLASH , WAIT, CT, SETTe, NORMAL, etc. are purely
cosmetic. They can be used how you like to present the text on the
screen.
RL or READ LIST waits for the player to type in an answer at the
keyboard . The MAKE statement with HANS enables you to use the
replies as part of the computer's own sentences as in
The computer next uses the player's reply :ANS 1 and :ANS in its next
sentence on the screen.
Adventure games
Since LOGO can handle words, you can use LOGO to write your own
adventure games. For those of you who are not familiar with this type
of game, you, the player, assume the role of the hero or heroine.
Depending on the game, you can be a swash-buckling knight or Super
Girl. You can be an animal or an explorer. The games take place in a
fantasy world in which you have to overcome obstacles and reach a
final goal. On the way you can meet monsters, find treasure and
generally use your wits to solve problems and defeat your opponents.
To begin, here is a simple adventure game in which you have to find the
Pink Punk. He is hiding somewhere in the house. You have to search
for him. The figure opposite shows you a plan of some of the rooms
used in the game. A room in an adventure is simply a location and
could be a wood or a cave, and not just a room of a house.
It is a good idea to draw a plan of the rooms that you wish to use
before you start programming. I have used a separate procedure for
each room (location). By typing the name of a procedure, you are
transported at once to that location. I have also used RANDOM to
make the game a little more interesting. The whole game is only an
outline for an adventure. It is only meant to provide you with some
ideas to use in your own adventure games.
79
80 Turtle Fun: LOGO for the Spectrum 48K
Downstairs easement
yellow
+une. etc...
Contents
choice,
Cupboard
Silverdoor
TO START
CS SETBG 3 SETPC 7
SETCUR [3 10]
BRIGHT 1
FLASH PR [FIND THE PINK PUNK!]
TUNE
SETCUR [3 12]
Adventure games 81
TO YELLOW
TUNE TS SETTC [7 3]
PR [ In the Yellow bedroom you find some objects. Is the
Pink Punk hiding in one of them? Type CONTENTS to
find out what the objects are]
END
TO CONTENTS
TSSETTC[75] 7
FLASH PR [WARDROBE BED CUPBOARD] •
END
82 Turtle Fun: LOGO for the Spectrum 48K
TO WARDROBE
TUNE TS SETTC [7 1]
PR [The Wardrobe is full of ancient dressing-gowns. But at
the back there are two secret doors. One is SILVER, the
other is BRONZE.]
END
TO BRONZE
TUNE TS SETTC [7 3]
PR [You go through the Bronze door and find two passages.
One is a NARROW passage while the other is a BROAD
cheerfully-lit passage.]
END
TO BROAD
TUNE PP SETCUR [3 10] BRIGHT I
PR [Here's your key Sir!]
END
TO TUNE
SOUND [120] SOUND [0.222]
SOUND [I 23] SOUND [0.2 19]
SOUND [0.5 20] SOUND [0.15 22]
SOUND [0.5 23] SOUND [0.1 26]
SOUND [1.525]
END
TO DOWNSTAIRS
TS SETTC [7 4]
SOU ND [I -18] SOUND [1 -19]
SOUND [0.6 -20]
PR [You see before you a winding staircase leading down to
the kitchen. At the bottom of the stairs there is a huge
hairy hound asleep. You could if you are very careful
tiptoe past it. Will you dare, YEA or NAY?]
END
TOPP
END
Adventure games 83
This last procedure draws the head of the Pink Punk on the screen. You
can fill in the details!
How you display the text is purely a matter of choice. I have used
SETCUR to set the cursor roughly in the middle of the screen, and I
have used BRIGHT 1 to display the text "Find the Pink Punk". I have
also included TUNE, which appears every time you get closer to
finding that elusive cat. Background colour and text can be set using
either the graphics mode SETBG and SETPC or by using the text mode
SETTC followed by the background colour and the text colour. Again
this is a matter of choice. I find that TS (TEXTSCREEN) is necessary if
you do not want all the previous text appearing at the same time as the
new text on the screen. You can of course omit TS if you wish.
You may wish to develop this program and for this purpose I have
left some of the rooms undefined. If you are not interested in the Pink
Punk, you can still use this program as a model for your own ideas.
TO LANDSCAPE
LT 90 FD 200 BK 600 RT 45
FD RANDOM 100 LT RANDOM 100 HOME
L T 20 FD RANDOM 50 LT RANDOM 90 FD RANDOM
100 SETH 0
END
There is plenty of scope here for improvement, as you will see when you
run LANDSCAPE!
After the text, MENU calls the subprocedure TAKE. This allows
you to take one useful object with you. The second subprocedure
BEGIN starts your journey on the planet.
TO TAKE
PR [Before you set off to explore the planet, you may take
one of the following objects]
FLASH PR [SWORD HAMMER BOTTLE]
PR []
NORMAL PR [Remember you can only choose one object.
Which one will you take with you?]
MAKE "INV LPUT RL :INV
END
MAKE "INV [ ]
MAKE "INV [ ]
TO CHECK
SETTC [3 7]
SOUND [ ]
FLASH PR :INV
SETTC [7 1]
END
TO GET :OBJECT
MAKE "INV LPUT :OBJECT :INV
PR SE[You have picked up the] :OBJECT
END
GET "BOTTLE
Writing GET as a procedure does tend to limit its use, since once you
have used GET you find yourself outside the current room. Could you
use GET some other way to pick up objects and keep the game moving?
TO BEGIN
WAIT 50
SETTC [7 2]
CT
PR [Now you are ready to set off. You consult your
compass. But which way?]
END
You are left to decide for yourself. You must find the appropriate
word! Seasoned players of adventure games will be familiar with this
type of decision. They usually have a vast dictionary of useful words
which enable them to play adventures. But for those of you who are
new to the adventure game, the clue is 'compass'.
Adventure games 87
TO EAST
SETTC [1 7]
PR [You see a dark cave in front of you.]
IF RL = [ENTER] [PR [Inside the entrance you find an old
lantern and a stick on the ground.]] [PR [You can't use
that word! Try EAST again.]]
IF RL = [LANTERN] [CAVE1] [HOLE]
END
TO CAVEI
PR [Off you go]
MAKE "INV LPUT "LANTERN :INV
CAVE2
END
If on the other hand you are unfortunate to choose the stick (or use
another word) you are plunged into the procedure HOLE!
TO HOLE
PR [It is so dark that you fall down a deep hole and that's the
end of you!]
END
TOCAVE2
PR [You see an upper and a lower passage. Which one will
you choose?]
END
88 Turtle Fun: LOGO for the Spectrum 48K
TO LOWER
PR [You find a door covered with brass buttons]
IF RL = [PUSH] [CA VE3] [CA VE2]
END
TO CAVE3
PR [The door swings back to let you through]
WAIT 200
PR [At the end of this passage there is a light. You hear a
strange noise]
WAIT 100
SOUND [0.5 -14] SOUND [0.5 -15] SOUND [1 -16]
SOUND [2 -2]
WAIT 200
PR [You discover an underground city]
WAIT 100
CITY
END
TO UPPER
PR [You see a red door]
WAIT 50
IF MEMBERP [HAMMER] :INV [PR [You strike the
door with your hammer and the door opens]] [(ANS)
STOP]
CAVE4
END
TOANS
PR [You can't enter because you did not pick up the
hammer!]
CAVE2
END
TOCAVE4
PR [You arrive back at the surface of the planet]
END
TO CITY
WAIT 100
PR [You have struck lucky! There is more than enough
Adventure games 89
special ore here to refuel your engines. The city is also
inhabited by friendly robots who will help you to take
the ore to your spaceship]
WAIT 50
PR [Your next task is to find water and food for yourself and
your crew]
WAIT 200
BEGIN
END
TO AIRLOCK
SETTC [02]
PR [You enter an airlock. The door closes behind you and
you suddenly notice that the oxygen is running out. Be
quick and find a way out!]
WAIT 200
IF RL = [SHOOT] [PR [You melt down a concealed door
and enter an underground city] CITY STOP] [PR
[Hurry up you only have 50% oxygen left]]
WAIT 150
PR [Your oxygen supply is nearly up!..]
IF RL = [SHOOT] [PR [You melt down a concealed door]
CITY STOP] [PR [Too late you have snuffed it!]]
END
TO WEST
PR [You find yourself in a vast arid desert. But after a few
hours of wandering you find a waterhole]
IF MEMBERP [BOTTLE] [PR [You fill your bottle]
[(ANS2)]
DESERT
END
TO ANS2
PR [You can't go on without a bottle!]
MENU
END
TO DESERT
PR [After walking for another hour you come to a steep
mountain range. At the foot of the mountains there are a
90 Turtle Fun: LOGO for the Spectrum 48K
TO BROWN
PR [The ladder takes you down on to a sandy floor. There is
a damp smell. To your left you see a sword and an old
rope lying on the sand]
IF RL = [SWORD] [PUT] [ROPE]
WAIT 100
PR [You continue along a low tunnel until you reach a
chamber with metal walls. There is a door and it is
partially open.]
IF RL = [ENTER] [PR [You have entered an airlock. The
door suddenly closes behind you.] AIRLOCK STOP]
[PR [That is not the correct command. Try BROWN
again!]]
END
TO GREEN
PR [The ladder descends into a deep well. If you like
swimming, you might explore further!]
END
TO ROPE
MAKE " INV LPUT "ROPE :INV
END
TO PUT
MAKE "INV LPUT " SWORD :INV
END
TO SOUTH
PR [The terrain is very rocky and uneven . There are huge
sand dunes and wide gaping craters. The whole area is
very gloomy and creepy]
SOUND [1-10] SOUND [0.5 -11] SOUND [1-20] SOUND
[1 -2I[ SOUND [1 -22]
SETTC [0 3]
Adventure games 91
PR [Just over the next rise you suddenly see a huge hairy
Nurk. It attacks you]
WAIT 100
PR [You can kill it if you use the correct weapon]
IF RL = [SHOOT] [PR [The Nurk runs away in pain. You
are safe for the moment. Better choose another direction
before it is too late!]] [PR [The Nurk gets you.] MENU]
END
Any mistakes or blunders in your attempts to explore the Planet either
return you back to MENU or finish you off!
Not all of the rooms are complete. You are free to write your own
procedures for NORTH and its subsequent rooms. I find it a good idea
to link all the rooms in the program by the use of random commands.
This use of random makes the game slightly different each time and
allows players to explore most of the rooms in one session.
Since you have the plan of the rooms you can of course modify the
program for your own games. Feel free to use the structure of the
Purple Planet, ifnot the story. Remember, how you write adventures is
up to you. Don't be influenced too much by commercial games. There
are no hard and fast rules on how adventures should be written.
Index
92
Index 93
The intelligent bug 57 WAIT 9,10
The Purple Planet 83 WINDOW 28
Total Turtle Trip (TTT) 48 window-system 60
TOWARDS 28
Treacle-pops 58 XeOR 58.61
variable(s) 16,18-24 yeOR 57,58,61
94