0

我制作的这段代码成功地在 800x600px 的板上创建了一个 16x12 的 50x50 方格的网格。如您所见,玩家移动到玩家鼠标点击的坐标。

网格的每个方格上都有一个 Felt(字段)对象,可以是 laast(锁定)。如果字段锁定属性设置为 1,则不应将播放器移动到该位置。

我如何检测玩家试图继续前进以实现这一目标的领域?

public class SimpleGame extends BasicGame{

    private Image plane;
    private float planeX;
    private float planeY;

    public SimpleGame()
    {
        super("SpilTest");
    }

    @Override
    public void init(GameContainer gc) throws SlickException {
        plane = new Image("figur.png");
    }

    @Override
    public void update(GameContainer gc, int delta) throws SlickException {
        Input input = gc.getInput();

        if (input.isMousePressed(input.MOUSE_LEFT_BUTTON)) {
            this.planeX = input.getMouseX() - 30;
            this.planeY = input.getMouseY() - 50;
        }

    }

    public void render(GameContainer gc, Graphics g) throws SlickException {

        Felt board[][] = nytGrid();

        int distancex = 0;
        int distancey = 0;
        int counter = 0;

        for (int i=0; i < board.length ; i++) {
            for (int j=0; j < board[i].length ; j++) {                
                if (board[i][j].getLaast() == 1) {
                    g.setColor(Color.red);
                    g.fillRect(distancex, distancey, 50, 50);
                }

                distancex += 50;
                counter++;
                if (counter == 16) {
                    distancey += 50;
                    distancex = 0;
                    counter = 0;
                }
            }
        }

        g.drawImage(plane, planeX, planeY);

    }

    public static void main(String[] args) throws SlickException {

         AppGameContainer app = new AppGameContainer(new SimpleGame());

         app.setDisplayMode(800, 600, false);
         app.setTargetFrameRate(60);
         app.start();
    }

    public Felt[][] nytGrid() {

        Felt [][] board = new Felt[16][12];        

        for (int i=0; i < board.length ; i++) {
            for (int j=0; j < board[i].length ; j++) {
                int x = i;
                int y = j;
                board[i][j] = new Felt(x, y);

                if (i == 5 && j == 5) {
                    board[i][j].setLaast(1);
                }
            }
        }

        return board;
    }
}
4

2 回答 2

0

首先,您可能应该在 init() 方法中初始化板而不是渲染,因此不必每帧都这样做,并将网格的声明移动到类中的平面、平面 X 和平面 Y 声明旁边.

现在要禁止移动到锁定的正方形,首先添加一个方法来检查某个坐标处的正方形是否被锁定,因此类似于:

private boolean isLocked(int x, int y) {
    int square = board[x/50][y/50];
    if (square == 1) return true;
    else return false;
}

接下来修改 update() 方法中更新平面坐标的部分,模糊如下:

if (input.isMousePressed(input.MOUSE_LEFT_BUTTON)) {
        int destX = input.getMouseX() - 30;
        int destY = input.getMouseY() - 50;
        if (!isLocked(destX, destY)) {
                   this.planeX = destX;
                   this.planeY = destY;     
        }
}
于 2012-12-21T09:34:30.863 回答
0

这简单!

int mx = Mouse.getX();
int my = Mouse.getY();

但是,它为您提供了世界坐标,您必须将其转换为像素:

int mx = Mouse.getX();
int my = Mouse.getY() * -1 + (Window.WIDTH / 2) + 71;
于 2015-04-02T15:42:09.900 回答