|
Step 1. Adding buttons to the interface
A. Add a 'setup' button (click for image): Under the Interface tab, click on +Add [button] at the top; type setup in the commands box
B. Add a 'go' button: Add another button, type go in the commands box, and check the "Forever" box
Step 2. Adding code
A. Add a chunk of code for each button: Click on on the "Procedures" tab and add the following code:
to setup
clear-all
create-turtles 25 [ use setup commands for turtles here ]
ask patches [ use setup commands for patches here ]
end
to go
ask turtles [ use commands for turtles here ]
ask patches [ use commands for patches here ]
end
|
Step 3. Syntax
A procedure is a chunk of code with the format
to procedure-name
commands
end |
Agents are addressed with ask followed by commands in brackets
ask turtles [forward 1]
ask patches [set pcolor blue] |
Brackets are also used for some variables and keywords
ask turtles [set [pcolor] of patch-here orange]
ask patches with [pcolor = black] [set pcolor yellow] |
Numbers, mathematical operations, etc. are enclosed in parenthesis
| ask (.5 * count turtles) turtles [forward 1] |
= 50% of the total numbers of turtles are asked to move forward 1
Logic
if conditions [run these commands]
ifelse conditions [run these commands] [if not, run these commands |
= if is followed by a set of conditions then the commands set in [brackets] are run if they are met
Step 4. Common codes & examples (click here for the complete dictionary)
| Code |
What it does |
Also try |
|
|
ask turtles [forward 1]
|
All turtles move forward 1 patch
|
back, left, right, jump
Can use any number, decimals, generate random numbers, ie 10, 5.5, random 50
|
|
ask turtles [set xcor 10 set color blue set heading 180]
ask patches [set pcolor green]
|
set is used for individual variables like color/pcolor (turtle/patch), xcor/pxcor (turtle/patch), heading, etc.
|
For turtles:
set heading 360
set heading (heading + 10)
set pcolor of patch-here green
set shape "butterfly"
set size "5"
set size size + 1
set [pcolor] of patch-here green
For patches:
ask patch-ahead 5 [set pcolor blue]
|
|
random 100
|
Randomly picks a number between 0 and 99
|
random-xcor and random-ycor (for turtles), random-pxcor and random-pycor (for patches)
|
ask patch 0 0 [set pcolor green]
ask turtle 13 [set shape "bug"]
ask turtles-at 5 5 [set xcor 0 set ycor 0]
|
Patches are identified by their x-coordinate and y-coordinate; Turtles are numbered but also have coordinates.
|
ask turtles with [color = blue] [jump random 50]
ask turtles with [xcor != 0] [set xcor 0] != means without/not equal to
ask patches with [pycor > 5] [set pcolor black]
|
ask turtles [if [pcolor] of patch-here = green [forward 5]]
ask patches [if any? turtles-here [set pcolor white]]
|
A turtle can interact with the patch by asking about it's variables with, for ex, patch-here
A patch can determine if there are turtles on it by asking any? turtles-here
|
ifelse [forward 5] [back 5]
if random 10 > 5 [set heading random 360 forward 10]
|
to go
ask turtles [forward 5]
ask turtles [if xcor > 5 [turn-around]]
end
to turn-around
set heading (heading - 180)
end
|
You can name a procedure anything, and then put the name in a set of
commands for turtles/patches; The turtles/patches will run those
commands then return to the first procedure and run the remainder of
the commands. |
to go
ask patches with [pxcor > 5] [check-for-turtles]
end
to check-for-turtles
if any? turtles-here [ask turtles-here [fd 10]]
end
|
|
Net logo
Post new comment