1

我有一个 2D 瓷砖游戏,我正在尝试为其创建画笔大小。目前,我的代码如下所示:

if (isMouseClicked0) {
    int grid_x = Math.round(mouseX / blockSize);
    int grid_y = Math.round(mouseY / blockSize);
    for (int x = 0; x < brushSize; x++) {
        for (int y = 0; y < brushSize; y++) {
            world.setAt(grid_x, grid_y, b[inventorySelect]);
            grid_x += x;
            grid_y += y;
        }
    }
}

setAt()方法如下所示:

public void setAt(int x, int y, BlockType b) {
    if (x <= Display.getWidth() / blockSize && y <= Display.getHeight() / blockSize) {
        blocks[x][y] = new BaseBlock(b, x * blockSize, y * blockSize);
    }
    render();
}

这当前产生此输出:

瓷砖

左上角第一个图块上方的图块是我单击鼠标的位置,因此您可以看到下一个图块未渲染。我已经在这几个小时了,所以我可能错过了一些简单的东西。有什么帮助吗?

编辑:画笔大小是2,所以我应该创建四个图块。blockSize32,这是我的积木有多大。

4

1 回答 1

1

问题在于:

grid_x += x;
grid_y += y;

你基本上是在对角线移动。这可能会更好:

    for (int x = 0; x < brushSize; x++) {
        for (int y = 0; y < brushSize; y++) {
            world.setAt(grid_x + x, grid_y + y, b[inventorySelect]);
        }
    }
于 2013-03-13T01:55:14.903 回答