128x160 Snake Xenzia Java Game Apr 2026
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;
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();
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) {} } } 128x160 snake xenzia java game
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; private boolean collidesWithSnake(int x, int y) for(int i=0;
// 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;
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 boolean collidesWithSnake(int x
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);
public SnakeCanvas(MIDlet m) super(true); midlet = m; setFullScreenMode(true); initGame();