Java 从二维数组中获取值
这是我的阵列:Java 从二维数组中获取值,java,arrays,tiles,Java,Arrays,Tiles,这是我的阵列: private static int[][] map = new int[WIDTH][HEIGHT]; 我使用以下类生成平铺贴图: public class Map { public static final int CLEAR = 0; public static final int STONE = 1; public static final int GRASS = 2; public static final int DIRT = 3; public static
private static int[][] map = new int[WIDTH][HEIGHT];
我使用以下类生成平铺贴图:
public class Map {
public static final int CLEAR = 0;
public static final int STONE = 1;
public static final int GRASS = 2;
public static final int DIRT = 3;
public static final int WIDTH = 32;
public static final int HEIGHT = 24;
public static final int TILE_SIZE = 25;
private static int[][] map = new int[WIDTH][HEIGHT];
Image air, grass, stone, dirt;
Random rand = new Random();
public Map() {
/* default map */
/*for(int y = 0; y < WIDTH; y++){
map[y][y] = (rand.nextInt(2));
System.out.println(map[y][y]);
}*/
for (int y = 18; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
map[x][y] = STONE;
}
}
for (int y = 18; y < 19; y++) {
for (int x = 0; x < WIDTH; x++) {
map[x][y] = GRASS;
}
}
for (int y = 19; y < 20; y++) {
for (int x = 0; x < WIDTH; x++) {
map[x][y] = DIRT;
}
}
try {
init(null, null);
} catch (SlickException e) {
e.printStackTrace();
}
render(null, null, null);
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
air = new Image("res/air.png");
grass = new Image("res/grass.png");
stone = new Image("res/stone.png");
dirt = new Image("res/dirt.png");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) {
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
switch (map[x][y]) {
case CLEAR:
air.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
break;
case STONE:
stone.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
break;
case GRASS:
grass.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
break;
case DIRT:
dirt.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
break;
}
}
}
}
public boolean blocked(float x, float y) {
return map[(int) x][(int) y] == STONE;
}
public static Rectangle blockBounds(int x, int y) {
return(new Rectangle(map[0][x], map[y][0], 25, 25));
}}
如何获取瓷砖的x和y坐标并将其放入矩形代码中?我认为您需要这样的东西:
new Rectangle(x * TILE_SIZE, Y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
我想你在用
private static int[]map=new int[WIDTH][HEIGHT]代码>
来表示你的地图。所以地图的每个{x,y}代表地图上的一个点。矩形类Rectangle(intx,inty,intwidth,intheight)的构造函数用于构造一个新矩形,其左上角指定为(x,y),其宽度和高度由同名参数指定。所以我认为这很容易做到
new Rectangle(x, y, TILE_SIZE, TILE_SIZE)
同样要使用这个构造函数,你必须从{0,0}开始地图导航,所以我的代码应该是这样的publicstaticrectangleblockbounds(intx,inty){return(newrectangle(x,y,TILE_SIZE,TILE_SIZE))}
我该如何实际执行冲突检测呢?我知道你必须使用。相交,但我该如何设置方法?我不明白你的问题?你的意思是你不知道你指定的t点的矩形瓷砖是否与其他点相交?
new Rectangle(x, y, TILE_SIZE, TILE_SIZE)