0

我正在尝试构建一个小游戏,其中有一个大网格。(10x20) 个空间和一个小网格 (3x3)。我想将小网格映射到大网格。

我必须能够为要放置的网格指定 ay/row 位置和 ax/cell 位置。

我怎样才能做到这一点?

例子:

    small grid:
    |0|1|0|
    |1|1|1|
    |0|1|0|

    large grid
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0

    result:
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|1|0|0|0|0|0
    |0|0|0|1|1|1|0|0|0|0
    |0|0|0|0|1|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0

更新 游戏是俄罗斯方块。我检查了一个小网格(砖)不能从大网格边界中出来。

        A brick holds:
        X: cell position on the large grid
        Y: row position on the large grid
        Width: the width of the small grid
        Height: the height of the small grid
        Fields: the small grid where some fields are 0 (unmarked), and some are 1 (marked)

我尝试过这样的事情:但无法让它工作

        for (var row = brick.y; row < brick.y + brick.height; row++) {
            for (var cell = brick.x; cell < brick.x + brick.width; cell++) {
                for (var fieldRow = 0; fieldRow < brick.type.fields.length; fieldRow++) {
                    for (var fieldCell = 0; fieldCell < brick.type.fields[fieldRow].length; fieldCell++) {
                        console.log(brick.type.fields[fieldRow][fieldCell]);
                        if (brick.type.fields[fieldRow][fieldCell] == 1) {
                            this.grid[row][cell] = 1;
                        }
                    }
                }
            }
        }
4

1 回答 1

1

假设您使用 2 个阵列来构建游戏,您的 3x3 阵列位于大网格的中间。

小数组的中心是smallGrid[1][1],大数组的中心是bigGrid[4][11]。让我们构建传输算法

for(i=0; i<=2; i++){
 for(j=0; j<=2; j++){
   if(smallGrid[i][j] == 1) 
    { 
      bigGrid[i+3][j+10]=1;
    } 
 }
}
于 2012-09-03T12:02:35.420 回答