Basic Program to setup clear-all reset-ticks end to go tick wait .4 end ►WRITING CODE What do you want to do? → When do you want it to happen? → Who is going to do it? → Anything else? Example: I want to make new turtles. I want it to happen during setup. I want the patches to make the turtles. Code: ►ASKS ask turtles [___] ask turtle x [___] ask n-of x turtles [___] ask turtles in-radius x [___] ask patches [___] ask patch x y [___] ask n-of x patches [___] ask patches in-radius x [___] ask links [___] ask link x [___] ask n-of x links [___] ►COMMANDS [set color x] pcolor x] size x] shape “shape”] heading x] heading heading + x] thickness x] [setxy x y] [fd x] [show “hello”] [crt x] [sprout x] [hatch x] [die] ►LOGIC if ___ [ ] ifelse ____ [ ][ ] ►RANDOM NUMBERS random x random-xcor random-ycor if random x < y ►COUNT if any? ___ if count ___ > x if count ___ in-radius ask n-of 3 patches [sprout 1 [setxy random-xcor random-ycor set color blue set size 3]] This will have 3 random patches each make 1 turtle and give it a random location, color blue, and size 3. EXAMPLE CODE WHAT DOES THIS DO? WHO CAN DO THIS? ask turtles [set color blue] asks all turtles to turn blue observer, patches, turtles, links ask turtle 4 [set heading 24] asks turtle with who 4 to set heading to 24 degrees observer, patches, turtles, links ask n-of 3 turtles [die] asks 3 random turtles to die observer, patches, turtles, links ask turtles in-radius 4 [die] asks all the turtles in a radius of 4 to die patches, turtles ask patches [set pcolor black] asks all patches to change their color to black observer, patches, turtles, links ask patch 3 2 [sprout 1] asks patch at location (3,2) to make a turtle observer, patches, turtles, links ask n-of 7 patches [set pcolor pcolor – 1] asks 7 random patches to decrease their pcolor by 1 observer, patches, turtles, links ask patches in-radius 2 [set pcolor 45] asks all the patches in a radius 2 to set pcolor patches, turtles ask links [set thickness 2] asks all links to set their thickness to 2 observer, patches, turtles, links ask link 2 [set color 78] asks link with who 2 to set it’s color to 78 observer, patches, turtles, links ask n-of 3 links [die] ask 3 random links to die observer, patches, turtles, links EXAMPLE CODE WHAT DOES THIS DO? WHO CAN DO THIS? ask turtles [set color red] sets turtle or link color turtles, links ask patches [set pcolor green] sets patch color patches (turtles can set pcolor) ask turtles [set size 10] sets turtle size turtles ask turtle 1 [set shape “person”] sets turtle shape turtles ask turtle 4 [set heading 90] sets turtle heading turtles ask turtle 3 [set heading heading + 20 ] increase/decrease turtle heading turtles ask links [set thickness 2] sets link thickness links ask turtle 3 [setxy 3 8] sets turtle location turtles ask turtle 0 [fd -2] turtle(s) moves forward or backward turtles ask turtle [if pcolor = green [show “hello!”]] asks turtle to speak turtles crt 3 [set size 2 set shape “cow”] asks observer to create 4 turtles observer ask patch 3 2 [sprout 2 [set size 3]] asks patches to make baby turtles patches ask turtle 0 [if pcolor = yellow [hatch 1]] asks turtles to make baby turtles turtles ask turtle [if pcolor = red [die] ] asks turtle to die turtles, links EXAMPLE CODE WHAT DOES THIS DO? if ticks > 10 [ask turtles [set pcolor green]] if the number of ticks is greater than 10, asks all turtles to turn green ask turtles [ifelse count turtles > 20 [fd 1][fd 2]] asks the turtles, if there are more than 20 turtles, move forward 1, otherwise 2 EXAMPLE CODE WHAT DOES THIS DO? set heading heading + random 30 this will set the turtle’s heading to it’s current heading + a random amount from 0 to 29 setxy random-xcor random-ycor this will set the turtle’s location to a random location if random 100 < 50 [ask turtle 1 [fd 1]] this will ask turtle 1 to move fd 1 half the time EXAMPLE CODE WHAT DOES THIS DO? ask patches [if any? turtles in-radius 5 [set pcolor red]] if there are any turtles in a radius of 5, patches turn red if count turtles > 20 [ask n-of 1 turtle [die]] if there are more than 20 turtles, then a random turtle will die ask turtles [if count turtles in-radius 4 > 20 [die]] if there are more than 20 turtles around the turtle, it will die BREEDS AND GLOBALS MUST BE ESTABLISHED AT THE VERY BEGINNING OF YOUR CODE - BEFORE THE SETUP FUNCTION ►BREEDS breed [singular plural] EXAMPLE CODE breed [goodguy goodguys] WHAT DOES THIS DO? creates a subtype of turtles called goodguy(s) Breeds create subtypes of turtles and links which you can assign different globals and call with functions independently of each other. In this example I have created the breed goodguy which I can then create by saying create-goodguys 4 [setxy random-xcor random-ycor set color yellow set size 3] and then I could have them do things like ask goodguys [set heading heading + random 20 – random 20 fd 1 if any? goodguys in-radius 2 [show “hello friend”]]. ►GLOBALS turtles-own [ ] patches-own [ ] links-own [ ] EXAMPLE CODE turtles-own [time-since-birth] WHAT DOES THIS DO? will give each turtle the characteristic “time-since-birth” In this example, you’d want to have any turtles created to [set time-since-birth 0] and somewhere in your go command ask turtles [set time-since-birth time-since-birth + 1] so that the value will increase with each tick. You could later use this variable by doing something like ask turtles [if time-since-birth > 65 [die]]. ►RANDOM USEFUL CODE FRAGMENTS ask badguy [set heading ( towards min-one-of goodguy [distance myself] ) fd 1] ask goodguy [ ifelse any? badguy in-radius 7 [set heading ( towards min-one-of goodguy [distance myself] – 180 ) fd 1.2 ][set heading heading + random 30 – random 30 fd 1 ] ask turtle 0 [create-link-with turtle 1 [set thickness 1 set color red]] ►EXAMPLE PROGRAM breed [goodguy] breed [badguy] to setup ca reset-ticks ask patches [set pcolor green] create-goodguy 5 [set shape "person" set size 2 set color blue setxy random-xcor random-ycor] create-badguy 2 [set shape "person" set size 3 set color red setxy random-xcor random-ycor] end to go ask badguy [set heading ( towards min-one-of goodguy [distance myself] ) fd 1] ask goodguy [ ifelse any? badguy in-radius 6 [set heading (towards min-one-of badguy [distance myself] - 180 ) fd .8] [set heading heading + random 30 - random 30 fd .6]] ask goodguy [if count goodguy in-radius 2 > 1 [hatch 1 [setxy random-xcor random-ycor]]] ask goodguy [if any? badguy in-radius 1 [die]] wait .3 tick end