NetLogo User Community Models
by Jim Lyons (Submitted: 01/06/2005)
NUKE SNAKE 1.0
It is a game of survival -- the one who doesn't die wins. Each player controls the constantly moving head of a growing trail. If you run into one of the trails, or into the walls or rocks, you die.
In Nuke Snake, you also have the ability to shoot. If you shoot the other snake's head, it dies and you win. You can also shoot your way through the trails, rocks, and walls, but be careful -- if anything blows up within one patch of your head, you die.
HOW TO PLAY
This version of Nuke Snake requires two players. (If you want a real challenge though, try steering both snakes, one with each hand, and see how long you can keep them from crashing into the rocks.) Notice that in order to use the keyboard controls for the buttons when starting up, or after using the Command Center or going to another Tab, you have to click in the panel outside of any buttons or text; you will see a difference in the buttons when the keyboard is enabled. (To change a keyboard control, edit its button in the Interface and change its Action Key field.)
The Trails? switch, the Sound? switch, and the speed control can be changed while playing. The Walls? switch, the Rocks? switch, and the num-rocks slider must be set before pressing the New Game button. Be sure to try all combinations of trails, rocks, and walls.
A match is a series of games that continues until one player reaches a predetermined number of wins, usually 10. The New Match button resets the Wins score for the two players.
The two snakes are called Snake Zero and Snake One to avoid using colors as the names (so they can be whatever colors you want) and also to remind you about numbering things starting with zero in NetLogo. There are comments in the Procedures to show you where to set the colors for the model if you want to change them.
HOW IT WORKS
The snakes (actually just the heads) are a breed of turtles. A snake's "body" -- the trail -- is really just colored patches showing where the head has been. In order to make the head look exactly like the rest of its body, the patch the head is on is stamped with its color. The turtle shape used for the snake's head is actually just the eyes.
Patch color is the key to operation of the game. If a patch is the background color, that means it is clear and the snakes can move there. If a patch is any other color, that means it is a trail, wall, or rock. The move command in the Snakes section of the Procedures just has to check the color of the patch-ahead 1. If it's the background color, go forward 1; if it's not, crash and die. The move script also checks for the special case of the snakes running into each other.
Bullets are another breed of turtles. To fire, a snake simply "hatches" a bullet with the same heading as itself and the main loop (see below) will make it "streak" along until it hits something. The streak command in the Bullets section of the Procedures just moves the bullet and checks the patch color. If it's the background color the bullet keeps going. If not, whatever was there is blown away and the neighboring patches are blasted. If a snake's head is hit or blasted, it dies.
The main loop of this game is the go command in the Observer section of the Procedures. It is called by a "forever" button in the Interface and is just three lines long. The first line checks to see if both snakes are still alive and if not, ends the game:
if count snakes < 2 [ ask snakes [ add-win celebrate ] stop ]
Notice that this covers the situations where both snakes die; the "ask snakes" just refers to the surviving snake, if any.
The other two lines make the bullets streak and the snakes move:
every 1 / (5 * speed) [ ask bullets [ streak ] ]
"speed" is the value of the slider in the Interface, which goes from 1 to 10. The bullets move five times as fast as the snakes and the snakes move every 1 to 1/10 second. The streak and move scripts take care of all collision detection. (The slider at the top of the Graphics area controls the speed of the NetLogo engine and should stay all the way to the right for best results.) Look up "every" in the Primitives Dictionary of the NetLogo manual for more information.
To create the visual effect when something is blasted, each patch has its own memory variable, mem, to store its color while the blast is shown. See the Patches section of the Procedures for the blast command. Also notice the other patch command, make-rock. When the Rocks? switch is on, the new-game command asks a number of patches, set by the num-rocks slider, to make the rocks.
The sound effects are written in the form of individual commands for convenience. This separates the details of creating the sounds from the logic of the procedures that use them, which has two benefits: It makes the logic easier to follow and it collects all the sound effects in one place so it is easier to experiment with them. Look at the Sound section of the NetLogo manual and try creating your own sound design for the game with the available drum and instrument sounds.
This NetLogo model is copyright 2005 by Jim Lyons. All rights reserved.
Permission to use, modify or redistribute this model is hereby granted, provided that this copyright notice is included. This model may not be redistributed for profit without permission of the author. Contact Jim Lyons, email@example.com.
(back to the NetLogo User Community Models)