Ask Turtle Commands - Santa Fe Institute

Basic NetLogo Commands
Procedure Keywords
to <name>
…
end
Statement used to name and start a procedure
Example: to setup
Statement used to end a procedure.
Clear Commands
clear-all
clear-turtles
clear-patches
Deletes all turtles and resets all patches to default values
Removes all the turtles without erasing the Netlogo surface or changing the patches
Clears the patches by resetting all patch variables to their default values, including setting
their color to black.
Create Turtles Commands
create-turtles #
sprout #
Creates the specified number of turtles at origin and gives a random heading to each turtle
Example: create-turtles 1
Patches create turtles. The turtle gets created on the patch that created it.
Example: sprout 2
Turtle Movement Commands
forward #
back #
pen-down
pen-up
setxy # #
set heading #
right #
left #
Tells an agent to move forward a number of patches
Example: forward 1
Tells an agent to move back a number of patches
Example: back 1
Tells an agent to put its pen down draw a trail
Tells an agent to pick up its pen and stop drawing a trail
Move the turtle to the x-coordinate and y-coordinate given
Example: setxy 10 20
Sets heading or direction that the agent faces in degrees relative to the Netlogo coordinate
system
Example: set heading 90
Asks the turtle to turn right a certain number of degrees relative to the direction the turtle is
facing
Example: right 90
Asks the turtle to turn left a certain number of degrees relative to the direction the turtle is
facing
Example: left 90
Document1
Turtle Attribute Commands
set color color_name
set size #
set pen-size #
set color #
set color R# G# B#
Specifies or changes the color of the turtles to the specified color (color_name). Some
possible color names include red, blue, green, orange, yellow, pink, magenta, lime, gray,
and violet.
Example: set color red
Specifies or changes the size of the turtles to a specified number (#). The default is 1.
Example: set size 3
Specifies or changes the size of the pen the turtles carry to a specified number (#). The
default is 1.
Example: set pen-size 3
Changes an agent’s color to # (see Netlogo Programming guide for color values). There
are 140 colors in Netlogo.
Example: set color to 15
sets the color to bright red
Changes an agent’s color to R# G# B# (RGB notation) where R# stands for how much red
is in the color, G# stands for how much green is in the color and B# stands for how much
blue is in the color. The R# G# and B# can range between 0 and 255.
Example: set color 255 0 0
sets the color to bright red
Ask Turtle Commands
ask turtles […..]
ask turtle # [ ]
Asks the turtles to perform the instructions (commands) given in square brackets. Each
turtle performs the instructions one at a time.
Example: ask turtles
[forward 1]
Asks a specific turtle to perform the commands in brackets.
Patch Attribute Commands
set pcolor # (or name or RGB)
The command line sets the patch color Color can be represented either as a NetLogo color (a single
number or name) or an RGB color (a list of 3 numbers).
Example: set pcolor 15
; set the color of the patch to red
ask patch # #
[commands]
Asks the specific patch to run the commands given in the command block
Example: ask patch 1 3
[set pcolor 15 ] ; asks patch 1 3 to set its color to red
Asks the all patches to run the commands given in the command block
Example: ask patchs
[set pcolor 15 ] ; asks all the patches to set their color to red
Ask Patches Commands
ask patches
[commands]
Document1
Patches Reporters
max-pxcor, max-pycor
min-pxcor, min-pxcor
patch-ahead #
These reporters give the maximum x-coordinate and maximum y-coordinate, (respectively) for patches,
which determines the size of the world.
Example: create-turtles 100
[ setxy random-float max-pxcor
random-float max-pycor ]
;; distributes 100 turtles randomly in the
;; first quadrant
These reporters give the minimum x-coordinate and minimum y-coordinate, (respectively) for patches,
which determines the size of the world.
Example: creare-turtles 100
[ setxy random-float min-pxcor
random-float min-pycor ]
;; distributes 100 turtles randomly in the
;; third quadrant
Give you patch that is the given distance, #, "ahead" of this turtle, that is, along the turtle's current
heading. Reports nobody if the patch does not exist because it is outside the world.
Example:
ask patch-ahead 1 [ set pcolor green ] ;; turns the patch 1 in front of this turtle green; note that this might
be the same patch the turtle is standing on
Variables Keywords
globals [variable_names]
turtles-own [variable_names]
patches-own [variable_names]
links-own [variable_names]
A keyword that can only be used at the beginning of a program, before any functions or
procedures. It defines new global variables. Global variables are "global" because they
are accessible by all agents and can be used anywhere in a model. Most often,
globals is used to define variables or constants that need to be used in many parts of
the program.
Example: globals [NumTurtles ColorTurtles] ;; declares two global variables
NumTurtles and ColorTurtles.
A keyword that can only be used at the beginning of a program, before any functions or
procedures. It defines new turtle variables unique to each turtle
Example: turtles-own [eyes legs] ;; declares two turtle variables eyes and legs.
A keyword that can only be used at the beginning of a program, before any functions or
procedures. It defines new patch variables unique to each patch
Example: patches-own [patchColor] ;; declares one patch variable patchColor.
A keyword that can only be used at the beginning of a program, before any functions or
procedures. It defines new link variables unique to each link.
Example: links-own [traffic] ;; declares one link variable traffic.
Document1
Get and Set Variables Commands
let local_variable_name value
set variable_name value
Creates a new local variable called local_variable_name and gives it the given value. A
local variable is one that exists only within the enclosing block of commands such as a
procedure or within the ask turtles brackets.
Example: let num1 10
This creates a local variable num1 and gives it an initial value of 10
Sets variable (variable_name) to the given value.
Example : set num1 25
Changes the value of the local variable num1 to 25
Looping Commands
repeat # [commands]
while [condition] [commands]
Repeats the set of commands in the square bracket a certain number (#) of times.
Example: repeat 20
[
right 5
forward 1
]
the turtle turn right 5o and steps forward 1 step 20 times
Repeats the set of commands in the second set of square brackets while the condition
in the first set of square brackets is true. When the condition within the first set of
square brackets is false, the loop is exited.
Conditionals Commands
if condition
[commands]
ifelse condition
[commands1]
[commands2]
If condition reports true, then the program runs the commands in the command block. The reporter may
report a different value for different agents, so some agents may run commands and others don't.
Example: if xcor > 0[ set color blue ]
;; turtles in the right half of the world turn blue
If condition is true, runs commands1.
If condition is false, runs commands2.
The reporter may report a different value for different agents, so some agents may run commands1
while others run commands2.
Example:
ask patches
[
ifelse pxcor > 0
[ set pcolor blue ]
[ set pcolor red ]
]
;; the left half of the world turns red and the right half turns blue
Document1
Random Number Commands
random #
If number is positive, reports a random integer greater than or equal to 0, but strictly
less than the number #.
If number is negative, reports a random integer less than or equal to 0, but strictly
greater than number.
If number is zero, the result is always 0 as well.
Examples:
show random 3
;; prints 0, 1, or 2
show random -3
;; prints 0, -1, or -2
random-pxcor, random-pycor
Provides a random integer ranging from min-pxcor (or -y) to max-pxcor (or -y) inclusive.
Example: setxy random-pxcor random-pycor
Tick Commands
reset-ticks
tick
ticks
Resets the tick counter to zero. Normally reset-ticks goes at the end of a setup
procedure
Advances the tick counter by one. If the tick counter has not been started yet with
reset-ticks, an error results. Normally tick goes at the end of a go procedure.
Reports the current value of the tick counter. The result is always a positive number
and never negative. If the tick counter has not been started yet with reset-ticks, an
error results.
Document1