You are on page 1of 8

DRAFT Feb 2011: L ikely to change

FMSLogo
Using Multiple Turtles
Like many implementations, FMSLogo supports multiple Turtles. These extend the
possibilities for graphical work as well as opening up experiments on interacton
between Turtles.
This workbook aims to provide some fairly straightforward examples based on my
own initial explorations. IT IS A WORK-IN-PROGRESS AND IS STILL CHANGING.
Some of these activities are repeated in my workbook on using bitmapped Turtles
(see Activity Book 3: "Using Bitmaps").
Snail Races is a project workbook on my site that uses multiple bitmapped Turtles
and pixel checking (see below).
You could also look at Chapter 11 in The Great Logo Adventure by Jim Muller
which has some interesting ideas to explore. I have reproduced his predator-prey
model in a separate workbook.

Overview

FMSLogo supports unlimited turtles; each is numbered. The default Turtle is zero.
Each Turtle has its own state properties, i.e. its own heading, position and pen
control.
In addition, if you want to, each Turtle can be given its own individual pen.
The more Turtles you use the slower Logo will go.
When using multiple turtles always use consecutive index numbers. If you use non-
consecutive numbers Logo assumes all the Turtles in between require processing.
CLEARSCREEN always resets all turtles to their default state, i.e. showing and with
the pen down, except Turtle 0 (the default) which retains its penstate and its
hide/show mode.

Generally, in this workbook I am ignoring Turtle 0, the default Turtle, using it


only for generic tasks.

File: multiple turtles.docx 1 http://davidlongman.com/logo

Created:21-Feb-11 David Longman: david.longman@newport.ac.uk Last saved: 21-Feb-11


In most of the activities in this workbook there's two stages:
1. set up the turtles so that they are positioned where you want them;
2. run the actions for each Turtle.

Commands used

The following specific commands are used in this workbook.

 SETTURTLE tells Logo which Turtle to activate. Once activated


all commands are carried out by that Turtle.
Takes a number as input. Has optional input "true
or "false. If TRUE active Turtle has its own pen
which can be set independently. If FALSE pen is
restored. Default is FALSE.

 TURTLE Reports which Turtle is currently active.

 ASK Allows controls of a Turtle by activating it


temporarily.
Takes a Turtle number as input and a list of
instructions.

A few additional commands are available. See FMSLogo Help > Contents >
Graphics > Multiple Turtles.
Commands specific to bitmapped Turtles are also covered in Using Bitmaps: Part 3
Bitmapped Turtles
To simplify managing multiple Turtles a little, Turtles 1 - 8 are used. Turtle 0 is
ignored unless used for general purpose tasks such as drawing backgrounds.
REPEAT is used frequently in this workbook and particular use is made of the
system variable REPCOUNT which is simply the number of the current repetition of
REPEAT. So, e.g. this will draw four circles of increasing size:
REPEAT 4 [CIRCLE 100 + REPCOUNT * 10]
produces circles of radii 110, 120, 130, 140
And this will draw four circles of decreasing size:
REPEAT 4 [CIRCLE 100 - REPCOUNT * 10]
produces circles of radii 90, 80, 70, 60

File: multiple turtles.docx 2 http://davidlongman.com/logo

Created:21-Feb-11 David Longman: david.longman@newport.ac.uk Last saved: 21-Feb-11


Inside procedures I make more use of the multi-line form of syntax for lists. The
previous REPEAT example would look like this in a procedure:
TO MCIRC
REPEAT 4 [
CIRCLE 100 - REPCOUNT * 10
]
END

Activity 1. Talking to Turtles

ASK, SETTURTLE, TURTLE, CLEARSCREEN


The most straightforward way to use multiple Turtles is with the ASK and
SETTURTLE commands.
CLEARSCREEN (CS) resets your Turtles so any setup must be repeated.

Activity 2. Multiple patterns

Let's start with four Turtles and run a simple graphical routine to draw a figure
in each quadrant of the graphics screen with centres at the points as
illustrated:

File: multiple turtles.docx 3 http://davidlongman.com/logo

Created:21-Feb-11 David Longman: david.longman@newport.ac.uk Last saved: 21-Feb-11


Each figure is made up of a set of rotated pentagons.

We need to move each Turtle to its designated point starting point and then
draw the figures. For illustration this is done in two stages. First, position the
Turtles:

CS
ASK 1 [PU SETPO [150 150] PD RT 18]
ASK 2 [PU SETPOS [150 -150] PD RT 18]
ASK 3 [PU SETPOS [-150 -150] PD RT 18]
ASK 4 [PU SETPOS [-150 150] PD RT 18]

Next, draw the figures. An individual pattern can be made wIth a procedure
like this:

TO HEXPATTERN
REPEAT 6 [ REPEAT 6 [ FD 50 LT 60 ] LT 60 ]
END

So, taking care with the brackets make all the Turtles draw a pattern in:

ASK 1 [ HEXPATTERN ]
ASK 2 [ HEXPATTERN ]
ASK 3 [ HEXPATTERN ]
ASK 4 [ HEXPATTERN ]

Note that if you type TURTLE, Logo will report zero, showing that the default
Turtle is still active. Typing any command such as FD or RT without using
ASK will control only Turtle 0.

Activity 3. Organising

To make these actions easier to manage let's put them into some procedures:

TO SETUP
CS
ASK 1 [PU SETPO [150 150] PD RT 18]
ASK 2 [PU SETPOS [150 -150] PD RT 18]
ASK 3 [PU SETPOS [-150 -150] PD RT 18]
ASK 4 [PU SETPOS [-150 150] PD RT 18]

File: multiple turtles.docx 4 http://davidlongman.com/logo

Created:21-Feb-11 David Longman: david.longman@newport.ac.uk Last saved: 21-Feb-11


END

TO FOURHEX
ASK 1 [ HEXPATTERN ]
ASK 2 [ HEXPATTERN ]
ASK 3 [ HEXPATTERN ]
ASK 4 [ HEXPATTERN ]
END

It's easy to see that in FOURHEX there are four versions of the same
instruction, only the Turtle index changes.

So FOURHEX could be simplified like this:

TO FOURHEX
REPEAT 4 [ ASK REPCOUNT [ HEXPATTERN ] ]
END

Activity 4. ASK ALL

In fact it is could often the case that the same action needs to be applied to
many Turtles. We can make our own procedure ASKALL to do this:

TO ASKALL :NUM :INSTRUCTIONS


REPEAT :NUM [ ASK REPCOUNT :INSTRUCTIONS ]
END

To hide or show all the Turtles at once:

ASKALL 4 [HT]
or
ASKALL 4 [ST]

FOURHEX can now be simplified still further:

TO FOURHEX
ASKALL 4 [ HEXPATTERN ]
END

Now type:

File: multiple turtles.docx 5 http://davidlongman.com/logo

Created:21-Feb-11 David Longman: david.longman@newport.ac.uk Last saved: 21-Feb-11


CS SETUP FOURHEX

Remember was said in the introduction about not asking for or activating more
Turtles than you need. The number used with ASKALL will activate that
number of Turtles. The more you use the slower everything goes.

You could experiment with this:

CS FOREVER [ASKALL 30 [PU

CLEARSCREEN (CS) will reset your multiple Turtles.

Activity 5. Make the patterns move.

FOURHEX can be made a little more interesting by animating the pattern to


make it spin. The technique used here is covered in other workbooks on my
website:

TO FOURHEX
ASKALL 4 [SETPC 0 HEXPATTERN] ;set the pen to black and draw
WAIT 1 ;put in a delay
ASKALL 4 [SETPC 7 HEXPATTERN] ;white pen removes the pattern
WAIT 1 ;another delay
ASKALL 4 [RT 6] ;rotate the Turtles a bit
END

Try this:

CS SETUP FOREVER [FOURHEX]

Hiding the Turtles has a significant effect on the speed of the animation:

CS SETUP ASKALL 4 [HT] FOREVER [FOURHEX]

Finally we can make this group of patterns slide across the screen using
SETX and XCOR:

TO FOURHEX
ASKALL 4 [SETPC 0 HEXPATTERN]
WAIT 1
ASKALL 4 [SETPC 7 HEXPATTERN]

File: multiple turtles.docx 6 http://davidlongman.com/logo

Created:21-Feb-11 David Longman: david.longman@newport.ac.uk Last saved: 21-Feb-11


WAIT 1
ASKALL 4 [RT 6 SETX XCOR - 10 ]
END

Experiment!

Activity 6. Particles

I am very pleased with this little activity as I sort of discovered it while writing
this workbook. I have seen things like it many times but you don't see this kind
of activity very often in Logo. My version is very simple and it is just a starting
point for something even more interesting - but at the time of writing I don't
that the time to do this.

A very good source is of course Starlogo and Mitchell Resnick's book "Turtles,
Termites and Traffic Jams." There is also the classic source book Turtle
Geometry by Abel and DiSessa and in there is simple predator-prey model.
Many other examples may suggest themselves.

Starlogo is designed from the ground up to be a parallel processing approach


and assumes many Turtles. In a Starlogo version o f TurtleLand, one Turtle
would be very unusual.

'Particles' is very simple pictorially but dynamic and really very effective. The
basis of a good project.

It only works in FMSLogo because it uses the ZOOM to magnify what is in fact
a very tiny image. That's because the particles are made using SETPIXEL to
colour jus tone pixel at a time. Each Turtle simply moves about inside a box in
the form of a yellow dot.

The advantage of this is that the visibility of the dots is greatly enhanced and
because there are in reality fewer pixels being used to display the particles
(the red box is very small in normal zoom) the simulation has a faster time
cycle.

The 'bounce' algorithm I used for this simulation is very simplistic and is one of
the first aspects to improve. I started learning about dot products at this point

File: multiple turtles.docx 7 http://davidlongman.com/logo

Created:21-Feb-11 David Longman: david.longman@newport.ac.uk Last saved: 21-Feb-11


but had to stop for want of time!

But it's still an interesting model and even in simple mode it can be used to
experiment with if...then type rules that test different kinds of pixel conditions.

It also illustrates a key feature of multiple turtles which is that each can have
its own behaviour.

can be applied to it and there are vaiours numbers to be adjusted

Activity 7.

You can create your own images using any tool you like. Make sure they are

saved in BMP format.

Activity 8.

Activity 9.

File: multiple turtles.docx 8 http://davidlongman.com/logo

Created:21-Feb-11 David Longman: david.longman@newport.ac.uk Last saved: 21-Feb-11

You might also like