Javascript - 俄罗斯方块 - 如何转换块

时间:2017-12-07 09:54:29

标签: javascript html

我正和朋友一起做Tetris游戏。我们怎样才能扭转这些障碍?

我们知道如何计算块的位置,但我们不知道如何将其添加到代码中:

i' = a * i + b * j

j' = c * i + d * j

newJ = -i;

newI =  j;

我们想到的是添加阵列,但正如我所说,我们不确定......如何启动它

到目前为止,这是我们的代码:

function stylingSquares() {
  var i;
  var j;

  for (i = 1; i > -2; i--) {
    for (j = -1; j < 2; j++) {
      var myDiv = document.createElement("div");
      
      myDiv.style.width = "100px";
      myDiv.style.height = "100px";
      myDiv.style.backgroundColor = "black";

      myDiv.setAttribute("class", "quad");
      document.getElementById("game").appendChild(myDiv);

      if ((i === 0 && j === 0) ||
        (i === 1 && j === -1) ||
        (i === 0 && j === -1) ||
        (i === 0 && j === 1) ||
        (i === 0 && j === 0)) {
        myDiv.style.backgroundColor = "red";
      } else {

      }
    }
  }

  setInterval(function() {
    document.body.onkeyup = function(e) {
      if (e.keyCode == 32) {


      }
    }
  }, 300);
}

stylingSquares();
.quad {
  float: left;
  margin: 1 1 1 1;
}

#game {
  position: relative;
  width: 308px;
  height: 308px;
  background-color: black;
}
<div id="game"></div>

1 个答案:

答案 0 :(得分:0)

我会创建一个游戏区域的2D数组(以跟踪当前棋子,已经放置的块,边框),为当前棋子选择一个旋转点,最后尝试移动棋子的块在这一点上,1或2&#34;步骤&#34;一次(根据需要)。

enter image description here

如果新位置与阵列中的其他位置发生碰撞,请尝试向相反方向移动该位置一步。如果那不可能,那么这件作品就无法旋转。

相关问题