Chapter 4: Paintball
In this set of exercises, you will program more complex behavior and interactions.
Learning Objectives:
|
New terms:
New blocks:
|
Try to write the code without 'peeking' at the sample code.
Read the description of each part and see if you can figure out how to put the blocks together. If you get stuck, put your mouse over the gray block to see the actual code. Test your program often to make sure that each chunk of code you add works before moving on to the next part.
Part 1: Create Turtle agents
Use what you've learned in previous chapters to build some code.
![]() |
Steps:
Check your code:
|
You can get the blank NetLogo template file here. Note that we need to create a separate breed called "target". This is because, in NetLogo, the breed "turtle" applies to all agents no matter the species. The Net Logo code for this would be: breed [targets target] to setup clear-all create-targets 50 [ set color brown set shape "box" set xcor (random 32) - 16 set ycor (random 32) - 16 ] |
Part 2: Create Player Breed and Player agent
The skills for this can be found in "Catching Flies".
![]() |
Steps:
|
The Net Logo code for this would be: breed [players player] breed [targets target] to setup clear-all create-targets 50 [ set color brown set shape "box" set xcor (random 32) - 16 set ycor (random 32) - 16 ] create-players 1 [ set color blue ] end |
![]() |
Check your code:
|
![]() |
Part 3: Program controls for the Player agent
Program the Player agent to move in response to holding down the arrow keys
![]() |
Steps:
|
In NetLogo, this takes 2 steps. First, make 4 buttons like the one below. Give them names and commands like "goForward", "goBack", "turnLeft", "turnRight", and assign action keys as appropriate. to goForward ask players [ forward 1 ] end to goBack ask players [ forward -1 ] end to turnLeft ask players [ left 10 ] end to turnRight ask players [ right 10 ] end NetLogo will fuss if the names of the commands (e.g. "goForward") on the buttons don't match the code. |
![]() |
Check your code.
|
![]() |
Part 4: Create the paintball agents
Create a Paintball breed and Program the Player agent to launch them.
![]() |
Steps:
|
You will need to create a button as above called "Shoot" with the command "shoot" and the action key "s". to shoot create-paintballs 1 [ move-to one-of players set heading [heading] of one-of players set shape "dot" set color one-of base-colors ] end The "move-to" and "set heading" are required in NetLogo to have the paintball start at the player and facing the same direction. In NetLogo, new agents are created at 0,0 with random headings. |
![]() |
Check your code:
|
![]() |
Part 5: Program the behavior of the paintball agents.
Write code so that the paintballs fly in straight lines and are deleted when they leave SpaceLand.
![]() |
Steps:
|
Add this code to the "to Forever" block: to forever ask paintballs [ forward 1 if xcor <= -15 [die] if ycor <= -15 [die] if xcor >= 15 [die] if ycor >= 15 [die] ] end Spaceland in NetLogo extends from -16 to 16 in the x direction and -16 to 16 in the y direction with 0,0 in the center. You can change this by clicking the "settings" button at the upper right of the controls in NetLogo. |
![]() |
Test your code: Be sure that your code can do all of these things (you should learn to push it hard and try to "break" it - better to find bugs early):
|
The complete code so far is: breed [players player] breed [paintballs paintball] breed [targets target] to setup clear-all create-targets 50 [ set color brown set shape "box" set xcor (random 32) - 16 set ycor (random 32) - 16 ] create-players 1 [ set color blue ] end to goForward ask players [ forward 1 ] end to goBack ask players [ forward -1 ] end to turnLeft ask players [ left 10 ] end to turnRight ask players [ right 10 ] end to shoot create-paintballs 1 [ move-to one-of players set heading [heading] of one-of players set shape "dot" set color one-of base-colors ] end to forever ask paintballs [ forward 1 if xcor <= -15 [die] if ycor <= -15 [die] if xcor >= 15 [die] if ycor >= 15 [die] end |
Part 6: Program the Collision between Paintball and Turtle
Write code so that, when a turtle is hit by a paintball, the paintball disappears and the turtle gets 'painted all over'.
![]() |
Steps:
|
In NetLogo, it is necessary to look at the collision from the point of view of the paintball, not the target. (This is because there isn't a "kill" command
in NetLogo, only "die") to forever ask paintballs [ forward 1 if xcor <= -15 [die] if ycor <= -15 [die] if xcor >= 15 [die] if ycor >= 15 [die] if any? targets in-radius 1 [ ask targets in-radius 1 [ set color [color] of myself ] die ] ] end |
![]() |
Test your code: You should be sure that your code does all of these things:
|
![]() You can download a working version of this project here. |
Part 7: Extensions
Get creative and see what you can do.
Here are some things to try:
Part 8: NetLogo Extensions
There are some cool things you can only do in NetLogo.
Here are some things to try:
Use comments to document your work. Anything on a line of code after two semicolons (";;") will be ignored by NetLogo but visible to you. You can use this to leave notes to yourself so you don't have to remember:
Comments can also be on lines by themselves. |
The comment below explains the 'weird xcor code' to setup clear-all create-targets 50 [ set color brown set shape "box" ;; this is the equivalent of "scatter" set xcor (random 32) - 16 set ycor (random 32) - 16 ] create-players 1 [ set color blue ] |
|
Use comments to make easy-to-undo changes to your code. There isn't a good 'undo' button in SLN, so if you wonder what happens when you remove a block of code, it can sometimes be a pain to put it back in the right place if you change your mind. In NetLogo, you can comment out code, see what happens, and then 'un-comment' it easily. |
Here, you can see what happens if you take out one of the 'weird xcor' lines in the setup code by adding 2 semicolons. Try it and see what happens. Just delete the semicolons to put it back to the way it was. to setup clear-all create-targets 50 [ set color brown set shape "box" ;; this is the equivalent of "scatter" set xcor (random 32) - 16 ;;set ycor (random 32) - 16 |
|
![]() |
Use the Command Center to test the effect of individual lines of code. If you look at the bottom of the "Interface" pane, you'll see "Command Center" and a line marked "observer>". Using this, you can execute individual commands to experiment with what they can do. For example, after clicking "setup", you could enter "ask players [set color red]" (without quotes). If you type that in the "observer>" box and hit return, you'll see the player turn red. The command you entered will then appear in the "Command Center". The commands you enter here will not become part of your code - you can see this by clicking 'setup' and you'll see that the player starts off blue. This can help you if you want to experiment with new code without editing your main code. You can also let the simulation run to a specific point and then enter a command to see what's going on right then. |
|
![]() |
Inspect and Alter Individual Agents. If you right-click or hold the control key while clicking any of the agents on the screen, a menu pops up. If you choose the name of the agent, another menu pops up and you can "inspect" that agent. In the window that pops up, you can:
This can be very useful, especially when you add custom traits to agents, in debugging your code and being able to see "what would happen if...". |
|