?? 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 + -