Input Direction confusion in snake game javascript

In summary, the inputDir variable represents the row and column coordinates of the input area, rather than a vector.
  • #1
shivajikobardan
674
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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)?
 
  • #6
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.
 
  • #7
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?
 
  • #8
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.
 
  • #9
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

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
33
Views
6K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
Back
Top