Here’s the full code:
This is the code that I’m interested in:
window.addEventListener(“keydown”, e => {
inputDir = { x: 0, y: 1 };
moveSound.play();
switch (e.key) {
case “ArrowUp”:
inputDir.x = 0;
inputDir.y = -1;
break;
case “ArrowDown”:
inputDir.x = 0;
inputDir.y = 1;
break;
case “ArrowLeft”:
inputDir.x = -1;
inputDir.y = 0;
break;
case “ArrowRight”:
inputDir.x = 1;
inputDir.y = 0;
break;
}
})
What is inputDir variable(ofc it’s input direction)? (I didn’t write this code, I’m learning it from a tutorial, you know tutorials don’t cover stuffs well).
What I’m not understanding is why are we setting inputDir at 0th row, 1st column? What does that mean? Can you provide some explanations with figures(if possible) about this issue?
The reason for this confusion is we’re using this code earlier in same program:
let snakeArr = [
{
x: 13,
y: 15
}
]
Here x and y means grid row 13, grid column 15. (18*18 grid is used).
But in this question, we’re using x and y for different purposes, it confused me.
1 Answer