example.wormgame
Class Worm

java.lang.Object
  extended by example.wormgame.Worm

public class Worm
extends java.lang.Object

A Worm is a combination of one or more WormLinks. The first WormLink is the physical tail of the worm. The last WormLink is the physical head of the worm. As the worm moves, it adds one space to the head and then removes one from the tail. If the worm eats a piece of food, the head adds one space but the tail will not remove one. In this manner, the worm will grow outward from its head.


Field Summary
static byte DOWN
           
static byte LEFT
           
static byte RIGHT
           
static byte UP
           
 
Constructor Summary
Worm(WormPit pit)
           
 
Method Summary
 boolean contains(int x, int y)
          Returns true if the worm contains the given cell coordinate.
 void drawLink(javax.microedition.lcdui.Graphics g, int x1, int y1, int x2, int y2, int len)
          Draw a worm link.
 void eat()
          Signals the worm to eat a food item
 int getX()
          Get the X coordinate of the cell that contains the worm's head.
 int getY()
          Get the Y coordinate of the cell that contains the worm's head.
 void moveOnUpdate()
          Schedule the worm for growth.
 void paint(javax.microedition.lcdui.Graphics g)
          Draw the entire worm.
 void regenerate()
          Regenerate the worm in its initial position.
 void setDirection(byte direction)
          Change the direction of the worm.
 void update(javax.microedition.lcdui.Graphics g)
          Update the worm.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DOWN

public static final byte DOWN
See Also:
Constant Field Values

LEFT

public static final byte LEFT
See Also:
Constant Field Values

RIGHT

public static final byte RIGHT
See Also:
Constant Field Values

UP

public static final byte UP
See Also:
Constant Field Values
Constructor Detail

Worm

public Worm(WormPit pit)
Method Detail

regenerate

public void regenerate()
Regenerate the worm in its initial position. This is used to restart the game after the worm is killed.


setDirection

public void setDirection(byte direction)
Change the direction of the worm. It will only change directions if you set a direction that is 90 degrees different from the current direction of the worm.


moveOnUpdate

public void moveOnUpdate()
Schedule the worm for growth. The next time the update() method is invoked, the worm will move forward by one cell.


update

public void update(javax.microedition.lcdui.Graphics g)
            throws WormException
Update the worm. If the worm is scheduled to grow, this method will grow the worm head by one cell and potentially shrink the tail by one cell. If the worm has eaten a piece of food, the tail will not shrink. If the worm is not scheduled to grow, this method will do nothing.

Throws:
WormException

drawLink

public void drawLink(javax.microedition.lcdui.Graphics g,
                     int x1,
                     int y1,
                     int x2,
                     int y2,
                     int len)
Draw a worm link.


paint

public void paint(javax.microedition.lcdui.Graphics g)
Draw the entire worm. Logically, the worm is composed of one or more links. A worm is usually composed of a few straight segments. This allows us to paint a full worm with a minimum of draw calls.


eat

public void eat()
Signals the worm to eat a food item


getX

public int getX()
Get the X coordinate of the cell that contains the worm's head.


getY

public int getY()
Get the Y coordinate of the cell that contains the worm's head.


contains

public boolean contains(int x,
                        int y)
Returns true if the worm contains the given cell coordinate.