Let’s practice reading and understanding NetLogo code together.
Activity 1: Code Reading¶
Look at this simple procedure. What do you think it does?
to setup
clear-all
create-turtles 100 [
setxy random-xcor random-ycor
set color one-of [red green blue yellow]
]
reset-ticks
end
Answer (only click after discussing)
This procedure:
- Clears everything from the world
- Creates 100 turtles
- Places each turtle at a random location
- Gives each turtle a random color (red, green, blue, or yellow)
- Resets the tick counter to 0
Activity 2: Spot the Difference¶
Compare these two procedures. What’s different?
Procedure A:
to move-turtles
ask turtles [
forward 1
]
end
Procedure B:
to move-turtles
ask turtles [
right random 360
forward 1
]
end
What’s the difference in behavior? TTYN
Answer (only click after discussing)
- Procedure A: All turtles move straight forward
- Procedure B: All turtles turn randomly first, then move forward
Activity 3: Human Computer¶
Let’s act out NetLogo commands!
Setup:
- 5 students are “turtles”
- Classroom is the “world”
- One student is the “computer” giving commands
Commands to try:
create-turtles 5
- 5 students enter the “world”ask turtles [ forward 2 ]
- everyone takes 2 steps forwardask turtles [ right 90 ]
- everyone turns rightask turtle 0 [ set color red ]
- student #0 puts on red hatask turtles [ if color = red [ forward 3 ] ]
- only red turtle moves