坦克老巢的绘制
在map包中创建TankHouse类
package map;
import util.Constant;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
/**
* 玩家的大本营
*/
public class TankHouse {
//老巢的坐标
public static final int HOUSE_X = (Constant.FRAME_WIDTH - 3*MapTile.tileW >> 1) + 2;
public static final int HOUSE_Y = Constant.FRAME_HEIGHT - 2*MapTile.tileW ;
//一共六块地图块
private List<MapTile> tiles = new ArrayList<>();
public TankHouse(){
tiles.add(new MapTile(HOUSE_X,HOUSE_Y));
tiles.add(new MapTile(HOUSE_X,HOUSE_Y + MapTile.tileW));
tiles.add(new MapTile(HOUSE_X + MapTile.tileW,HOUSE_Y));
tiles.add(new MapTile(HOUSE_X + MapTile.tileW * 2,HOUSE_Y));
tiles.add(new MapTile(HOUSE_X + MapTile.tileW * 2,HOUSE_Y + MapTile.tileW));
tiles.add(new MapTile(HOUSE_X + MapTile.tileW,HOUSE_Y + MapTile.tileW));
}
public void draw(Graphics g){
for (MapTile tile : tiles) {
tile.draw(g);
}
}
public List<MapTile> getTiles() {
return tiles;
}
}
在GameMap类中调用
TankHouse house;
public GameMap(){
initMap();
}
private void initMap() {
final int count =20;
for (int i = 0; i < count; i++) {
MapTile mapTile = MapTilePool.get();
int x = MyUtil.getRandomNumber(MAP_X,MAP_X+MAP_WIDTH-MapTile.tileW);
int y = MyUtil.getRandomNumber(MAP_Y,MAP_X+MAP_HEIGHT-MapTile.tileW);
if(isCollide(tiles,x,y))
{
i--;
continue;
}
mapTile.setX(x);
mapTile.setY(y);
tiles.add(mapTile);
}
house=new TankHouse();
addHouse();
}
//将老巢的所有元素添加到地图的容器中
private void addHouse(){
tiles.addAll(house.getTiles());
}