Previous: Movement (part 2 of 6)
Launching Paintballs
Let's program Mario to launch paintballs. We'll create a new procedure called Launch in the Mario section of the canvas.
This procedure will "hatch" a new agent. Hatch creates an exact copy of an agent. But it also allows us to do some things to that newly created agent. In this case we want to create a small sphere and set it going. Grab a Hatch-Do block from the Logic tab and attach it to the Launch procedure.
Inside of the Do section of the Hatch block, we want to have that new agent move forward a little, get a lot smaller, move off the ground, change its color to a random color and change its breed to "Paint". Drag out the Forward block from the Movement tab, and the Set Size, Set Altitude, Set Breed, and Set Color blocks from the Traits drawer.
Set the agent size .2 and set agent altitude to 1. Color is a little tricky. There are 140 whole number colors in StarLogo TNG (there can be fractional numbers as well). The "main" colors in StarLogo are all of those that end in 5 (5, 15, 25, 35, etc.). These are all of the named colors. To create agents of only those colors you take a random multiple of 10 and add 5 to it. The blocks here show how to do that. Finally, find the Paint breed block in the Paint tab of the My Blocks palette.
Now we have a procedure to launch paintballs. To program Mario to run the procedure, add another keyboard control at the end of the Move procedure that has the "a" key call this launch procedure. This way when we press the "a" key a new paintball will launch. The Launch procedure call block is found in the Mario tab of the My Blocks palette.
If you try to run your procedure now, you'll find that it doesn't do much. It creates a new sphere but the sphere doesn't go anywhere. In order to get it moving we need to tell the Paint breed how to move. One way to do this is to tell them to move forward, but add a little bit of randomness. The block stack below shows a new procedure called Projectile Move that runs such a random walk. Put this procedure in the Paint section of the canvas.
Then go to the Paint tab (My Blocks palette) and pull out a Projectile Move call procedure block and attach it in the Paint section of the Start Game (forever) block.
Run your game and shoot some paintballs. But they're just passing through the turtles! We'll program some interactions between the paintballs and the turtles next.
| Next: Collisions (part 4starlogo-tng/ of 6) | Back To Top |