创建一个数组Javascript突破游戏

时间:2022-01-20 16:58:36

I'm creating a simple breakout clone using HTML5 Canvas and have come across the loadHitGrid() function, but I'm having difficulty understanding what it does.

我正在使用HTML5 Canvas创建一个简单的突破克隆,并遇到了loadHitGrid()函数,但我很难理解它的作用。

It looks like its creating arrays within the hitgrid array and then filling this with 1's

看起来它在hitgrid数组中创建数组然后用1填充它

Can someone help or draw this out?

有人可以帮忙或者说出来吗?

function loadHitGrid() {
  for (var i = 0; i < NUM_ROWS; i++) {
    hitGrid[i] = new Array;

    for (var j = 0; j < NUM_COLS; j++) {
      hitGrid[i][j] = 1;
    }
  }

}


//Can i replace hitGrid with the following?


hitGrid = [
  1, 1, 1, 1, 1, // is this the same as the above????
  1, 1, 1, 1, 1,
  1, 1, 1, 1, 1,
  1, 1, 1, 1, 1,
  1, 1, 1, 1, 1
]





function drawblocks() {
    for (var i = 0; i < NUM_ROWS; i++) { // loops  trough number of rows
      for (var j = 0; j < NUM_COLS; j++) { // loops thgrough number of cols
        if (hitGrid[i][j] == 1) { // for each row / col check for 1
          ctx.beginPath(); // Satrts a new path used when drawing!
          ctx.fillStyle = colours[i];
          ctx.fillRect(j * (blockW + SPACING) + SPACING, 
                       i * (blockH + SPACING) + SPACING, blockW, blockH);
        }
      }
    }

2 个解决方案

#1


0  

This would be :

这将是:

hitGrid = [[1, 1, 1, 1, 1], // is this the same as the above????
           [1, 1, 1, 1, 1], 
           [1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1], 
           [1, 1, 1, 1, 1]];

Of course that means that NUM_ROWS, and NUM_COLUMNS is 5 :)

当然这意味着NUM_ROWS和NUM_COLUMNS是5 :)

#2


0  

Check out my comments on the code below:

看看我对以下代码的评论:

     function loadHitGrid () {     
          for(var i = 0 ; i < NUM_ROWS ; i ++) {
              hitGrid[i] = new Array; //Creating an empty array NUM_ROWS amount of times

              for(var j = 0; j < NUM_COLS; j ++) {
                   hitGrid[i][j] = 1 ; //Populating each newly created empty array with NUM_COLS amount of ones.
              }
          }
      }

So in NUM_ROWS and NUM_COLS both equal 5 and assuming hitGrid was an empty array, the output would look more like:

所以在NUM_ROWS和NUM_COLS都等于5并假设hitGrid是一个空数组,输出看起来更像:

[[1,1,1,1,1],
 [1,1,1,1,1],
 [1,1,1,1,1],
 [1,1,1,1,1]]

#1


0  

This would be :

这将是:

hitGrid = [[1, 1, 1, 1, 1], // is this the same as the above????
           [1, 1, 1, 1, 1], 
           [1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1], 
           [1, 1, 1, 1, 1]];

Of course that means that NUM_ROWS, and NUM_COLUMNS is 5 :)

当然这意味着NUM_ROWS和NUM_COLUMNS是5 :)

#2


0  

Check out my comments on the code below:

看看我对以下代码的评论:

     function loadHitGrid () {     
          for(var i = 0 ; i < NUM_ROWS ; i ++) {
              hitGrid[i] = new Array; //Creating an empty array NUM_ROWS amount of times

              for(var j = 0; j < NUM_COLS; j ++) {
                   hitGrid[i][j] = 1 ; //Populating each newly created empty array with NUM_COLS amount of ones.
              }
          }
      }

So in NUM_ROWS and NUM_COLS both equal 5 and assuming hitGrid was an empty array, the output would look more like:

所以在NUM_ROWS和NUM_COLS都等于5并假设hitGrid是一个空数组,输出看起来更像:

[[1,1,1,1,1],
 [1,1,1,1,1],
 [1,1,1,1,1],
 [1,1,1,1,1]]