Java菱形等距地图瓷砖选择

时间:2014-10-31 14:41:10

标签: java cursor mouse isometric

我用Java编写了一个等距游戏,但是当我想要获得那个鼠标指向的磁贴时我就卡住了。

有没有办法计算呢?

这是渲染的代码

int posX = 10;
int posY = 0;      
static int beginY = 500-72;
static int beginX = 800-36;
static int newLineY = 60;
static int newLineX = 200;    

posY = ((0+1)*9)-9+beginY/2;
posX = ((beginX)/2)+18-(18*(0+1));

for (int y = 0; y <= world.length-1; y++) {
    for (int x = 0; x <= world[y].length-1; x++) {
        if (world[y][x] == 1) {
            g.drawImage(grass, posX, posY, null);
            posX += 18;
            posY += 9;
        } else if (world[y][x] == 2) {
            g.drawImage(wall, posX, posY-38, null);
            posX += 18;
            posY += 9;
        } else if (world[y][x] == 3) {
            g.drawImage(stone, posX, posY, null);
            posX += 18;
            posY += 9;
        } else if (world[y][x] == 4) {
            g.drawImage(water, posX, posY, null);
            posX += 18;
            posY += 9;
        } else {
            posX += 18;
            posY += 9;
        }
        if ((y-pPosX) * (y-pPosX) + (x-pPosY) * (x-pPosY) <= 3*3 && world[y][x] != 0 && world[y][x] != 2 && world[y][x] != 4) {
            g.drawImage(hollow, posX-18, posY-9, null);
        }
        if (y == selectedX && x == selectedY) {
            g.drawImage(selected, posX-18, posY-9, null);
        }
        if (world2[y][x] == 9) {
            g.drawImage(character, posX-18, posY-53, null);
        }
    }
    posY = ((y+1)*9)+beginY/2;
    posX = ((beginX)/2)-(18*(y+1));
}

这是结果。 beginXbeginY从钻石的顶角开始:

isometric tile map

2 个答案:

答案 0 :(得分:0)

以下是渲染的代码

int posX = 10;
int posY = 0;      
static int beginY = 500-72;
static int beginX = 800-36;
static int newLineY = 60;
static int newLineX = 200;    

posY = ((0+1)*9)-9+beginY/2;
posX = ((beginX)/2)+18-(18*(0+1));
for (int y = 0; y <= world.length-1; y++) {
for (int x = 0; x <= world[y].length-1; x++) {
    if (world[y][x] == 1) {
        g.drawImage(grass, posX, posY, null);
        posX += 18;
        posY += 9;
    } else if (world[y][x] == 2) {
        g.drawImage(wall, posX, posY-38, null);
        posX += 18;
        posY += 9;
    } else if (world[y][x] == 3) {
        g.drawImage(stone, posX, posY, null);
        posX += 18;
        posY += 9;
    } else if (world[y][x] == 4) {
        g.drawImage(water, posX, posY, null);
        posX += 18;
        posY += 9;
    } else {
        posX += 18;
        posY += 9;
    }
    if ((y-pPosX) * (y-pPosX) + (x-pPosY) * (x-pPosY) <= 3*3 && world[y][x] != 0 && world[y][x] != 2 && world[y][x] != 4) {
        g.drawImage(hollow, posX-18, posY-9, null);
    }
    if (y == selectedX && x == selectedY) {
        g.drawImage(selected, posX-18, posY-9, null);
    }
    if (world2[y][x] == 9) {
        g.drawImage(character, posX-18, posY-53, null);
    }
}
posY = ((y+1)*9)+beginY/2;
posX = ((beginX)/2)-(18*(y+1));
}

答案 1 :(得分:0)

首先将MouseListener注册到面板(要绘制图块的面板)。 现在,根据您的渲染代码,您实际上是&#34; 知道&#34;每个瓷砖开始的位置。所以你要做的就是计算点击了哪个图块。

JPanel gamePanel = new JPanel();
gamePanel.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent event) {
        int x=event.getX();
        int y=event.getY();
        System.out.println("clicked at ("+x+","+y+")");  //these co-ords are relative to the your gamePanel           
    }
});

修改

我改为上面的代码,它不正确。但我相信你会弄明白的。因此,上面的代码显示了如何向面板/框架添加MouseListener。如果它有效,您应该能够在控制台中看到您单击的位置。

现在您只需要计算点击了哪个图块:

(x,y) --> + - - - 
          '    /\
          '  /    \
          '/        \
           \        /
             \    /
               \/     + <-- (x+36, y+18)

您的鼠标指针需要介于两个点(x,y)(x+36, y+18)之间。 当然有一个额外的问题,有人可以点击(x,y)和(y + 36,y + 18)之间的某个地方,但仍然没有点击瓷砖(可能有人点击它左上方的瓷砖),那里有你可能需要决定您要选择哪个图块。

另一种方法是将您的面板划分为一组正方形。但当然没有画出它们。想象一下,每个瓷砖中间都可以有一个正方形:

               /\
             /____\
           / |    | \
           \ |____| /
             \    /
               \/    
这样的事情。所以你只需要计算是否有人在广场内点击了。