📄 map.java
字号:
package snake;
/*
* Map.java
*
* Created on 2007年11月26日, 下午3:50
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.lcdui.game.Sprite;
/**
*
* @author xiaoxin
*/
public class Map {
//地图层
private short data[][][];
//逻辑层
private byte logicData[][];
private Image mapImage;
public static int tileWidth;
public static int tileHeight;
public static int column;
public static int row;
/** Creates a new instance of Map */
public Map(Image img) {
this.mapImage = img;
}
public void loadMap(DataInputStream dis) throws Exception{
int layerNum = dis.readByte();
column = dis.readByte();
row = dis.readByte();
tileWidth = dis.readByte();
tileHeight = dis.readByte();
data = new short[layerNum][row][column];
logicData = new byte[row][column];
for(int i = -1; ++i < layerNum;){
for(int j = -1; ++j < row;){
for(int h = -1; ++h < column;){
data[i][j][h] = dis.readShort();
}
}
}
for(int i = -1; ++i < row;){
for(int j = -1; ++j < column;){
logicData[i][j] = dis.readByte();
}
}
}
public int getLogicCode(int row, int col){
return logicData[row][col];
}
//后面两个参数为视口坐标(看不到得地方不绘制)
//未用卡马克缓冲,需要的请自己修改
/* public void render(Graphics g, int showX, int showY){
int row = showY / tileHeight;
int col = showX / tileWidth;
int wSize = myCanvas.ScreenWidth / tileWidth + 2;
int hSize = myCanvas.ScreenHeight / tileHeight + 2;
int drawX = -showX;
int drawY = -showY;
for(int h = -1; ++h < data.length;){
for(int i = row - 1; ++i < row + hSize;){
for(int j = col - 1; ++j < col + wSize;){
if(i >= data[h].length || j >= data[h][i].length || i < 0 || j < 0){
continue;
}
short id = data[h][i][j];
if(id != -1){
int clipX = id % (mapImage.getWidth() / tileWidth) * tileWidth;
int clipY = id / (mapImage.getHeight() / tileHeight) * tileHeight;
g.drawRegion(mapImage, clipX, clipY, tileWidth, tileHeight, Sprite.TRANS_NONE, drawX + j * tileWidth, drawY + i * tileHeight, 0);
}
}
}
}*/
public void render(Graphics g, int showX, int showY){
int row = showY / tileHeight;
int col = showX / tileWidth;
int wSize = myCanvas.ScreenWidth / tileWidth + 1;
int hSize = myCanvas.ScreenHeight / tileHeight + 1;
int drawX = 0;
int drawY = 0;
for(int h = -1; ++h < data.length;){
drawY = -(showY % tileHeight);
for(int i = row - 1; ++i < row + hSize;){
drawX = -(showX % tileWidth);
for(int j = col - 1; ++j < col + wSize;){
if(i >= data[h].length || j >= data[h][i].length || i < 0 || j < 0){
continue;
}
short id = data[h][i][j];
if(id != -1){
//int clipX = id % (mapImage.getWidth() / tileWidth) * tileWidth;
//int clipY = id / (mapImage.getHeight() / tileHeight) * tileHeight;
int clipX = id % (mapImage.getWidth() / tileWidth) * tileWidth;
int clipY = id / (mapImage.getWidth() / tileWidth) * tileHeight;
g.drawRegion(mapImage, clipX, clipY, tileWidth, tileHeight, Sprite.TRANS_NONE, drawX, drawY, 0);
}
drawX += tileWidth;
}
drawY += tileHeight;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -