example.wormgame
Class WormPit

java.lang.Object
  extended by javax.microedition.lcdui.Displayable
      extended by javax.microedition.lcdui.Canvas
          extended by example.wormgame.WormPit
All Implemented Interfaces:
java.lang.Runnable

public class WormPit
extends javax.microedition.lcdui.Canvas
implements java.lang.Runnable

The WormPit contains a Worm and some WormFood. The Worm will slither around the pit in search of WormFood. If the Worm eats it will grow in length. The Worm will be killed if it attempts to slither past the edge of the pit or if it eats itself.


Field Summary
static int CELL_SIZE
          Size of a cell for worm rendering.
(package private) static int CellHeight
          Current screen height in cells.
(package private) static int CellWidth
          Current screen size in cells.
(package private) static int DRAW_COLOUR
          Color for drawing worm cells. (0x000000)
(package private) static int ERASE_COLOUR
          Color for erasing worm cells. (0xffffff)
(package private) static int FOOD_COLOUR
          Color for target food object. (0x00ff00)
(package private) static byte MAX_LEVELS
          Maximum number of difficulty levels. (10)
(package private) static int TEXT_COLOUR
          Color for text objects. (0xff0000)
 
Fields inherited from class javax.microedition.lcdui.Canvas
DOWN, FIRE, GAME_A, GAME_B, GAME_C, GAME_D, KEY_NUM0, KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9, KEY_POUND, KEY_STAR, LEFT, RIGHT, UP
 
Constructor Summary
WormPit()
          Default constructor for worm pit.
 
Method Summary
 void createAudioPlayer()
          Create an audio player.
 void destroyAudioPlayer()
          Destroy the audio player.
 void destroyGame()
          Destroy the entire game.
 int getLevel()
          Get the difficulty level.
 int getScore()
          Get the score of this game.
protected  void hideNotify()
          Notification handler when canvas hidden.
(package private) static boolean isInBounds(int x, int y)
          Returns true if the given point is within the bounds of the worm pit.
 void keyPressed(int keyCode)
          Handle keyboard input.
 void mute()
          if audio is on - mute
 void paint(javax.microedition.lcdui.Graphics g)
          Paint the worm pit and its components
(package private)  void restart()
          Restart the game.
 void run()
          The main execution loop.
 void setDestroyed(boolean destroyGame)
          Set Destroyed state in order to undestroy game after pause.
 void setLevel(int level)
          Set the difficulty level.
 void unMute()
          if audio is stopped - restart it
 
Methods inherited from class javax.microedition.lcdui.Canvas
getGameAction, getKeyCode, getKeyName, hasPointerEvents, hasPointerMotionEvents, hasRepeatEvents, isDoubleBuffered, keyReleased, keyRepeated, pointerDragged, pointerPressed, pointerReleased, repaint, repaint, serviceRepaints, setFullScreenMode, showNotify, sizeChanged
 
Methods inherited from class javax.microedition.lcdui.Displayable
addCommand, getHeight, getTicker, getTitle, getWidth, isShown, removeCommand, setCommandListener, setTicker, setTitle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CellWidth

static int CellWidth
Current screen size in cells.


CellHeight

static int CellHeight
Current screen height in cells.


MAX_LEVELS

static final byte MAX_LEVELS
Maximum number of difficulty levels. (10)

See Also:
Constant Field Values

FOOD_COLOUR

static final int FOOD_COLOUR
Color for target food object. (0x00ff00)

See Also:
Constant Field Values

TEXT_COLOUR

static final int TEXT_COLOUR
Color for text objects. (0xff0000)

See Also:
Constant Field Values

ERASE_COLOUR

static final int ERASE_COLOUR
Color for erasing worm cells. (0xffffff)

See Also:
Constant Field Values

DRAW_COLOUR

static final int DRAW_COLOUR
Color for drawing worm cells. (0x000000)

See Also:
Constant Field Values

CELL_SIZE

public static final int CELL_SIZE
Size of a cell for worm rendering.

See Also:
Constant Field Values
Constructor Detail

WormPit

public WormPit()
Default constructor for worm pit. Initialized the food and worm objects.

Method Detail

setLevel

public void setLevel(int level)
Set the difficulty level. If the new level is different from the current level, any game currently in progress is terminated and the score is lost.

Parameters:
level - value of new level requested
See Also:
getLevel()

getLevel

public int getLevel()
Get the difficulty level.

Returns:
current game level
See Also:
setLevel(int)

getScore

public int getScore()
Get the score of this game.

Returns:
current score

isInBounds

static boolean isInBounds(int x,
                          int y)
Returns true if the given point is within the bounds of the worm pit.

Parameters:
x - x coordinate of point to check (0 - CellWidth)
y - y coordinate of point to check (0 - CellHeight)
Returns:
true, if coordinate is in the worm pit

restart

void restart()
Restart the game.


keyPressed

public void keyPressed(int keyCode)
Handle keyboard input. This is used for worm movement.

Overrides:
keyPressed in class javax.microedition.lcdui.Canvas
Parameters:
keyCode - pressed key is either Canvas arrow key (UP, DOWN, LEFT, RIGHT) or simulated with KEY_NUM (2, 8, 4,6).

paint

public void paint(javax.microedition.lcdui.Graphics g)
Paint the worm pit and its components

Specified by:
paint in class javax.microedition.lcdui.Canvas
Parameters:
g - graphics object to be rendered

hideNotify

protected void hideNotify()
Notification handler when canvas hidden. Forces a redraw when canvas is shown again. Signals that the game is paused while the canvas is obscured.

Overrides:
hideNotify in class javax.microedition.lcdui.Canvas

run

public void run()
The main execution loop.

Specified by:
run in interface java.lang.Runnable

destroyGame

public void destroyGame()
Destroy the entire game. Call this prior to destroying the MIDlet.


setDestroyed

public void setDestroyed(boolean destroyGame)
Set Destroyed state in order to undestroy game after pause.


destroyAudioPlayer

public void destroyAudioPlayer()
Destroy the audio player. Call this destroy the audio player.


createAudioPlayer

public void createAudioPlayer()
Create an audio player.


mute

public void mute()
if audio is on - mute


unMute

public void unMute()
if audio is stopped - restart it