📄 mapcontainer.java
字号:
package pack05;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
/**
* 坦克游戏地图。其中包含若干个单元素平铺层。
* @author 孙继磊
*
*/
public class MapContainer {
//地图上的元素类型
public static final int NOTHING=0;
public static final int GRASS=1;
public static final int WATER=2;
public static final int STEEL=3;
public static final int BRICK=4;
//元素类型数量(不包括空白)
public static final int MapObjectTypeNum=4;
//地图大小,行数,列数
public static final int ROWS=18;
public static final int COLUMNS=15;
//地图中单元格的宽和高
public static final int CellWidth=16;
public static final int CellHeight=16;
//地图中包含的背景层
private GameMap[] layers;
//地图元素数组
private int[][][] mapArray=
{{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0 },
{ 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0 },
{ 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0 },
{ 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0 },
{ 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0 },
{ 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0 },
{ 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 3, 0, 4, 4, 4, 0, 4, 4, 4, 0, 4, 4, 4, 0, 3 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 4, 0, 4, 0, 0, 4, 0, 4, 0, 0, 4, 0, 4, 0 },
{ 0, 4, 0, 4, 0, 0, 4, 3, 4, 0, 0, 4, 0, 4, 0 },
{ 0, 4, 0, 4, 0, 0, 4, 0, 4, 0, 0, 4, 0, 4, 0 },
{ 0, 4, 0, 4, 0, 0, 4, 0, 4, 0, 0, 4, 0, 4, 0 },
{ 0, 4, 0, 4, 0, 0, 0, 0, 0, 0, 0, 4, 0, 4, 0 },
{ 0, 4, 0, 4, 0, 0, 4, 4, 4, 0, 0, 4, 0, 4, 0 },
{ 0, 0, 0, 0, 0, 0, 4, 0, 4, 0, 0, 0, 0, 0, 0 }
}};
public MapContainer()
{
layers=new GameMap[MapObjectTypeNum];
for(int i=0;i<MapObjectTypeNum;i++)
{
layers[i]=new GameMap(i+1);
}
}
/**
* 加载某一关地图
* @param level 关卡
*/
public void loadMap(int level)
{
int type;
for(int i=0;i<ROWS;i++)
for(int j=0;j<COLUMNS;j++)
{
type=mapArray[level -1][i][j];
//把地图元素放到对应的层中
if(type>0)
layers[type-1].setCell(j, i, type);
}
}
/**
* 得到地图宽度
* @return 地图宽度
*/
public static int getWidth(){return CellWidth*COLUMNS;}
/**
* 得到地图高度
* @return 地图高度
*/
public static int getHeight(){return CellHeight*ROWS;}
public TiledLayer[] getLayers(){return layers;}
/**
* 检查指定精灵是否与地图中某元素碰撞
* @param s 精灵
* @return 若发生碰撞,则返回碰撞的元素类型,否则返回0
*/
public int checkCollide(Sprite s)
{
for(int i=0;i<layers.length;i++)
if(s.collidesWith(layers[i], false))
return layers[i].getElementtype();
return 0;
}
/**
* 检测指定精灵是否超出地图边界
* @param s 精灵
* @return 是否越界
*/
public boolean isOutOfBorder(Sprite s)
{
boolean b=(s.getX()<0 || s.getX()+s.getWidth()>MapContainer.getWidth()
||s.getY()<0 || s.getY()+s.getHeight()>MapContainer.getHeight());
return b;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -