128x160 Snake Xenzia Java Game 🔥 Best Pick
private void updateGame() newY<0
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) {} } }
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(); 128x160 snake xenzia java game
} 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
public void stop() running = false;
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;
// 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 updateGame() newY<0 public void run() {
public void pauseApp() {} public void destroyApp(boolean unconditional) canvas.stop();
private void spawnFood() // simple random do foodX = (int)(Math.random() * W); foodY = (int)(Math.random() * H); while(collidesWithSnake(foodX, foodY)); private void updateGame() newY<
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(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);