Table of Contents
1. Creating the world |
2. Buttons and Procedures |
3. Sliders and Variables |
4. Creating Turtles and Patches |
5. Making the Model Go |
In this part of the practical, we will start to create the fundamental components of our model: the turtles and the patches.
setup
procedure. We will now
improve it so that it does something more useful.setup
function so that it reads:
to setup
print "Setting Up Model"
clear-all
setup-patches
setup-turtles
end
The clear-all
command tells NetLogo to reset everything ready for us to
create a new model. The other two lines (setup-patches
and setup-turtles
)
call two new procedures that will contain commands to set up the turtles and patches. We
could put these commands directly into the setup
procedure, but by putting them
into their own separate procedures it makes the model code easier to understand.
end
of the setup function.
to setup-patches
ask patches [ set pcolor green ]
end
to setup-turtles
create-turtles initial-number-of-turtles
ask turtles [
set shape "sheep"
setxy (random 20) (random 20)
set color blue
]
end
The code above should be fairly self-explanatory. The first procedure
(setup-patches
) does one thing: it asks all the patches to change their
colour to green. This means that each patch will represent a square of grass, ready
to be eaten by the turtles.
The second procedure (setup-turtles
) creates the turtles. There are some
new commands in there that you wont be familiar with:
create-turtles
makes a number of new turtles. Instead of saying
exactly the number of turtles that we want (e.g. 10), we use the value from the slider
(initial-number-of-turtles
).ask turtles
is used to run some commands on the turtles that
have just been created. Notice that the commands we want to send to the turtles are
enclosed in square brackets ([
and ]
).set shape "sheep"
uses the set
command to change the
shape of the turtle. This is the same as changing other turtles variables such as
their color
. Have a look
here if you would like to see a list of all the shapes that are available.setxy (random 20) (random 20)
is a quick way of setting the turtles'
x
and y
coordinates. The random 20
means choose
a random number between 0 and 20. This is exactly the same as using two commands,
one after the other:
set xcor (random 20)
set ycor (random 20)
set color blue
makes our sheep blue. Now, try going back to the 'Interface' tab. If you have made any mistakes with the model code, NetLogo will tell you about them. Otherwise you will see the main interface again.
That's almost everything. To finish the basic model, move onto the final section to get the sheep moving around the environment.
ask
command as we did in the last practical).
fd 1
tells a turtle to go one step forward).