📄 mazegamecanvas.java
字号:
import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
public class MazeGameCanvas extends GameCanvas implements Runnable{
private MazeNetwork midlet;
static final int CELL_WIDTH = 24; //图像的宽度
static final int CELL_HEIGHT = 24; //图像的高度
static final byte GRASS = 1; //背景草地
static final byte WALL = 2; //墙
static final byte BORDER = 72; //地图的边大小,到达边后就滚动地图
//游戏运行标志
boolean isRunning = true;
//游戏地图
int[][] cells = null;
//================================================视窗定义
/**
* 可视窗口的坐上角X坐标
*/
private int vwX=0;
/**
* 可视窗口的坐上角Y坐标
*/
private int vwY=0;
/**
* 可视窗口的宽度
*/
private int vwWidth = 0;
/**
* 可视窗口的高度
*/
private int vwHeight = 0;
//================================================汽车定义
/**
* 汽车的移动方向
*/
private int direction = RIGHT;
/**
* 定义汽车的动画
*/
private int[] aniSequence = {0, 1, 0, 1, 0, 2};
/**
* 汽车的X坐标
*/
int carX;
/**
* 汽车的Y坐标
*/
int carY;
/**
* 自己的汽车精灵
*/
Sprite myCar;
/**
* 对手的汽车精灵
*/
Sprite otherCar;
//================================================图层定义
/**
* 墙图层
*/
TiledLayer walls;
/**
* 背景图层
*/
TiledLayer bg;
/**
* 定义LayerManager管理图层以及滚动地图
*/
LayerManager layers;
/**
* 构造函数,创建游戏画布
* @param parent
*/
public MazeGameCanvas(MazeNetwork midlet, int[][] map) {
super(false);
this.cells = map;
this.midlet = midlet;
setFullScreenMode(true);
vwWidth = getWidth();
vwHeight = getHeight();
}
public void run() {
Graphics g = getGraphics();
isRunning = true;
while (isRunning) {
int keyState = getKeyStates();
if ((keyState & LEFT_PRESSED) != 0) {
move(LEFT);
} else if ((keyState & RIGHT_PRESSED) != 0) {
move(RIGHT);
}else if ((keyState & UP_PRESSED) != 0) {
move(UP);
}else if ((keyState & DOWN_PRESSED) != 0) {
move(DOWN);
}
g.setColor(0);
g.fillRect(0, 0, vwWidth, vwHeight);
layers.paint(g, 0, 0);
myCar.nextFrame();
otherCar.nextFrame();
layers.paint(g, 0,0);
// 刷新画面
flushGraphics();
try {
Thread.sleep(100); // sleep a bit
} catch(InterruptedException ex){
ex.printStackTrace();
break;
}
}
}
/**
* 移动汽车
* <p>
* @param dir 汽车的方向
*/
private void move(int dir) {
//改变汽车方向
if (direction != dir) {
direction = dir;
switch(direction) {
case LEFT:
myCar.setTransform(Sprite.TRANS_NONE);
break;
case RIGHT:
myCar.setTransform(Sprite.TRANS_MIRROR);
break;
case UP:
myCar.setTransform(Sprite.TRANS_ROT90);
break;
case DOWN:
myCar.setTransform(Sprite.TRANS_ROT270);
break;
}
}
myCar.setPosition(carX*CELL_HEIGHT, carY*CELL_WIDTH);
//移动汽车
int x = carX;
int y = carY;
switch(direction) {
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
}
move(x, y);
}
private void move(int x, int y) {
myCar.setPosition(x*CELL_HEIGHT, y*CELL_WIDTH);
//判断是否可以移动 - 碰撞检测
if (myCar.collidesWith(walls, false)) {
myCar.setPosition(carX*CELL_HEIGHT, carY*CELL_WIDTH);
return;
}
carX = x;
carY = y;
midlet.requestMove(carX, carY);
//检测是否走出迷宫
if (carY == cells.length-1 && carX == cells[0].length-2) {
midlet.requestWin();
isRunning = false;
Alert alert = new Alert("恭喜,走出了迷宫");
alert.setString("游戏完成!!!");
alert.setType(AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
midlet.display.setCurrent(alert, midlet.frmHall);
}
//判断是否需要滚动地图
int cx = carX * CELL_WIDTH;
int cy = carY * CELL_WIDTH;
if (cx < this.vwX + BORDER && vwX>=CELL_WIDTH) {
//向左滚动屏幕
vwX -= CELL_WIDTH;
layers.setViewWindow(vwX, vwY, vwWidth, vwHeight);
} else if (cx > this.vwX + vwWidth - BORDER
&& vwX + vwWidth<=bg.getWidth() - CELL_WIDTH) {
//向右滚动屏幕
vwX += CELL_WIDTH;
layers.setViewWindow(vwX, vwY, vwWidth, vwHeight);
}
if (cy < this.vwY + BORDER && vwY>=CELL_HEIGHT) {
//向上滚动屏幕
vwY -= CELL_HEIGHT;
layers.setViewWindow(vwX, vwY, vwWidth, vwHeight);
} else if (cy > this.vwY + vwHeight - BORDER
&& vwY + vwHeight<=bg.getHeight() - CELL_HEIGHT) {
//向下滚动屏幕
vwY += CELL_HEIGHT;
layers.setViewWindow(vwX, vwY, vwWidth, vwHeight);
}
}
/**
* 初始化游戏
*/
public void initialize() {
//初始化数据
vwX = 0;
vwY = 0;
carX = 1;
carY = 1;
Image imgBG = null;
Image imgCar = null;
Image imgOtherCar = null;
try {
imgBG = Image.createImage("/res/bg.PNG");
imgCar = Image.createImage("/res/car.PNG");
imgOtherCar = changeImgColor(imgCar);
} catch (IOException e) {
System.out.println("装载图像出现错误: " + e.getMessage());
e.printStackTrace();
}
//创建背景草地图层
bg = new TiledLayer(cells.length, cells[0].length,
imgBG, CELL_WIDTH, CELL_HEIGHT);
//创建墙图层
walls = new TiledLayer(cells.length, cells[0].length,
imgBG, CELL_WIDTH, CELL_HEIGHT);
for(int y=0;y<cells.length;y++) {
for(int x=0;x<cells[0].length;x++) {
bg.setCell(x, y, 0);
walls.setCell(x, y, 0);
if (cells[y][x] == 1) {
walls.setCell(x, y, WALL);
} else {
bg.setCell(x, y, GRASS);
}
}
}
myCar = new Sprite(imgCar, CELL_WIDTH, CELL_HEIGHT);
myCar.setFrameSequence(aniSequence);
//缺省方向向右
myCar.setTransform(Sprite.TRANS_MIRROR);
myCar.setPosition(carX*CELL_HEIGHT, carY*CELL_WIDTH);
otherCar = new Sprite(imgOtherCar, CELL_WIDTH, CELL_HEIGHT);
otherCar.setFrameSequence(aniSequence);
//缺省方向向右
otherCar.setTransform(Sprite.TRANS_MIRROR);
otherCar.setPosition(carX*CELL_HEIGHT, carY*CELL_WIDTH);
/*
* 创建图层管理
*/
layers = new LayerManager();
layers.append(myCar);
layers.append(otherCar);
layers.append(walls);
layers.append(bg);
/*
* 设置可视窗口
*/
layers.setViewWindow(vwX, vwY, vwWidth, vwHeight);
new Thread(this).start();
}
private Image changeImgColor(Image source) {
Image img = null;
int width = source.getWidth();
int height = source.getHeight();
int[] rgbData = new int[width * height];
source.getRGB(rgbData, 0, width, 0, 0, width, height);
for (int i = 0; i < rgbData.length; i++) {
int p = rgbData[i];
int a = ((p & 0xff000000) >> 24);
int r = ((p & 0x00ff0000) >> 16);
int g = ((p & 0x0000ff00) >> 8);
if (g>0 && g<255) {
g = ~g;
}
int b = ((p & 0x000000ff) >> 0);
rgbData[i] = (a << 24) | (r << 16) | (g << 8) | b;
}
try {
img = Image.createRGBImage(rgbData, width, height, true);
} catch(Exception e) {}
return img;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -