Previous: Motion (part 3 of 8)
Infection
We will make infection happen when Agents collide with each other. Go to the Collisions area of the Canvas.
Open the Turtle's tab from the My Blocks palette and drag out the Collision block that is between Turtles and Turtles. Collision blocks get automatically run when Agents collide into each other.
We will use an If block to test if the collidee turtle is red. If so, then the turtle agent will turn its own color to red, to show that it's been infected by the turtle that it collided with. Connect an If block (from the Logic tab) to the top Turtles section of the Collision block.
Grab an = block (from the Math tab) and attach it to the Test port on the If block.
On the left side of the = attach a Color-Of block from the Other Agents tab..
Then attach a Collidee block (from the Other Agents tab) to the Color-Of block . This reports the color of the Agent who was just collided into.
We want to see if this is red, so grab a Red block from the Colors palette and attach it to the right side of the = block.
Put a Set Color Red block in the Then part of the If block.
Collision blocks only get run once for each pair of collisions. So the top and bottom of the collision should be the same in this case, in order to make sure to check both cases whether either one is infected. It is easy to copy the set of blocks you just created to the bottom portion by clicking on the If block, press Ctrl-C (or Command-C in Macs) to copy, and press Ctrl-V (or Command-V) in Macs) to paste it. Then attach the new copy to the bottom Turtles section of the Collision block.
Run your model by clicking on the Run button in the Runtime window. You should see the infection spreading as more turtles turn red.
| Next: Part 5 of 8 - Recovery | Back To Top |
