【问题标题】:Filling a square with tiles用瓷砖填充正方形
【发布时间】:2013-03-13 01:43:09
【问题描述】:

我有一个 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,这是我的积木有多大。

【问题讨论】:

    标签: java opengl lwjgl tiles


    【解决方案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]);
            }
        }
    

    【讨论】:

    • 哇哦。我不敢相信我没想到!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2018-05-28
    • 2021-04-01
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多