📄 engine.java
字号:
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
public class Engine
extends Thread {
Display display;
FiremanMIDlet midlet;
FiremanCanvas canvas;
static final byte MENU_MAIN = 10;
static final byte MENU_RANK = 11;
static final byte MENU_SETUP = 12;
static final byte MENU_HELP = 13;
static final byte MENU_MODE = 14;
static final byte MENU_DIFFICULTY = 15;
static final byte INIT_GAME = 16; //初始化游戏资源
static final byte LEVEL_PREPARE = 17;//每一关的准备动作
static final byte GAME_LOOP = 18; //游戏正常状态循环,执行判断等
static final byte FIRE_PATH = 19; //水流过path
static final byte PUT_OUT_FIRE=20;//灭火过程
static final byte FALL_PIPES = 21; //掉落新的水管填充位置
static final byte GAME_OVER=22;//游戏失败
static final byte LEVEL_UP=23;//过关
static final byte GAME_PAUSE=24;//暂停
static final byte GAME_SUBMENU=25;//游戏中右键菜单
int state = MENU_MAIN; //主菜单
boolean sound = true;
boolean vibration = false;
byte mode = 0; //游戏模式
byte difficulty = 0; //难度
int level=0; //关数
int score=0; //分数
boolean hasSaved = false; //是否有保存游戏
boolean upload = false; //是否打开上传
long levelStartTime; //本关开始时间
//long currentTime; //当前时间
int leftTime; //剩余时间(秒)
int leftFires; //剩余火
boolean mapscanned = false; //地图是否扫描过
boolean alert = false; //大火警报
int cursorx = 0, cursory = 0; //当前光标位置
int[] fireState;
long[] fireStateChanged;
Pipe[][] map = null;
Random rnd = null;
Vector path = null; //火路图
//boolean onfire=false;//是否有火路可用path.isEmpty()判断
//boolean newFirePath=false;可用path.isEmpty()判断
int currentPathDepth=0;//当前流水的管道深度
int animationFrame=0;//动画帧数,在动画过程中使用
public Engine(FiremanMIDlet m, Display d) {
midlet = m;
display = d;
canvas = new FiremanCanvas(this);
display.setCurrent(canvas);
}
public void keyPressed(int keyCode) {
if(keyCode==35){
if (state==GAME_LOOP)
state=GAME_PAUSE;
else if(state==GAME_PAUSE)
state=GAME_LOOP;
}
else if(state==GAME_LOOP){//此时才接受按键信息
switch (keyCode) {
case 50:
case -1: //2 or up
if (cursory != 0) {
cursory--;
}
break;
case 56:
case -2: //8 or down
if (cursory != 6) {
cursory++;
}
break;
case 52:
case -3: //4 or left
if (cursorx != 1) {
cursorx--;
}
break;
case 54:
case -4: //6 or right
if (cursorx != 5) {
cursorx++;
}
break;
case 53:
case -6: //5 or leftkey
map[cursory][cursorx].rotate();
scanMap(); //mapscanned=false;//重新扫描
break;
case -7://rightkey
state=GAME_SUBMENU;
break;
}
canvas.repaint();
}
}
public void run() {
InfiniteLoop:
while (true) {
try {
Thread.sleep(50);
}
catch (Exception ex) {}
switch (state) {
case INIT_GAME:
initGame();//直接进入LEVEL_PREPARE
case LEVEL_PREPARE:
mapscanned = false;
generateLevel(level);
cursorx = cursory = 3; //当前光标置中
levelStartTime=System.currentTimeMillis();
state = GAME_LOOP;
break;
case GAME_LOOP://游戏循环等待过程
leftTime=200-(int)((System.currentTimeMillis()-levelStartTime)/1000);
if(leftTime<0){//剩余时间没有,game over
state = GAME_OVER;
continue InfiniteLoop;//直接结束本次循环
}
if(alert){//有大火警报
for(int i=0;i<7;i++){
if(fireState[i]>=20&&(System.currentTimeMillis()-fireStateChanged[i])>20000L){
//大火20秒未灭则game over
state = GAME_OVER;
continue InfiniteLoop; //直接结束本次循环
}
}
}
if(leftFires<=0){//剩余火没有,level up
state = LEVEL_UP;
animationFrame=0;//level up初始帧
continue InfiniteLoop;//直接结束本次循环
}
refire(level);//重新燃烧灭掉的楼层
if (!mapscanned) { //如果没扫描过
scanMap();
}
if (!path.isEmpty()) { //扫描后有完整通路
currentPathDepth=1;//从深度1开始置管道为流水中
state = FIRE_PATH;
}
else {
canvas.repaint();
}
break;
case FIRE_PATH://水流过管道过程
if (!path.isEmpty()) { //有新的路径生成或当前路径没有处理完
Pipe temp=null;
int i=0;
while(i<path.size()){
temp=(Pipe)path.elementAt(i);
if(temp.atPathDepth==currentPathDepth){//当前最低的depth
temp.state=4;//流水中
if(temp.type<4){//消除管道加分
score+=5;//每消掉一个管道5分
}
path.removeElementAt(i);
System.out.println("Remove "+((temp.y-32)/12)+","+((temp.x-29)/12)+" from path");
}
else
i++;
}
currentPathDepth++;//下一次找depth大1的
canvas.repaint();
}
else {//处理完路径后
animationFrame=0;
state = PUT_OUT_FIRE; //灭火过程
}
break;
case PUT_OUT_FIRE: //灭火过程
animationFrame++;
if(animationFrame==5){//到第5帧时改变火的状态,使火灭
int fireCount=0;
for(int i=0;i<7;i++){
if(map[i][6].state==4){//火被连起来
if(fireState[i]>=10)//以前有火,确实灭掉火才加分
fireCount++;
fireState[i]=0;
fireStateChanged[i]=System.currentTimeMillis();
}
}
leftFires-=fireCount;
score+=fireCount*fireCount*10;//灭火加分
}
else if(animationFrame==9){//到第9帧时动画结束
animationFrame=0;//消失管子的第0帧
state=FALL_PIPES;
}
else
canvas.repaint();
break;
case FALL_PIPES://管子消失及下落过程
if(animationFrame==0){//管子消失
for(int i=0;i<7;i++){
for(int j=1;j<6;j++){
if(map[i][j].state==4)
map[i][j].state=0;
}
}
animationFrame=1;//管子下落
canvas.repaint();
}
else if(animationFrame==1){//管子下落
for (int j = 1; j < 6; j++) {//忽略两边火和水
int i=6;
while(i>=0){
if (map[i][j].state == 0) { //找到空管子
Pipe temp = map[i][j]; //空管子保存
for (int k = i - 1; k >= 0; k--) {
map[k + 1][j] = map[k][j]; //以上的按顺序向下移动
}
temp.create(); //空的那个重生成
map[0][j] = temp; //移到最顶
}
else
i--;//指针向上移
}
}
canvas.repaint();
mapscanned=false;
state=GAME_LOOP;
}
break;
case LEVEL_UP:
level++;//升一级
state=LEVEL_PREPARE;
/*
animationFrame++;
if(animationFrame==1){//载入图片
try {
canvas.imgBg = Image.createImage(
"/images/levelup.png");
canvas.imgMain = Image.createImage(
"/images/bigfire.png");
canvas.imgItem = Image.createImage(
"/images/fireman.png");
}
catch (IOException ex1) { System.out.println("level up pics loaded error");}
}
else if(animationFrame==20){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -