Missile Command

Missile Command is a classic arcade game. Inter-Continental Ballistic Missiles ( ICBMs) rain down from the sky while the player frantically launces Anti-Ballistic Missiles (ABMs) that streak up to their target and detonate, hopefully taking out a bunch of ICBMs with them. When the ICBMs have completely destroyed the player's territory, the game ends.

Here's a screen shot of the NetLogo version of Missile Command (MC):

Although we can start with the Eco-0 framework, some modifications will need to be made.

To begin, we define two breeds of turtles:

breed [icbms icbm]
breed [abms abm]

The breed declaration requires us to specify the plural and singular names of the breed. NetLogo automatically defines analogues of most turtle procedures for each new breed. For example:

abms-own [target-xcor target-ycor]
ask-abms [update-abm]
create-abms 1 [set target-xcor tx set target-ycor ty]
if is-icbm? self [set score score + 1 die]

The init-patch procedure colors all patches near the bottom of the screen green. This represents the player's territory. As ICBMs reach the bottom of the screen and explode, green patches in the blast radius flash red then turn black (add a beep for sound effects). MC keeps track of the number of green patches remaining, when this number hits zero the game ends.

A player launches an ABM by clicking the mouse on any patch. This creates an ABM at the bottom center of the screen. The ABM remembers the mouse coordinates. This is its target. As it is updated, it moves toward its target. When it gets reasonably close to the target (it may not get to the exact point) it explodes, destroying everything within its blast radius. NetLogo provides a handy mouse-down? reporter. If the mouse button happens to be down when this procedure is called, the mouse coordinates are stored in the pre-defined mouse-xcor ad mouse-ycor variables. Of course we must call this procedure frequently in order to make MC responsive to player input.

Here is the complete model-update procedure:

to update-model
  if count patches with [pcolor = green] = 0 [ct stop] ; game over
  if mouse-down? [launch-abm mouse-xcor mouse-ycor]
  ask abms [update-abm]
  if mouse-down? [launch-abm mouse-xcor mouse-ycor]
  ask icbms [update-icbm]
  if mouse-down? [launch-abm mouse-xcor mouse-ycor]
  if count icbms < max-icbms [launch-icbm]
  if mouse-down? [launch-abm mouse-xcor mouse-ycor]
  tick
end

ICBMs are launched from random locations along the top of the screen and move relentlessly downward to the corresponding point on the bottom of the screen. If they arrive, they explode, destroying all greenery within their blast radius. If there are too many ICBMs, or if they are too fast, the game becomes too hard.

Hint: Turtle motion is smoother if they move fractional distances. You can also slow ICBMs by having them move only some small percentage of the time they are updated:

if random 100 < 1 [fd .1 * ICBM-speed]