FileDocCategorySizeDatePackage
WormFood.javaAPI DocJ2ME MIDP 2.02811Thu Nov 07 12:02:18 GMT 2002example.wormgame

WormFood

public class WormFood extends Object
WormFood is a food item that the worm can eat. It is placed at a pseudo randomly generated location in the pit.

Fields Summary
private int
cellX
Current food location x coordinate in cells.
private int
cellY
Current food location y coordinate in cells.
private int
x
Current food location x coordinate in pixels.
private int
y
Current food location y coordinate in pixels.
private long
arenaSize
Size of arena in cells.
Constructors Summary
public WormFood(WormPit pit)
Constructor for worm food object.

param
pit pit to which the food is associated. (unused currently)

	arenaSize = WormPit.CellWidth * WormPit.CellHeight;
	regenerate();
    
Methods Summary
public intgetX()
Get the X coordinate of the cell that contains the food item.

return
x coordinate of food cell location

	return cellX;
    
public intgetY()
Get the Y coordinate of the cell that contains the food item.

return
y coordinate of food cell location

	return cellY;
    
public booleanisAt(int cx, int cy)
Returns true if the food item is at the given cell.

param
cx x coordinate of cell to test
param
cy y coordinate of cell to test
return
true, if the food is at the cell coordinate

	return ((this.cellX == cx) && (this.cellY == cy));
    
public voidpaint(javax.microedition.lcdui.Graphics g)
Paint the piece of food.

param
g graphics object to receive rendering of food object

	g.setColor(WormPit.FOOD_COLOUR);
	g.fillRect(x+1, y+1, WormPit.CELL_SIZE-2, WormPit.CELL_SIZE-2);
	g.setColor(WormPit.DRAW_COLOUR);
	g.drawRect(x, y, WormPit.CELL_SIZE-1, WormPit.CELL_SIZE-1);
    
public voidregenerate()
Regenerate the food item. Whenever the worm eats a piece of food, this method is called.

	int loc = (int)(System.currentTimeMillis() % arenaSize);
	cellY = (int)(loc / WormPit.CellWidth);
	cellX = (int)(loc % WormPit.CellHeight);
	y = cellY * WormPit.CELL_SIZE;      // Cache to save time during paint
	x = cellX * WormPit.CELL_SIZE;      // Cache to save time during paint