128x160 Snake Xenzia Java Game -

private void spawnFood() // simple random do foodX = (int)(Math.random() * W); foodY = (int)(Math.random() * H); while(collidesWithSnake(foodX, foodY));

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;

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

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 paint(Graphics g) int cell = 8; int offsetX = (128 - W*cell)/2; int offsetY = (160 - H*cell)/2; // background g.setColor(0x000000); g.fillRect(0,0,128,160); // draw food g.setColor(0xFF0000); g.fillRect(offsetX+foodX*cell, offsetY+foodY*cell, cell-1, cell-1); // draw snake g.setColor(0x00FF00); for(int i=0; i<length; i++) g.fillRect(offsetX+snakeX[i]*cell, offsetY+snakeY[i]*cell, cell-1, cell-1); // score g.setColor(0xFFFFFF); g.drawString("Score: "+score, 5, 5, Graphics.TOP

g.setColor(0xFFFFFF); // head highlight g.fillRect(offsetX + x[0]*CELL_SIZE + 2, offsetY + y[0]*CELL_SIZE + 2, 4,4); g.setColor(0xFF0000); g.fillArc(offsetX + foodX*CELL_SIZE, offsetY + foodY*CELL_SIZE, CELL_SIZE, CELL_SIZE, 0, 360); Score Display Draw at top: Score: 0 using small font ( Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL ). 6. Input Handling Override keyPressed(int keyCode) in Canvas:

private void initGame() length = 3; snakeX[0] = W/2; snakeY[0] = H/2; snakeX[1] = W/2-1; snakeY[1] = H/2; snakeX[2] = W/2-2; snakeY[2] = H/2; direction = 1; // right nextDir = 1; score = 0; spawnFood(); gameState = 0; private void spawnFood() // simple random do foodX

// game data private static final int W = 15, H = 18; private int[] snakeX = new int[400]; private int[] snakeY = new int[400]; private int length, direction, nextDir; private int foodX, foodY; private int score;

public SnakeCanvas(MIDlet m) super(true); midlet = m; setFullScreenMode(true); initGame();

private boolean collidesWithSnake(int x, int y) for(int i=0; i<length; i++) if(snakeX[i]==x && snakeY[i]==y) return true; return false; foodY = (int)(Math.random() * H)

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) {} } }

public void start() thread = new Thread(this); running = true; thread.start();

} 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