128x160 Snake Xenzia Java Game Guide

private void updateGame()

public void run() { while(running) { long start = System.currentTimeMillis(); if(gameState == 0) updateGame(); repaint(); long delay = 150 - (System.currentTimeMillis()-start); if(delay < 5) delay = 5; try Thread.sleep(delay); catch(Exception e) {} } }

RecordStore rs = RecordStore.openRecordStore("HighScore", true); byte[] data = (score + "").getBytes(); rs.addRecord(data, 0, data.length); Play Tone Manager.playTone(ToneControl.C4, 100, 100); Vibrate Display.getDisplay(midlet).vibrate(200); 128x160 snake xenzia java game

public void pauseApp() {} public void destroyApp(boolean unconditional) canvas.stop();

} import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.GameCanvas; public class SnakeCanvas extends GameCanvas implements Runnable { private MIDlet midlet; private Thread thread; private boolean running; private int gameState; // 0=run,1=pause,2=gameover private void updateGame() public void run() { while(running)

g.setColor(0x00FF00); // green body for(int i=0; i<length; i++) g.fillRect(offsetX + x[i]*CELL_SIZE, offsetY + y[i]*CELL_SIZE, CELL_SIZE-1, CELL_SIZE-1);

int action = getGameAction(keyCode); switch(action) case UP: if(direction != DOWN) nextDir = UP; break; case DOWN: if(direction != UP) nextDir = DOWN; break; case LEFT: if(direction != RIGHT) nextDir = LEFT; break; case RIGHT: if(direction != LEFT) nextDir = RIGHT; break; case FIRE: if(gameState == RUNNING) gameState = PAUSED; else if(gameState == PAUSED) gameState = RUNNING; break; if(gameState == 0) updateGame()

protected void keyPressed(int keyCode) int key = getGameAction(keyCode); if(key == UP && direction != 2) nextDir = 0; else if(key == DOWN && direction != 0) nextDir = 2; else if(key == LEFT && direction != 1) nextDir = 3; else if(key == RIGHT && direction != 3) nextDir = 1; else if(key == FIRE && gameState == 0) gameState = 1; else if(key == FIRE && gameState == 1) gameState = 0; else if(keyCode == KEY_NUM9 && gameState == 2) initGame();