📄 mazegamecanvas.java
字号:
import java.io.IOException;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
import javax.microedition.lcdui.*;
//import java.util.*;
public class MazeGameCanvas extends GameCanvas implements Runnable{
private static final int CELL_WIDTH = 24; //图像格子的宽度
private static final int CELL_HEIGHT = 24; //图像格子的高度
private Display display;
static final byte GRASS = 1; //背景草地ݵ�
static final byte WALL = 2; //墙
static final byte BORDER = 72; //地图的边大小,到达边后就滚动地图�ߺ���ͼ
//游戏运行标志
private boolean isRunning = true;
//////////////////////
private int isTime=Maze.isTime;
private long oldTime = 0;
private long newTime = 0;
private long playTime = 10000;
public Maze maze;
//游戏地图
int[][] cells = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1},
{1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1},
{1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
{1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}
};
//=============== 视窗定义 =====================
/**
* 可视窗口的左上角坐标
*/
private int vwX=0,vwY=0;
/**
* 可视窗口的宽度、高度
*/
private int vwWidth = 0, vwHeight = 0;
//=============== 汽车定义 =====================
//移动方向ƶ�����
private int direction = RIGHT;
//汽车的动画序列���
private int[] aniSequence = {0, 1, 0, 1, 0, 2};
//汽车的坐标
int carX,carY;
//汽车精灵
Sprite car;
//================ 图层定义 ===================
// 墙图层�
TiledLayer walls;
// 背景图层
TiledLayer bg;
//定义LayerManager管理图层以及滚动地图
LayerManager layers;
/**
* 构造函数,创建游戏画布
* @param parent
*/
public MazeGameCanvas(Display dis,Maze maze1) {
super(false);
maze=maze1;
setFullScreenMode(true);
vwWidth = getWidth();
vwHeight = getHeight();
display=dis;
}
//线程的执行体
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);
}
car.nextFrame();
layers.paint(g, 0,0);
//刷新画面
flushGraphics();
newTime=System.currentTimeMillis();
if(!(carY == cells.length-1 && carX == cells[0].length-2) && (newTime-oldTime)>=playTime && isTime==1)
{
information info=new information(false,display,maze);
display.setCurrent(info);
}
try {
Thread.sleep(50); // sleep a bit
} catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
/**
* 移动汽车
* <p>
* @param dir 汽车的方向
*/
private void move(int dir) {
//改变汽车方向
direction = dir;
switch(direction) {
case LEFT:
car.setTransform(Sprite.TRANS_NONE);
break;
case RIGHT:
car.setTransform(Sprite.TRANS_MIRROR);
break;
case UP:
car.setTransform(Sprite.TRANS_ROT90);
break;
case DOWN:
car.setTransform(Sprite.TRANS_ROT270);
break;
}
car.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;
}
movexy(x, y);
}
private void movexy(int x, int y) {
car.setPosition(x*CELL_HEIGHT, y*CELL_WIDTH);
//判断是否可以移动 - 与墙进行碰撞检测
if (car.collidesWith(walls, false)) {
car.setPosition(carX*CELL_HEIGHT, carY*CELL_WIDTH);
return;
}
carX = x;
carY = y;
//检测是否走出迷宫
if (carY == cells.length-1 && carX == cells[0].length-2) {
information info=new information(true,display,maze);
display.setCurrent(info);
}
//判断是否需要滚动地图
int cx = carX * CELL_WIDTH;
int cy = carY * CELL_WIDTH;
if (cx < vwX + BORDER && vwX>=CELL_WIDTH) {
//向左滚动屏幕
vwX -= CELL_WIDTH;
layers.setViewWindow(vwX, vwY, vwWidth, vwHeight);
} else if (cx > vwX + vwWidth - BORDER
&& vwX + vwWidth<=bg.getWidth() - CELL_WIDTH) {
//向右滚动屏幕
vwX += CELL_WIDTH;
layers.setViewWindow(vwX, vwY, vwWidth, vwHeight);
}
if (cy < vwY + BORDER && vwY>=CELL_HEIGHT) {
//向上滚动屏幕
vwY -= CELL_HEIGHT;
layers.setViewWindow(vwX, vwY, vwWidth, vwHeight);
} else if (cy > 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;
try {
imgBG = Image.createImage("/res/bg.png");
imgCar = Image.createImage("/res/car.png");
} 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++) {
if (cells[y][x] == 1) {
walls.setCell(x, y, WALL);
} else {
bg.setCell(x, y, GRASS);
}
}
}
car = new Sprite(imgCar, CELL_WIDTH, CELL_HEIGHT);
car.setFrameSequence(aniSequence);
//缺省方向向右
car.setTransform(Sprite.TRANS_MIRROR);
car.setPosition(carX*CELL_HEIGHT, carY*CELL_WIDTH);
// 创建图层管理
layers = new LayerManager();
layers.append(car);
layers.append(walls);
layers.append(bg);
//设置可视窗口
layers.setViewWindow(vwX, vwY, vwWidth, vwHeight);
//创建并启动游戏线程
new Thread(this).start();
oldTime=System.currentTimeMillis();
}
public void intCells(int[] arr)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -