/* 貪吃蛇可能移動的方向 */
public final static byte DOWN = 2;
public final static byte LEFT = 4;
public final static byte RIGHT = 6;
public final static byte UP = 8;
// 貪吃蛇的當(dāng)前方向
PRivate byte currentDirection;
// 保存貪吃蛇每一段的列表
private Vector worm = new Vector(5, 2);
// 是否需要更新狀態(tài)
private boolean needUpdate;
// 是否在運動中
private boolean moveOnNextUpdate;
// 是否吃到食物
private boolean hasEaten;
// 貪吃蛇的初始位置、長度和方向
private final static int INIT_X = 3;
private final static int INIT_Y = 8;
private final static int INIT_LEN = 8;
private final static byte INIT_DIR =RIGHT;
public void setDirection(byte direction)
這個方法用來改變貪吃蛇運動的方向,只能90度。看下面的實現(xiàn)代碼:
if ((direction != currentDirection) && !needUpdate) {
// 取出列表中的最后一個元素(蛇的頭部)
WormLink sl = (WormLink)worm.lastElement();
int x = sl.getEndX();
int y = sl.getEndY();
// 不同的運動方向坐標(biāo)的改變也不一樣
switch (direction) {
case UP: // 當(dāng)這段向上運動的時候
if (currentDirection != DOWN) {
y--; needUpdate = true; }
break;
case DOWN: // 當(dāng)這段向下運動的時候
if (currentDirection != UP) {
y++; needUpdate = true; }
break;
case LEFT: // 當(dāng)這段向左運動的時候
if (currentDirection != RIGHT) {
x--; needUpdate = true; }
break;
case RIGHT: // 當(dāng)這段向右運動的時候
if (currentDirection != LEFT) {
x++; needUpdate = true; }
break; }
// 當(dāng)更改方向后需要更新
if (needUpdate == true) {
worm.addElement(new WormLink(x, y, 0, direction));
currentDirection = direction; } }
public void update(Graphics g)
這個函數(shù)是更新貪吃蛇狀態(tài)。每次更新都把頭部增加一節(jié),尾部減少一節(jié)。如果它吃到食物尾部段就不減少一節(jié)。看起來就像整只蛇長了一節(jié)。
// 把貪吃蛇頭部增加一格
head = (WormLink)worm.lastElement();
head.increaseLength();
// 如果沒有吃到食物則尾部減少一格
if (!hasEaten) {
WormLink tail;
tail = (WormLink)worm.firstElement();
int tailX = tail.getX();
int tailY = tail.getY();
// 如果尾部塊長度為0就刪除
tail.decreaseLength();
if (tail.getLength() == 0) {
worm.removeElement(tail); }
// 尾部減少一格
g.setColor(WormPit.ERASE_COLOUR);
drawLink(g, tailX, tailY, tailX, tailY, 1);
} else {
// 如果吃到食物就不刪除尾部
hasEaten = false; }
needUpdate = false;
// 確認(rèn)是否在邊界中
if (!WormPit.isInBounds(head.getEndX(), head.getEndY())) {
// 如果不在,就死了
throw new WormException("over the edge"); }
headX = (byte)head.getEndX();
headY = (byte)head.getEndY();
//貪吃蛇的頭部增加一格
g.setColor(WormPit.DRAW_COLOUR);
drawLink(g, headX, headY, headX, headY, 1);
// 判斷是否吃到自己
for (int i = 0; i < worm.size()-1; i++) {
sl = (WormLink)worm.elementAt(i);
if (sl.contains(headX, headY)) {
throw new WormException("you ate yourself"); } }
void drawLink(Graphics g, int x1, int y1, int x2, int y2, int len)
這個函數(shù)用來畫蛇的一段,一只完整的蛇是一段一段組成的。
// 把長度轉(zhuǎn)換成像素長度
len *= WormPit.CELL_SIZE;
// (x1 == x2)說明這一段是垂直的
if (x1 == x2) {
// 把x1轉(zhuǎn)成像素長度
x1 *= WormPit.CELL_SIZE;
// (y2 < y1)說明是向上運動
if (y2 < y1) {
// 就把頭、尾左邊交換并轉(zhuǎn)成像素
y1 = y2 * WormPit.CELL_SIZE;
} else {
// 把y1轉(zhuǎn)成像素
y1 *= WormPit.CELL_SIZE; }
g.fillRect(x1, y1, WormPit.CELL_SIZE, len);
} else {
// 這是水平的一段
y1 *= WormPit.CELL_SIZE;
if (x2 < x1) {
// 就把頭、尾左邊交換并轉(zhuǎn)成像素
x1 = x2 * WormPit.CELL_SIZE;
} else {
x1 *= WormPit.CELL_SIZE; }
g.fillRect(x1, y1, len, WormPit.CELL_SIZE); }
public void paint(Graphics g)
畫出一只完整的貪吃蛇
WormLink sl;
int x1, x2, y1, y2;
int len;
for (int i = 0; i < worm.size(); i++) {
// 取出每一段,然后畫出這一段,連起來就是一只完整的蛇
sl = (WormLink)worm.elementAt(i);
x1 = sl.getX(); x2 = sl.getEndX();
y1 = sl.getY(); y2 = sl.getEndY();
len = sl.getLength();
drawLink(g, x1, y1, x2, y2, len); }
// 段頭部坐標(biāo)
private int x, y;
// 段長度
private int len;
// 移動方向
private byte dir;
public void decreaseLength()
這是從段的頭部減少一格
// 首先段的總長度減少1
len--;
switch (dir) { // 不同的方向左邊的改變也不一樣
case Worm.LEFT:
x--; break;
case Worm.RIGHT:
x++; break;
case Worm.UP:
y--; break;
case Worm.DOWN:
y++; break; }
public boolean contains(int x, int y)
判斷所給的坐標(biāo)(x,y)是否包含在段中
switch (dir) { // 不同的方向判斷的方法也不一樣
case Worm.LEFT:
return ((y == this.y) && ((x <= this.x) && (x >= getEndX())));
case Worm.RIGHT:
return ((y == this.y) && ((x >= this.x) && (x <= getEndX())));
case Worm.UP:
return ((x == this.x) && ((y <= this.y) && (y >= getEndY())));
case Worm.DOWN:
return ((x == this.x) && ((y >= this.y) && (y <= getEndY())));
}
public int getEndX()
得到這一段的尾部x坐標(biāo)(段方向指向的最后一格的坐標(biāo)),當(dāng)這段是蛇的頭部段時,得到的是頭部最前面的坐標(biāo)。
// 不同的方向判斷方法不一樣
if (dir == Worm.LEFT)
return x-len;
if (dir == Worm.RIGHT)
return x+len;
return x;
private void paintPitContents(Graphics g)
重繪屏幕上的所有元素
// 更新貪吃蛇的狀態(tài)
myWorm.update(g);
// 頭部的位置和食物的位置重合就吃到食物
if (myFood.isAt(myWorm.getX(), myWorm.getY())) {
myWorm.eat();
score += level;
foodEaten++;
if (foodEaten > (level << 1)) {
/* 增加游戲難度 */
forceRedraw = true;
foodEaten = 0;
level++;
if (tonePlayer != null) {
try {
tonePlayer.setMediaTime(0);
tonePlayer.start();
} catch (MediaException me) { } }
} else {
if (audioPlayer != null) {
try {
Manager.playTone(69, 50, 100); // Play audio
} catch (MediaException me) { } } }
g.setColor(WormPit.ERASE_COLOUR);
// 填充長方形(三個字的寬度)
g.fillRect((width - (SCORE_CHAR_WIDTH * 3))-START_POS,
height-START_POS,
(SCORE_CHAR_WIDTH * 3),
SCORE_CHAR_HEIGHT);
g.setColor(WormPit.DRAW_COLOUR);
// 顯示新的分?jǐn)?shù)
g.drawString("" + score,
width - (SCORE_CHAR_WIDTH * 3) - START_POS,
height - START_POS, Graphics.TOPGraphics.LEFT);
// 重新生成食物
myFood.regenerate();
int x = myFood.getX();
int y = myFood.getY();
while (myWorm.contains(x, y)) {
// 如果食物和貪吃蛇的身體重復(fù)就重新生成
myFood.regenerate();
x = myFood.getX(); y = myFood.getY(); } }
// 畫出食物
myFood.paint(g);
} catch (WormException se) { gameOver = true; }
public void run()
主循環(huán)體:
while (!gameDestroyed) { // 游戲不終止就一直循環(huán)執(zhí)行
try {
synchronized (myWorm) { // 多線程中要進行同步
// 如果游戲結(jié)束
if (gameOver) {
if (WormScore.getHighScore(level) < score) {
// 把最高分保存
WormScore.setHighScore(level, score, "me"); }
if ((audioPlayer != null) &&
(audioPlayer.getState() == Player.STARTED)) {
try {
audioPlayer.stop();
Manager.playTone(60, 400, 100);
} catch (Exception ex) { } }
// 重繪
repaint();
// 游戲結(jié)束時等待用戶重新開始
myWorm.wait();
} else if (gamePaused) {
//重繪
repaint();
// 游戲暫停時等待用戶重新開始
myWorm.wait();
} else {
// 游戲繼續(xù)
myWorm.moveOnUpdate();
repaint();
// 這里的等待時間決定了游戲難度!!!
myWorm.wait(DEFAULT_WAIT-(level*40));
}
}
} catch (java.lang.InterruptedException ie) {
}
}
protected void startApp()進入討論組討論。
實現(xiàn)MIDlet父類的方法,當(dāng)開始程序時首先執(zhí)行這個函數(shù)
// 顯示畫板
Display.getDisplay(this).setCurrent(theGame);
try {
// 開始游戲線程
Thread myThread = new Thread(theGame);
myThread.start();
} catch (Error e) {
destroyApp(false);
notifyDestroyed(); }
public void commandAction(Command c, Displayable d)
接受并處理用戶輸入事件
// 重新開始
if (c == restartCmd) {
theGame.restart();
};
// 改變難度等級
if (c == levelCmd) {
Item[] levelItem = {//請自己替換[]
new Gauge("Level", true, 9, theGame.getLevel())
};
Form f = new Form("Change Level", levelItem);
f.addCommand(OKCmd);
f.addCommand(cancelCmd);
f.setCommandListener(this);
Display.getDisplay(this).setCurrent(f);
};
// 離開游戲
if (c == exitCmd) {
destroyApp(false);
notifyDestroyed();
};
// 開始游戲
if (c == startCmd) {
theGame.removeCommand(startCmd);
theGame.addCommand(restartCmd);
theGame.restart();
};
// 確定
if (c == OKCmd) {
Form f = (Form)d;
Gauge g = (Gauge)f.get(0);
theGame.setLevel(g.getvalue());
Display.getDisplay(this).setCurrent(theGame);
};
// 取消
if (c == cancelCmd) {
Display.getDisplay(this).setCurrent(theGame);
};
// 打開音效
if (c == audioOnCmd) {
/* 打開音效 */
theGame.createAudioPlayer();
theGame.removeCommand(audioOnCmd);
theGame.addCommand(audioOffCmd);
};
// 關(guān)閉音效
if (c == audioOffCmd) {
/* 關(guān)閉音效 */
theGame.destroyAudioPlayer();
theGame.removeCommand(audioOffCmd);
theGame.addCommand(audioOnCmd);
}
(出處:http://m.survivalescaperooms.com)
新聞熱點
疑難解答