import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class MiGong extends GameCanvas{
private static final int MG_COLS = 20; //迷宫水平列数
private static final int MG_ROWS = 20; //迷宫垂直行数
private static final int CELL_SIZE = 16; //单元格的尺寸
private static final int DISTANCE = 2;
private TiledLayer migong; //迷宫
private TiledLayer mgBack; //迷宫背景
private Sprite pathfinder; //探险者
private LayerManager manager; //图层管理器
private int[] backArray= { //迷宫数组
1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1,
1, 2, 1, 2, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1,
1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1,
1, 2, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 2, 1,
1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 1,
1, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 1, 2, 1, 1, 1, 1,
1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 2, 2, 1,
1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1, 2, 1, 1, 2, 1,
1, 2, 1, 2, 1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 1, 2, 2, 2, 2, 1,
1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 2, 1,
1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1,
1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 2, 1,
1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1, 2, 2, 1, 2, 1,
1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 1,
1, 2, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1,
1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 1,
1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1,
1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1, 2, 1, 2, 2, 2, 2, 2, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1
};
//探险者在不同方向上的帧序列
private int[] leftSequence = {9, 10, 11};
private int[] rightSequence = {3, 4, 5};
private int[] downSequence = {6, 7, 8};
private int[] upSequence = {0, 1, 2};
private Graphics g; //绘图对象
private int hd = 2; //水平移动距离
private int vd = 2; //垂直移动距离
private int mgX = 20; //迷宫位置
private int mgY = 15;
private int mgWidth;
private int mgHeight;
private int windowX; //可视窗口的位置
private int windowY;
private int windowWidth; //可视窗口的宽
private int windowHeight; //可视窗口的高
private int viewX; //窗口在屏幕上显示的位置
private int viewY;
private int currDirection; //当前方向
public MiGong() {
super(false);
try {
//创建并初始化迷宫
Image imgMigong = Image.createImage("/Storage16.png");
migong = new TiledLayer(MG_COLS, MG_ROWS, imgMigong,
CELL_SIZE, CELL_SIZE);
mgBack = new TiledLayer(MG_COLS, MG_ROWS, imgMigong,
CELL_SIZE, CELL_SIZE);
for(int i=0; i<backArray.length; i++) {
mgBack.setCell(i%MG_COLS, i/MG_COLS, backArray[i]);
if(backArray[i] == 2) {
migong.setCell(i%MG_COLS, i/MG_COLS, 0);
}
else {
migong.setCell(i%MG_COLS, i/MG_COLS, backArray[i]);
}
}
mgWidth = MG_COLS*CELL_SIZE;
mgHeight = MG_ROWS*CELL_SIZE;
//创建探险者
Image imgPathfinder = Image.createImage("/Worker16.png");
pathfinder = new Sprite(imgPathfinder, CELL_SIZE, CELL_SIZE);
pathfinder.setFrameSequence(leftSequence);
currDirection = LEFT;
hd = DISTANCE;
vd = 0;
}
catch(IOException ioe) {
}
g = getGraphics(); //获取绘图对象
//创建图层管理器
manager = new LayerManager();
//向图层管理器中添加图层,注意添加顺序不能颠倒。
manager.append(pathfinder);
manager.append(mgBack);
//设置迷宫和探险者的位置
migong.setPosition(mgX, mgY);
mgBack.setPosition(mgX, mgY);
pathfinder.setPosition(mgX+CELL_SIZE, mgY+CELL_SIZE);
}
//画面显示前自动调用该方法,初始化图层管理器的显示窗口
public void showNotify() {
int screenWidth = getWidth(); //屏幕的宽
int screenHeight = getHeight(); //屏幕的高
windowWidth = screenWidth*3/5;
windowHeight = screenHeight*3/5;
windowX = mgX;
windowY = mgY;
manager.setViewWindow(windowX, windowY,
windowWidth, windowHeight);
viewX = (screenWidth-windowWidth)/2;
viewY = (screenHeight-windowHeight)/2;
draw();
}
//绘制游戏界面
private void draw() {
g.fillRect(viewX, viewY, windowWidth, windowHeight);
manager.paint(g, viewX, viewY);
flushGraphics();
}
//搬运工worker转向
private void tranceDirection(int direction) {
currDirection = direction;
switch(direction) {
case LEFT:
pathfinder.setFrameSequence(leftSequence);
hd = -DISTANCE;
vd = 0;
break;
case RIGHT:
pathfinder.setFrameSequence(rightSequence);
hd = DISTANCE;
vd = 0;
break;
case UP:
pathfinder.setFrameSequence(upSequence);
hd = 0;
vd = -DISTANCE;
break;
case DOWN:
pathfinder.setFrameSequence(downSequence);
hd = 0;
vd = DISTANCE;
break;
}
}
//键盘按下事件处理方法
protected void keyPressed(int keyCode) {
int gameAction = getGameAction(keyCode);
if(currDirection != gameAction) {
tranceDirection(gameAction);
}
pathfinder.move(hd, vd);
if(pathfinder.collidesWith(migong, false)) {
pathfinder.move(-hd, -vd);
}
else {
int x = pathfinder.getX(); //探险者的位置
int y = pathfinder.getY();
if((x+CELL_SIZE) >= (windowX+windowWidth)) {
windowX += windowWidth/5;
if(windowX+windowWidth>mgX+mgWidth) {
windowX = mgX-windowWidth;
}
}else if(x<windowX) {
windowX -= windowWidth/5;
if(windowX<mgX) {
windowX = mgX;
}
}
if((y+CELL_SIZE) >= (windowY+windowHeight)) {
windowY += windowHeight/5;
if(windowY+windowHeight>mgY+mgHeight) {
windowY = mgY-windowHeight;
}
}else if(y<windowY) {
windowY -= windowHeight/5;
if(windowY<mgY) {
windowY = mgY;
}
}
manager.setViewWindow(windowX, windowY,
windowWidth, windowHeight);
}
pathfinder.nextFrame();
draw();
}
}