import javax.microedition.lcdui.*;
public class NewGame
extends Canvas
implements Runnable, CommandListener {
private static Displayable instance;
public Command back, start, pause;
private java.util.Random random;
private int COLOR_PANEL = (255 << 16) | (255 << 8) | (255);
private int COLOR_BACKGROUND = (200 << 16) | (200 << 8) | (200);
private int COLOR_SNAKE = (100 << 16) | (100 << 8) | (100);
private static int BLOCK_ROW_NUM = 8;
private static int BLOCK_COL_NUM = 12;
private int BLOCK_LENGTH = 9;
private int MARGIN_LEFT = 5;
private int MARGIN_TOP = 5;
private static Block[][] blockStatus;
private boolean isOut, isCross;
private boolean isRunning;
private boolean isHaveBlock;
private boolean isTouchRandom;
private static int oldDirection;
private Thread snakeThread;
private int sleepTime;
private int snakeLength;
private int headDirection;
final private static int DIRECTION_UP = 0;
final private static int DIRECTION_DOWN = 1;
final private static int DIRECTION_LEFT = 2;
final private static int DIRECTION_RIGHT = 3;
final public static int DIRECTION_NONE=4;
private int headX, headY;
private int tailX, tailY;
public NewGame() {
blockStatus = new Block[BLOCK_ROW_NUM][BLOCK_COL_NUM];
sleepTime = 500;
random = new java.util.Random(); initialization();
back = new Command("返回", Command.BACK, 1);
start = new Command("开始", Command.OK, 2);
pause = new Command("暂停", Command.STOP, 3);
addCommand(back);
addCommand(start);
setCommandListener(this);
}
private void initialization() {
isOut = false;
isCross = false;
isHaveBlock = false;
isTouchRandom = false;
oldDirection = DIRECTION_RIGHT;
snakeLength = 3;
for (int i = 0; i < BLOCK_ROW_NUM; i++) {
for (int j = 0; j < BLOCK_COL_NUM; j++) {
blockStatus[i][j] = new Block();
}
} blockStatus[BLOCK_ROW_NUM / 2][1].status = true;
blockStatus[BLOCK_ROW_NUM / 2][1].direction = DIRECTION_RIGHT;
blockStatus[BLOCK_ROW_NUM / 2][2].status = true;
blockStatus[BLOCK_ROW_NUM / 2][2].direction = DIRECTION_RIGHT;
blockStatus[BLOCK_ROW_NUM / 2][3].status = true;
blockStatus[BLOCK_ROW_NUM / 2][3].direction = DIRECTION_RIGHT;
headX = BLOCK_ROW_NUM / 2;
headY = 3;
tailX = BLOCK_ROW_NUM / 2;
tailY = 1;
headDirection = DIRECTION_RIGHT;
}
synchronized public static Displayable getInstance() {
if (instance == null) {
Navigator.isNewGame = false;
instance = new NewGame();
}
else if (Navigator.isNewGame) {
Navigator.isNewGame = false;
instance = new NewGame(); }
return instance;
}
public void commandAction(Command command, Displayable displable) {
if (command.getCommandType() == Command.OK) {
isRunning = true;
this.removeCommand(start);
this.removeCommand(back);
this.addCommand(pause);
snakeThread = new Thread(this);
snakeThread.start();
}
else if (command.getCommandType() == Command.STOP) {
isRunning = false;
this.removeCommand(pause);
this.addCommand(start);
this.addCommand(back);
}
else if (command.getCommandType() == Command.BACK) {
Navigator.changeScreen(command.getLabel());
}
}
protected void keyPressed(int keycode) {
if (isRunning) {
switch (keycode) {
case KEY_NUM2:
case -1:
if(headX-1<0){
break;
}
else
if(blockStatus[headX-1][headY].direction==DIRECTION_DOWN&&blockStatus[headX-1][headY].status==true){
break;
}
else{
blockStatus[headX][headY].direction = DIRECTION_UP;
headDirection = DIRECTION_UP;
break;
}
case KEY_NUM4:
case -3:
if(headY-1<0){
break;
} else
if(blockStatus[headX][headY-1].direction==DIRECTION_RIGHT&&blockStatus[headX][headY-1].status==true){
break;
}
else{
blockStatus[headX][headY].direction = DIRECTION_LEFT;
headDirection = DIRECTION_LEFT;
break;
}
case KEY_NUM6:
case -4:
if(headY+1==BLOCK_COL_NUM){
break;
}
else
if(blockStatus[headX][headY+1].direction==DIRECTION_LEFT&&blockStatus[headX][headY+1].status==true){
break;
}
else{
blockStatus[headX][headY].direction = DIRECTION_RIGHT;
headDirection = DIRECTION_RIGHT;
break;
}
case KEY_NUM8:
case -2: if(headX+1==BLOCK_ROW_NUM){
break;
}
else
if(blockStatus[headX+1][headY].direction==DIRECTION_UP&&blockStatus[headX+1][headY].status==true){
break;
}
else{
blockStatus[headX][headY].direction = DIRECTION_DOWN;
headDirection = DIRECTION_DOWN;
break;
}
}
}
}
public void paint(Graphics g) {
/**@todo Implement this javax.microedition.lcdui.Displayable abstract method*/
g.setColor(COLOR_PANEL);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
paintBlocks(g); }
private void paintBlocks(Graphics g) {
for (int i = 0; i < BLOCK_ROW_NUM; i++) {
for (int j = 0; j < BLOCK_COL_NUM; j++) {
if (!blockStatus[i][j].status) {
g.setColor(COLOR_BACKGROUND);
g.fillRect(MARGIN_LEFT + (BLOCK_LENGTH + 1) * j,
MARGIN_TOP + (BLOCK_LENGTH + 1) * i,
BLOCK_LENGTH, BLOCK_LENGTH);
}
else if (blockStatus[i][j].status) {
g.setColor(COLOR_SNAKE);
g.fillRect(MARGIN_LEFT + (BLOCK_LENGTH + 1) * j,
MARGIN_TOP + (BLOCK_LENGTH + 1) * i,
BLOCK_LENGTH, BLOCK_LENGTH);
}
}
}
}
public void run() {
while (isRunning) {
moveSnakeHead(); if (snakeLength >= BLOCK_ROW_NUM * BLOCK_COL_NUM) {
isRunning = false;
Navigator.changeScreen("胜利");
break;
}
else if (isOut || isCross) {
isRunning = false;
Navigator.isNewGame = true;
initialization();
Navigator.changeScreen("返回");
}
else {
moveSnak