LoginSignup
metaton
@metaton (metaton Go)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

[javascript] ブロックが横に移動しない誰か助けて

解決したいこと

現在javascriptでテトリスを作っていたところなぜかブロックが横に移動ができずネットでも分からなかったので質問させて頂きました

該当するソースコード

const FIELD_COL = 10;
const FIELD_ROW = 20;
let canvas = document.getElementById("can");
let ctx = canvas.getContext("2d");
const BLOCK_SIZE = 30;
const TETRO_SIZE = 4;
let tetro = [
  [0,0,0,0],
  [1,1,0,0],
  [0,1,1,0],
  [0,0,0,0]
]
let tetro_x = 0;
let tetro_y = 0;

function drawTetro(){
  ctx.clearRect(0,0,canvas.width,canvas.height);
  for (let y=0; y<TETRO_SIZE;y++){
    for (let x=0; x<TETRO_SIZE; x++){
      if(tetro[y][x]){
        let px = (tetro_x + x) * BLOCK_SIZE;
        let py = (tetro_y + y) * BLOCK_SIZE;
        ctx.fillStyle="red";
        ctx.fillRect(px,py,BLOCK_SIZE , BLOCK_SIZE);
        ctx.strokeStyle="black";
        ctx.strokeRect(px,py,BLOCK_SIZE,BLOCK_SIZE);
      }
    }
  }
}



document.onkeydown = function(e){
  switch(e.key){
    case "ArrowLeft"://Left
      tetro_x--;
      break;
    case "ArrowUp"://Up
      tetro_y--;
      break;
    case "ArrowLight"://Light
      tetro_x++;
      break;
    case "ArrowDown"://Down
      tetro_y++;
      break;
    case "Space"://Space
      break;
  }
  drawTetro();
}
drawTetro();

自分で試したこと

コードを片っ端から見直していろいろと調べたりもしましたが分かりませんでした。

0

1Answer

Comments

  1. @metaton

    Questioner
    回答ありがとうございます。
    とても初歩的なミスをしてしまいました。
    おかげさまでちゃんと動きましたありがとうございます。

Your answer might help someone💌