0

这是我的数组:

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));
}}

我想用这个代码为每个图块分配一个矩形(我知道它错了):

public static Rectangle blockBounds(int x, int y) {
    return(new Rectangle(map[0][x], map[y][0], 25, 25));
}

如何获取瓦片的 x 和 y 坐标并将它们放入矩形代码中?

4

2 回答 2

0

我认为你需要这样的东西:

new Rectangle(x * TILE_SIZE, Y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
于 2012-11-09T01:32:53.713 回答
0

我想你正在使用,

private static int[][] map = new int[WIDTH][HEIGHT];

代表您的地图。所以地图的每个`{x,y} 代表地图上的一个点。Rectangle类的构造函数,Rectangle(int x, int y, int width, int height) 用于构造一个新的Rectangle,其左上角指定为(x,y),其宽高由参数指定同名。所以我认为这很容易,

new Rectangle(x, y, TILE_SIZE, TILE_SIZE)

此外,要使用此构造函数,您必须从 {0,0} 开始导航您的地图

于 2012-11-09T02:12:27.033 回答