Procedures & Breeds

Once you have the turtles moving, you are ready to create more elaborate code that you might want to save and tweak. Not only that, but you can create many different types of "turtles" that all behave in unique ways. The first thing you are going to want to be able to do is to write code that you can easily modify. Creating procedures is a great way to do this. Creating breeds is the way to use many different "turtle" shapes that you can move independently.

Procedures

Defining a procedure is a great way to identify a set of code that you may want to repeat or update throughout your programming. Each procedure has a name, preceded by the keyword to. The keyword end marks the end of the procedure. Once you define a procedure, you can use or update it anytime in your program.

Breeds

NetLogo allows you to define different "breeds" of turtles. Once you define the breeds you want, you can then make each breed behave differently. For instance, in the example below, we are setting up the breeds of cats and mice and then giving each breed different physical characteristics as well as different moves.

You define turtle breeds using the breed keyword, at the top of the Procedures tab, before any procedures. You always specify the plural and then the singular name for the breed. Once you have breeds setup, you can use the ask command to give each breed different physical characteristics and you can use the if command to specify which breed should take an action.

Sample Procedures

  • Copy and paste the following code into the Procedures tab (starting with the line breeds [cats cat])
  • Before this code will work, you will need to create shapes called cat and mouse. Select Tools -> Turtle Shapes Editor -> New and create those shapes.
  • Create a button for each procedure: setup and meow. Select the Interface tab, click +Add and then click anywhere in the white space underneath. The button dialog box will appear. Type the procedure name in the Commands dialog box. Note that the setup procedure uses Observer agents and the meow procedure uses Turtles agent.

breed [cats cat]
breed [mice mouse]

to setup
ca
create-cats 1
ask cats [ set color white ]
ask cats [ set shape "cat" ]
ask cats [ set size 3 ]
create-mice 30
ask mice [ set color brown ]
ask mice [ set shape "mouse" ]
end

to meow
if breed = cats [ fd 5 ]
if breed = mice [fd 10]
if breed = cats [ set label "meow!" ]
if breed = mice [set label "eek!"]
end

Challenges:
  • Randomize the number of cats that created at setup. Replace the create number with the command random 10. Run Setup a few times. What do you notice