In a NetLogo model turtles are mobile agents. Of course a turtle doesn't have to represent a real turtle. It can represent a frog, cow, person, robot, or any other type of mobile agent.
Every turtle is born with 13 attributes (an attribute is a variable encapsulated by a turtle or patch):
who
color
heading
xcor
ycor
shape
label
label-color
breed
hidden?
size
pen-size
pen-mode
Additional attributes can be defined as follows:
turtles-own [energy age]
We can change these attributes using the set command:
set VARIABLE EXPRESSION
For example, here's a procedure a turtle can execute to initialize its attributes:
to init-turtle
set age 0
set energy 100
set color red
set shape "turtle"
set size 4
set label-color yellow
set label "arf!"
; make position and heading random:
set xcor random-xcor
set ycor random-ycor
set heading random 360
end
Note: NetLogo ignores everything between a semicolon and the end of a line. This is how we comment NetLogo programs.
Here's a snapshot of the pond containing 10 turtles taken just after this procedure has been executed by each one. Notice that the turtles are randomly distributed with random headings (and random amounts of energy, although this isn't visible). Also notice the yellow "arf!" labels next to each turtle:
We can inspect individual turtles by clicking on them with the right mouse button:
We can set hidden? to true or false:
ht ; set visible? false
st ; set visible? true
Note: NetLogo syntax is very liberal. Names may contain punctuation marks. Conventionally, names of Boolean-valued variables end with a question mark.
The pen-mode of a turtle is one of "down", "up", and "erase". These commands change pen-mode:
pu ; set pen-mode "up"
pd ; set pen-mode "down"
pe ; set pen-mode "erase"
The first attribute, who, is the turtle's unique identification number. This number can be used to ask specific turtles to execute command blocks:
ask turtle 5 [pd fd 5 pu] ; ask turtle #5 to draw a line
Here's another snapshot after this command has been executed:
We can also ask sets of turtles to execute blocks of commands:
ask turtles [pd fd 5 pu] ; ask each turtle to draw a line
Here's a screen shot after this command has been executed:
We have already seen that the observer can create and clear turtles:
crt 100 ; create 100 turtles
ct ; now get rid of them (ct = clear turtles)
Patches can sprout turtles:
ask patch 0 0 [sprout 3]
Turtles can give hatch new turtles:
ask turtle 5 [hatch 3]
When a turtle executes the die command it ceases to exist:
ask turtle 0 [die print "bye"] ; "bye" never printed
Note: We can use the print command to print strings to the command center window. This is useful for debugging. We can also use show, type, and write.
A turtle's position and heading are given by the attributes xcor, ycor, and heading, respectively. Besides using the set command, there are a number of ways to change position and heading.
We can move a turtle to a particular patch:
ask turtle 0 [move-to patch 0 0]
We can also simply set the coordinates of a turtle. For example, here are three ways to ask a turtle to move to the center of the pond:
ask turtle 0 [set xcor 0 set ycor 0]
ask turtle 0 [setxy 0 0]
ask turtle 0 [home]
We can ask a turtle to turn toward a particular patch:
ask turtle 0 [face patch 5 5]
ask turtle 0 [facexy 5 5]
ask turtle 0 [set heading 45]
Note: Headings are in degrees. If we think of the top of the pond as north, then a heading of 180 degrees is due south.
We can ask a turtle to move forward or backward some number of steps in the direction of its heading:
ask turtle 10 [fd 10]
ask turtle 10 [bk 10]
We can ask a turtle to move to a neighboring patch with smallest or largest value of some attribute:
ask turtle 10 [downhill danger]
ask turtle 10 [uphill temperature]
Note: If the turtle's pen is down, then a line is drawn from the starting position of the turtle to the end position. The color of the line will be the color of the turtle. The thickness of the line will be determined by the pen-size attribute.
We can ask a turtle to turn right or left some number of degrees:
ask turtle 10 [rt 30]
ask turtle 10 [lt 45]
Turtles can compute distances and angles
to hunt [prey]
set heading towards prey
fd distance
prey
ask prey [die]
end
Here are a few other navigational aids:
distancexy 0 0
towardsxy
patch-at
patch-ahead
patch-here
subtract-headings