~cpp
import java.lang.*;
import java.io.*;
import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
class SplashCanvas extends Canvas {
public void paint(Graphics g) {
g.setColor(0xFFFFFF);
g.fillRect(0, 0, getWidth(), getHeight());
try {
Image splashImage = Image.createImage("/Splash.png");
g.drawImage(splashImage, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
} catch(IOException e) {
e.printStackTrace();
}
}
};
class SnakeCell {
public int x;
public int y;
public SnakeCell(int x, int y) {
this.x = x;
this.y = y;
}
};
class Snake {
public static final int LEFT = 1;
public static final int RIGHT = 2;
public static final int UP = 3;
public static final int DOWN = 4;
private final int xRange;
private final int yRange;
private Vector cellVector;
private int headIndex;
private int direction;
private boolean growing;
public Snake(int length, int xRange, int yRange) {
this.xRange = xRange;
this.yRange = yRange;
cellVector = new Vector();
for(int i = length ; i >= 1 ; i--)
cellVector.addElement(new SnakeCell(i, 1));
headIndex = 0;
direction = Snake.RIGHT;
growing = false;
}
public int length() {
return cellVector.size();
}
public SnakeCell getSnakeCell(int index) {
return (SnakeCell)cellVector.elementAt((headIndex + index) % length());
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public boolean checkGameOver() {
SnakeCell head;
SnakeCell cell;
head = getSnakeCell(0);
if(head.x < 0 || head.x > xRange - 1
|| head.y < 0 || head.y > yRange - 1)
return false;
int length = length();
for(int i = 1 ; i < length ; i++) {
cell = getSnakeCell(i);
if(head.x == cell.x && head.y == cell.y)
return false;
}
return true;
}
public boolean go() {
SnakeCell prevHead = getSnakeCell(0);
SnakeCell currentHead;
if(growing) {
currentHead = new SnakeCell(prevHead.x, prevHead.y);
cellVector.insertElementAt(currentHead, headIndex);
growing = false;
}
else {
headIndex = (headIndex + length() - 1) % length();
currentHead = getSnakeCell(0);
currentHead.x = prevHead.x;
currentHead.y = prevHead.y;
}
if(direction == Snake.LEFT)
currentHead.x--;
else if(direction == Snake.RIGHT)
currentHead.x++;
else if(direction == Snake.UP)
currentHead.y--;
else if(direction == Snake.DOWN)
currentHead.y++;
return checkGameOver();
}
public void grow() {
growing = true;
}
};
class SnakeBiteCanvas extends Canvas implements Runnable {
private final int boardWallWidth = 4;
private final int snakeCellWidth = 4;
private final int canvasWidth;
private final int canvasHeight;
private final int boardX;
private final int boardY;
private final int boardWidth;
private final int boardHeight;
private final int boardInnerX;
private final int boardInnerY;
private final int boardInnerWidth;
private final int boardInnerHeight;
private final int snakeXRange;
private final int snakeYRange;
private Snake snake;
private int appleX;
private int appleY;
private int level;
private boolean pause;
private boolean drawBoard;
private boolean printLevel;
private boolean gameOver;
private int delayTime;
public Command pauseCommand;
public Command restartCommand;
public SnakeBiteCanvas() {
canvasWidth = getWidth();
canvasHeight = getHeight();
boardWidth = canvasWidth - 6 - (canvasWidth - 6 - boardWallWidth * 2) % snakeCellWidth;
boardHeight = boardWidth / 10 * 8 - (boardWidth / 10 * 8 - boardWallWidth * 2) % snakeCellWidth;
boardX = (canvasWidth - boardWidth) / 2;
boardY = (canvasHeight - boardHeight) /2;
boardInnerX = boardX + boardWallWidth;
boardInnerY = boardY + boardWallWidth;
boardInnerWidth = boardWidth - boardWallWidth * 2;
boardInnerHeight = boardHeight - boardWallWidth * 2;
snakeXRange = boardInnerWidth / snakeCellWidth;
snakeYRange = boardInnerHeight / snakeCellWidth;
newGame();
}
public void newGame() {
snake = new Snake(5, snakeXRange, snakeYRange);
newApple();
level = 1;
pause = true;
drawBoard = true;
printLevel = true;
gameOver = false;
delayTime = 200;
new Thread(this).start();
}
public void newApple() {
Random random = new Random();
int length = snake.length();
SnakeCell cell;
boolean ok;
for(;;) {
appleX = Math.abs(random.nextInt()) % snakeXRange;
appleY = Math.abs(random.nextInt()) % snakeYRange;
ok = true;
for(int i = 0 ; i < length ; i++) {
cell = snake.getSnakeCell(i);
if(appleX == cell.x && appleY == cell.y) {
ok = false;
break;
}
}
if(ok)
return;
}
}
public void drawBoard(Graphics g) {
g.setColor(0xFFFFFF);
g.fillRect(0, 0, canvasWidth, canvasHeight);
g.setColor(0x000000);
g.fillRect(boardX, boardY, boardWidth, boardHeight);
}
public void clearBoard(Graphics g) {
g.setColor(0xFFFFFF);
g.fillRect(boardInnerX - 1, boardInnerY - 1, boardInnerWidth + 2, boardInnerHeight + 2);
}
public void drawCell(Graphics g, int x, int y) {
g.fillRect(boardInnerX + snakeCellWidth * x,
boardInnerY + snakeCellWidth * y,
snakeCellWidth, snakeCellWidth);
}
public void paint(Graphics g) {
if(drawBoard) {
drawBoard(g);
drawBoard = false;
}
clearBoard(g);
g.setColor(0x000000);
int length = snake.length();
SnakeCell cell;
for(int i = 0; i < length ; i++) {
cell = snake.getSnakeCell(i);
drawCell(g, cell.x, cell.y);
}
drawCell(g, appleX, appleY);
if(printLevel) {
g.setColor(0xFFFFFF);
g.fillRect(0, 0, canvasWidth, boardY);
g.setColor(0x000000);
g.drawString("Level : " + level, canvasWidth / 2, 0, Graphics.HCENTER | Graphics.TOP);
printLevel = false;
}
if(gameOver) {
g.drawString("Game Over!!", canvasWidth / 2, canvasHeight, Graphics.HCENTER | Graphics.BOTTOM);
}
}
public void keyPressed(int keyCode) {
if(!pause && !gameOver) {
int gameAction = getGameAction(keyCode);
int direction = snake.getDirection();
if(gameAction == Canvas.LEFT && direction != Snake.RIGHT)
snake.setDirection(Snake.LEFT);
else if(gameAction == Canvas.RIGHT && direction != Snake.LEFT)
snake.setDirection(Snake.RIGHT);
else if(gameAction == Canvas.UP && direction != Snake.DOWN)
snake.setDirection(Snake.UP);
else if(gameAction == Canvas.DOWN && direction != Snake.UP)
snake.setDirection(Snake.DOWN);
}
}
public void start() {
pause = false;
}
public void pause() {
pause = true;
}
public void restart() {
newGame();
repaint();
}
public void run() {
SnakeCell head;
try {
for(;;) {
if(!pause) {
if(snake.go() == false) {
gameOver = true;
repaint();
this.removeCommand(pauseCommand);
this.addCommand(restartCommand);
return;
}
head = snake.getSnakeCell(0);
if(head.x == appleX && head.y == appleY) {
level++;
printLevel = true;
delayTime-=3;
snake.grow();
newApple();
}
repaint();
Thread.sleep(delayTime);
}
}
} catch(InterruptedException e) {
e.printStackTrace();
}
}
};
public class SnakeBite extends MIDlet implements CommandListener {
private Display display;
private SplashCanvas splashCanvas;
private SnakeBiteCanvas snakeBiteCanvas;
private Command continueCommand;
private Command startCommand;
private Command pauseCommand;
private Command restartCommand;
private Command exitCommand;
public SnakeBite() {
display = Display.getDisplay(this);
splashCanvas = new SplashCanvas();
snakeBiteCanvas = new SnakeBiteCanvas();
continueCommand = new Command("Continue", Command.SCREEN, 1);
startCommand = new Command("Start", Command.SCREEN, 1);
pauseCommand = new Command("Pause", Command.SCREEN, 1);
restartCommand = new Command("Restart", Command.SCREEN, 1);
exitCommand = new Command("Exit", Command.EXIT, 1);
splashCanvas.addCommand(continueCommand);
splashCanvas.addCommand(exitCommand);
splashCanvas.setCommandListener(this);
snakeBiteCanvas.pauseCommand = pauseCommand;
snakeBiteCanvas.restartCommand = restartCommand;
snakeBiteCanvas.addCommand(startCommand);
snakeBiteCanvas.addCommand(exitCommand);
snakeBiteCanvas.setCommandListener(this);
}
public void startApp() {
display.setCurrent(splashCanvas);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
display = null;
snakeBiteCanvas = null;
startCommand = null;
pauseCommand = null;
restartCommand = null;
exitCommand = null;
}
public void commandAction(Command c, Displayable d) {
if(c == continueCommand) {
display.setCurrent(snakeBiteCanvas);
}
else if(c == startCommand) {
snakeBiteCanvas.removeCommand(startCommand);
snakeBiteCanvas.addCommand(pauseCommand);
snakeBiteCanvas.start();
}
else if(c == pauseCommand) {
snakeBiteCanvas.removeCommand(pauseCommand);
snakeBiteCanvas.addCommand(startCommand);
snakeBiteCanvas.pause();
}
else if(c == restartCommand) {
snakeBiteCanvas.removeCommand(restartCommand);
snakeBiteCanvas.addCommand(startCommand);
snakeBiteCanvas.restart();
}
else if(c == exitCommand) {
destroyApp(true);
notifyDestroyed();
}
}
};