在大网格上映射小网格

Mapping a small grid on a large grid

本文关键字:网格 映射      更新时间:2023-09-26

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

我必须能够为要放置的网格指定y/行位置和x/单元格位置。

我该怎么做?

示例:

    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;
                        }
                    }
                }
            }
        }

假设您使用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;
    } 
 }
}