Class Caterpillar

java.lang.Object
  extended by Caterpillar

public class Caterpillar
extends java.lang.Object

Caterpillars, for use in the Munch Game

See Also:
MunchGame

Constructor Summary
Caterpillar(java.awt.Color c, java.awt.Point sp, int len, int loc)
          Construct a new caterpillar with the given color and starting position.
 
Method Summary
 int getScore()
          Get the caterpillar's score.
 void move(MunchGame game)
          Move the caterpillar one step.
 boolean onPosition(java.awt.Point newPos)
          Determine whether any part of the caterpillar's body lies on the given position.
 void paint(java.awt.Graphics g)
          Draw this caterpillar on the given graphics pane and draw its score in the appropriate location.
 void setDirection(char d)
          Set the direction of the head of the caterpillar.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Caterpillar

public Caterpillar(java.awt.Color c,
                   java.awt.Point sp,
                   int len,
                   int loc)
Construct a new caterpillar with the given color and starting position.

Parameters:
c - this caterpillar's color
sp - this caterpillar's starting position (a point on the grid)
len - the caterpillar's length (number of body segments)
loc - the x coordinate of this caterpillar's score
Method Detail

setDirection

public void setDirection(char d)
Set the direction of the head of the caterpillar.

Parameters:
d - the direction (N, S, E, or W) or pause command (Z)

getScore

public int getScore()
Get the caterpillar's score.

Returns:
the current score

move

public void move(MunchGame game)
Move the caterpillar one step.

Parameters:
game - the current state of the caterpillar game

onPosition

public boolean onPosition(java.awt.Point newPos)
Determine whether any part of the caterpillar's body lies on the given position.

Parameters:
newPos - the position to test
Returns:
true if any part of the caterpillar's body lies on newPos

paint

public void paint(java.awt.Graphics g)
Draw this caterpillar on the given graphics pane and draw its score in the appropriate location.

Parameters:
g - the graphics pane on which to draw the caterpillar