import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class MazeCanvas extends GameCanvas implements Runnable,CommandListener
{
private MazeMIDlet midlet;
private LevelCanvas level;
private boolean isPlay;
private boolean isPlaying;//判断是否在过关中
private boolean isFollowerMoving;//follower是否可以移动
private boolean isBadLuck;//判断精灵是否碰到金砖,如果碰到则显示"BAD LUCK"
private boolean bombFire;
private long delay;
private long counter;//计数器
private int lifeCount=5;//生命数,初始化为5
private int eatenPresent;//被吃掉的礼品数
private int direction;//Bomb的方向角度
private final int U=-1;//精灵移动的四个方向
private final int R=-2;
private final int D=-3;
private final int L=-4;
private int Dir;//sprite的移动方向
private long startTime,overTime;//游戏开始时间与结束时间
private long usedTime;//所用总时间
private int LEVEL=1;
private final int LEVEL_1=1;
private final int LEVEL_2=2;
private final int LEVEL_3=3;
private final int LEVEL_4=4;
private final int LEVEL_5=5;
private int vX,vY;//视窗坐标
private int x,y;//sprite的坐标索引
private int x_f,y_f;//追踪者的坐标索引
private int rows,columns;//地图的行数与列数
private int width,height;//屏幕的宽高
private int cellwidth,cellheight;//地图每个单元格的宽高
private int xStep,yStep;//sprite移动的步长值
private Command backCommand;//返回
private LayerManager layerManager;//图层管理器
private TiledLayer maze,maze1,maze2,maze3,maze4,maze5;
private Sprite sprite,follower,bomb;//主人公、追踪者、炸弹
private Image mazeImage,spriteImage,followerImage,bombImage;
private Image presentImage_1;//礼物图片,以便向玩家显示所吃礼物个数
private Image presentImage_2;//礼物图片,以便向玩家显示所吃礼物个数
private Image badLuckImage;//badLuck图片
private Image lifeImage;//生命值
private static final int []bombTransForms={//炸弹运动的动作序号
Sprite.TRANS_NONE, Sprite.TRANS_NONE,
Sprite.TRANS_ROT90, Sprite.TRANS_ROT90,
Sprite.TRANS_ROT180, Sprite.TRANS_ROT180,
Sprite.TRANS_ROT270, Sprite.TRANS_ROT270,
};
//第一关17*13
private static final int []map1={
// 0,1,0,3,4,5,6,7,8,9,0,1,0,3,4,5,6
1,1,1,1,1,1,1,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17];
private static final int [][]map2_c=new int[15][19];
private static final int [][]map3_c=new int[17][21];
private static final int [][]map4_c=new int[19][23];
private static final int [][]map5_c=new int[21][25];
//构造函数
public MazeCanvas(MazeMIDlet midlet)
{
super(false);
this.midlet=midlet;//取得主类的对象引用,以方便访问其他文件
level=new LevelCanvas(this);
setFullScreenMode(true);//全屏模式
backCommand=new Command("返回",Command.BACK,1);
addCommand(backCommand);//添加命令
setCommandListener(this);//为命令注册监听器
width=getWidth();
height=getHeight();//取得手机屏幕的宽、高
delay=50;//设置画面帧每50ms刷新一次
cellwidth=16;cellheight=16;//Tile的宽和高,根据所做图片的大小来设置的
LEVEL=LEVEL_1;//初始化为第一关
rows=13+2*(LEVEL-1);columns=17+2*(LEVEL-1);//初始化当前关地图的行数与列数
maze1=createMaze("/mazetile.png",map1,13,17,cellwidth,cellheight);//以下是先生成好5张地图
maze2=createMaze("/mazetile.png",map2,15,19,cellwidth,cellheight);
maze3=createMaze("/mazetile.