Commando Game Sample




Commando is a basic implementation of an arcade game using Denzo Mobile Game Engine.
Commando basically is a sprite that can explore an environment moving accordingly to user input and simulating a walk.

From previous presentation we need just 3 classes: the MIDlet, the Engine and a GraphicObject implementing Sprite that represent the commando men.

Commando MIDlet


package it.denzosoft.mobile.commando;

import it.denzosoft.mobile.common.util.Logger;
import it.denzosoft.mobile.graphicEngine.GameMIDlet;
import it.denzosoft.mobile.graphicEngine.LayeredCanvas;

public class CommandoMIDlet extends GameMIDlet {

public void startGame() {
try {
LayeredCanvas gameCanvas = new LayeredCanvas(this);
CommandoEngine ge = new CommandoEngine(gameCanvas);
setGameEngine(ge);
ge.startGame();
getDisplay().setCurrent(gameCanvas);
} catch (Exception ex) {
Logger.error(ex);

// Logger.showAlert(getDisplay(),Alert.FOREVER);
}
}


public String getName() {
return "Commando";
}
}


StartGame by default is called by MenuCanvas when user decides to start game.
We define the used GameCanvas and pass it to the new Engine
Then we run the engine and show the game canvas.
Really simple, isn’t it?
NB: getName is used by MenuCanvas to print the game name.

Commando Sprite


package it.denzosoft.mobile.commando;

import it.denzosoft.mobile.common.Rectangle;
import it.denzosoft.mobile.common.util.Logger;
import it.denzosoft.mobile.graphicEngine.GameEngine;
import it.denzosoft.mobile.graphicEngine.Sprite;
import it.denzosoft.mobile.graphicEngine.TiledGraphicObject;

public class Commando extends TiledGraphicObject implements Sprite {

public static final byte DIRECTION_N = 1;

public static final byte DIRECTION_NE = 3;

public static final byte DIRECTION_E = 2;

public static final byte DIRECTION_SE = 6;

public static final byte DIRECTION_S = 4;

public static final byte DIRECTION_SW = 12;

public static final byte DIRECTION_W = 8;

public static final byte DIRECTION_NW = 9;

public static final byte DIRECTION_STOPPED = 0;

public static final int MOVEMENT_MULTIPLIER = 2;

public static final int LINEAR_MOVEMENT = 1000 * MOVEMENT_MULTIPLIER;

public static final int DIAGONAL_MOVEMENT = 700 * MOVEMENT_MULTIPLIER;
private byte direction = DIRECTION_STOPPED;
private int currentBaseFrame = 0;

// Keep postions * 1000
private int posX = -1;
private int posY = -1;
private byte counterImage = 0;

/** Creates a new instance of Commando */
public Commando() {
}


public void execute(int userInput) {
Logger.debug("userInput=" + userInput);
direction = DIRECTION_STOPPED;

if ((userInput & GameEngine.DOWN_PRESSED) != 0) {
direction = (byte) (direction | DIRECTION_S);
}

if ((userInput & GameEngine.LEFT_PRESSED) != 0) {
direction = (byte) (direction | DIRECTION_W);
}

if ((userInput & GameEngine.RIGHT_PRESSED) != 0) {
direction = (byte) (direction | DIRECTION_E);
}

if ((userInput & GameEngine.UP_PRESSED) != 0) {
direction = (byte) (direction | DIRECTION_N);
}

if ((userInput & GameEngine.FIRE_PRESSED) != 0) {
// fire();
}

switch (direction) {
case DIRECTION_S:
posY = posY + LINEAR_MOVEMENT;
currentBaseFrame = 12;

break;

case DIRECTION_SE:
posY = posY + DIAGONAL_MOVEMENT;
posX = posX + DIAGONAL_MOVEMENT;
currentBaseFrame = 9;

break;

case DIRECTION_E:
posX = posX + LINEAR_MOVEMENT;
currentBaseFrame = 6;

break;

case DIRECTION_NE:
posY = posY - DIAGONAL_MOVEMENT;
posX = posX + DIAGONAL_MOVEMENT;
currentBaseFrame = 3;

break;

case DIRECTION_N:
posY = posY - LINEAR_MOVEMENT;
currentBaseFrame = 0;

// currentBaseFrame = 24;
break;

case DIRECTION_NW:
posY = posY - DIAGONAL_MOVEMENT;
posX = posX - DIAGONAL_MOVEMENT;
currentBaseFrame = 21;

break;

case DIRECTION_W:
posX = posX - LINEAR_MOVEMENT;
currentBaseFrame = 18;

break;

case DIRECTION_SW:
posY = posY + DIAGONAL_MOVEMENT;
posX = posX - DIAGONAL_MOVEMENT;
currentBaseFrame = 15;

break;
}

setX(posX / 1000);
setY(posY / 1000);

if (direction != DIRECTION_STOPPED) {
counterImage++;
}

if (counterImage > 8) {
counterImage = 0;
}

setFrame(currentBaseFrame + (counterImage / 3));
}


public void collisionDetection(Sprite sprite) {
}


public Rectangle getCollisionArea() {
return new Rectangle(0, 0, 32, 32);
}
}


The execute method translates the user input to sprite movement and tile.
SetFrame from TiledGraphicObject sets the frame to be printed.
As you can see if you keep your direction constant, the Sprite will change coherently the image emulating a walking man.
This is the tiled image representing the commando positions.
The tile set support 3 images for every direction.
The Commando object automatically moves from one to the next.

Commando Game Engine


package it.denzosoft.mobile.commando;

import it.denzosoft.mobile.common.util.Logger;
import it.denzosoft.mobile.graphicEngine.GameEngine;
import it.denzosoft.mobile.graphicEngine.LayeredCanvas;
import it.denzosoft.mobile.graphicEngine.TiledImage;
import it.denzosoft.mobile.iceland.EnergyBar;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;


public class CommandoEngine extends GameEngine {
/** DOCUMENT ME! */
LayeredCanvas canvas = null;
private Commando commando = null;
private EnergyBar energyBar = new EnergyBar();

/** Creates a new instance of CommandoEngine */
public CommandoEngine(LayeredCanvas canvas) {
super(canvas);
this.canvas = canvas;
init();
}

private void init() {
try {
commando = new Commando();

TiledImage spriteImg = new TiledImage("/guy.png", 32, 32);
commando.setImage(spriteImg);
commando.setVisible(true);
canvas.add(commando, LayeredCanvas.ZORDER_TOP);
energyBar.setWidth(this.getScreenWidth() / 2);
energyBar.setX(this.getScreenWidth() / 2);
energyBar.setHeight(10);
energyBar.setFull(100);
energyBar.setDanger(10);
energyBar.setWarning(25);
energyBar.setVisible(true);
canvas.add(energyBar, LayeredCanvas.ZORDER_TOPMOST);
energyBar.setEnergy(100);
} catch (Exception ex) {
Logger.error(ex);

// Logger.showAlert(canvas.getMIDlet().getDisplay(),1000);
}
}

public void gameTick() {
Logger.debug("##############");
Logger.memory();
commando.execute(getKeyStates());
}

public void commandAction(Command c, Displayable displayable) {
}
}


On engine initialization we load all images in a TiledImage,
Then we define a commando, we set it visible and put on a layeredCanvas.
Then we add also an energyBar object always on top.
The gameTick method updates commando passing it the user input.
The game is done. Run it!
Have a good luck.