📄 gamelogic.java
字号:
package pangxie;
import java.util.*;
import javax.microedition.lcdui.*;
class GameLogic {
public final static int Base_Point_X = 37;
public final static int Base_Point_Y = 60;
public final static int Delay = 100;
static Random random = new Random();
static int pangXieCount = 0;
GameCanvas gc;
Timer gameTimer;
Flower f;
static int baoNum = 100; //宝的初始数量
boolean isPaused;
public int i = 0; //控制游戏暂停时显示选项的变量
public static int level;
public GameLogic(GameCanvas gc) {
this.gc = gc;
gc.openDb("recorder");
gc.getPlayerList();
}
public void initGame() {
level = 0;
baoNum = 3;
pangXieCount = 0;
gc.pangs.removeAllElements();
int pointx = Base_Point_X;
int pointy = Base_Point_Y;
PangXie.total = 0;
for (int i = 0; i < gc.follows.length; i++) {
f = new Flower(pointx + 45 * i, pointy, this);
gc.follows[i] = f;
}
gc.man = new Man();
startGame();
}
public void startGame() {
gc.status = 2;
gameTimer = new Timer();
gameTimer.schedule(new CreatingTask(), Delay, Delay * 20 - level * 100);
gameTimer.schedule(new MovingTask(), Delay, Delay - level * 5);
}
public void pauseGame() {
isPaused = true;
}
public void resumeGame() {
isPaused = false;
}
public void gameOver() {
if (gameTimer != null) {
gameTimer.cancel();
gc.pangs.removeAllElements();
gc.status = -1;
}
System.gc();
}
public void bingo(PangXie px, Flower f) {
px.deadFlower = f;
px.crashX = px.x + 5;
px.crashY = px.y;
px.fall();
}
public void createNextPangXie() {
int pt = Math.abs(random.nextInt()) % PangXie.images.length;
int pos = random.nextInt() % 2;
if (pos == 0) {
gc.pangs.addElement(new PangXie(pt, PangXie.Left_X, PangXie.Base_Y,
PangXie.Dir_Right, gc));
} else {
gc.pangs.addElement(new PangXie(pt, PangXie.Right_X, PangXie.Base_Y,
PangXie.Dir_Left, gc));
}
this.pangXieCount++;
}
public void handleKeyEvent(int gameAction) {
switch (gc.status) {
case 1: //主界面
switch (gameAction) {
case Canvas.UP:
gc._Y -= 24;
if (86 + gc._Y < 86) {
gc._Y = 72;
}
break;
case Canvas.DOWN:
gc._Y += 24;
if (86 + gc._Y > 158) {
gc._Y = 0;
}
break;
case Canvas.FIRE:
if (gc._Y == 0) {
initGame();
} else if (gc._Y == 72) {
PangMIDlet.quitApp();
}
if (gc._Y == 24) {
gc.status = 3;
}
if (gc._Y == 48) {
gc.status = 5;
}
break;
}
break;
case 2: { //游戏中...
switch (gameAction) {
case Canvas.LEFT: //move left
gc.man.move( -1);
break;
case Canvas.RIGHT: //move right
gc.man.move(1);
break;
case 11: // 星号键,放宝
if (baoNum > 0) {
baoNum--;
f = gc.follows[gc.man.logicPos];
f.power = 100;
f.baoReleasedPermission = 1;
if (f.y == Base_Point_Y) {
f.fall();
}
}
break;
case 0: // Pause game(左右软键及“1”键)
isPaused = !isPaused;
gc.status = 10;
break;
default:
f = gc.follows[gc.man.logicPos];
if (f.y == Base_Point_Y) {
f.power = 1;
f.fall();
}
break;
}
break;
}
case 10: //pause in gaming
switch (gameAction) {
case Canvas.UP:
i = 1;
break;
case Canvas.DOWN:
i = 2;
break;
case Canvas.FIRE:
if (i == 2) {
endGame();
}
if (i == 1) {
gc.status = 2;
resumeGame();
}
break;
}
break;
case 3: //Help之一
switch (gameAction) {
case Canvas.FIRE:
gc.status = 1;
break;
default:
gc.status = 4;
break;
}
break;
case 4: //Help之二
switch (gameAction) {
case Canvas.FIRE:
gc.status = 1;
break;
default:
gc.status = 3;
break;
}
case 5: //topten
gc.status = 1;
break;
case -1: //game over
recorder rec = new recorder(gc);
PangMIDlet.instance.setScreen(rec);
if (true) {
gc.status = 1;
System.gc();
break;
}
}
}
class CreatingTask extends TimerTask {
public void run() {
if (isPaused) {
return;
}
createNextPangXie();
if (pangXieCount % 20 == 0) {
System.out.println("pangXieCount = " + pangXieCount);
System.out.println("level = " + level);
}
System.out.println("pangXieCount( = )" + pangXieCount);
if (pangXieCount % 20 == 0) {
level++;
System.out.println("level( = )" + level);
gameTimer.cancel();
startGame();
}
}
}
class MovingTask extends TimerTask {
public void run() {
if (isPaused) {
return;
}
for (int i = 0; i < gc.pangs.size(); i++) {
PangXie px = (PangXie) gc.pangs.elementAt(i);
px.crawl();
if (px.y > 200) {
gc.pangs.removeElementAt(i);
if (px.type == 0) {
PangXie.total += PangXie.addscore[1];
} else if (px.type == 4) {
PangXie.total += PangXie.addscore[2];
} else {
PangXie.total += PangXie.addscore[0];
}
}
}
}
}
public void endGame() {
pangXieCount = 0;
isPaused = false;
gameTimer.cancel();
gc.pangs.removeAllElements();
gc.status = 1;
gc.repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -