Java Input Direction confusion in snake game javascript

AI Thread Summary
The discussion centers on the confusion surrounding the `inputDir` variable in a JavaScript snake game code snippet. Participants clarify that `inputDir` represents a movement vector, not grid coordinates, which is why it is initialized to `{ x: 0, y: 1 }` for downward movement. The use of the same keys `x` and `y` for both the direction vector and the snake's coordinates is highlighted as a source of confusion, emphasizing that they serve different purposes. Suggestions are made to improve variable naming for clarity, distinguishing between movement vectors and positional coordinates. Overall, the conversation helps clarify the distinction between these concepts in coding.
shivajikobardan
Messages
637
Reaction score
54
TL;DR Summary
javascript snake game
Here's the full code:



This is the code that I'm interested in:
JavaScript:
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? (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?
 
Last edited by a moderator:
Technology news on Phys.org
shivajikobardan said:
What is inputDir variable?
What do you think it could be: inputDirectory? inputDirection?

shivajikobardan said:
What I'm not understanding is why are we setting inputDir at 0th row, 1st column?
You need to make a better guess if you think that inputDir represents row and column coordinates (rather than a vector). Anyway the code is terrible - among other things it makes no sense to set inputDir before you have worked out what key is pressed.
 
pbuk said:
You need to make a better guess if you think that inputDir represents row and column coordinates (rather than a vector). Anyway the code is terrible - among other things it makes no sense to set inputDir before you have worked out what key is pressed.
You might want to have a default value for inputdir, if a non-arrowkey is pressed. x=0, y=0 (for no movement) would probably make more sense.
 
pbuk said:
You need to make a better guess if you think that inputDir represents row and column coordinates (rather than a vector). Anyway the code is terrible - among other things it makes no sense to set inputDir before you have worked out what key is pressed.
But,
Code:
let snakeArr = [
  {
    x: 13,
    y: 15
  }
]
here, x and y represented the location of grid. So, should not here x,y mean the same? I am not getting it.
 
shivajikobardan said:
I am not getting it.
Really? Do you understand the difference between saying an item is at (5, 6) (coordinates) and an item moves by (0, -1) (a vector)?
 
pbuk said:
Really? Do you understand the difference between saying an item is at (5, 6) (coordinates) and an item moves by (0, -1) (a vector)?
but why are we using same variable names for 2 different things? is the question.
 
shivajikobardan said:
but why are we using same variable names for 2 different things? is the question.
I don't understand what you mean: what name are you referring to?
 
shivajikobardan said:
But,
Code:
let snakeArr = [
  {
    x: 13,
    y: 15
  }
]
here, x and y represented the location of grid. So, should not here x,y mean the same? I am not getting it.
this x,y and x,y of inputDir.
 
pbuk said:
Really? Do you understand the difference between saying an item is at (5, 6) (coordinates) and an item moves by (0, -1) (a vector)?
exactly that difference is what I'm talking about. Why are we able to use same variable name for 2 different things, one is (5,6) location, another is movement by (0,-1)..
 
  • #10
x and y are not variable names, they are object keys (aka property names). It would help if you used better labels and then it would be clear that
JavaScript:
directionVector.x; // is the x-component of the direction vector; and
snakeCoordinates[0].x // is the x-coordinate of the head of the snake.
 
  • #11
pbuk said:
x and y are not variable names, they are object keys (aka property names). It would help if you used better labels and then it would be clear that
JavaScript:
directionVector.x; // is the x-component of the direction vector; and
snakeCoordinates[0].x // is the x-coordinate of the head of the snake.
yeaah I now kinda get it.
 
  • #12
pbuk said:
x and y are not variable names, they are object keys (aka property names). It would help if you used better labels and then it would be clear that
JavaScript:
directionVector.x; // is the x-component of the direction vector; and
snakeCoordinates[0].x // is the x-coordinate of the head of the snake.
thanks for the info.
 

Similar threads

Back
Top