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
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.
 

1. What is input direction confusion in snake game javascript?

Input direction confusion in snake game javascript is a common issue where the direction of the snake's movement does not match the input from the player. This can happen due to various reasons such as incorrect coding, processing delays, or conflicting input commands.

2. How does input direction confusion affect the gameplay?

Input direction confusion can significantly impact the gameplay experience. It can cause the snake to move in unexpected directions, making it difficult for players to control and progress in the game. This can lead to frustration and dissatisfaction with the game.

3. What are some common causes of input direction confusion in snake game javascript?

Some common causes of input direction confusion in snake game javascript include incorrect coding of input commands, processing delays in the game, and conflicting input commands. It can also be caused by bugs or errors in the code.

4. How can input direction confusion be fixed in snake game javascript?

To fix input direction confusion in snake game javascript, the code needs to be carefully examined to identify and fix any errors. It is also important to optimize the code for smoother processing and to avoid conflicting input commands. Regular testing and debugging can also help in preventing input direction confusion.

5. Can input direction confusion be prevented in snake game javascript?

While it is difficult to completely prevent input direction confusion in snake game javascript, it can be minimized by writing efficient and bug-free code, optimizing the game's processing, and avoiding conflicting input commands. Regular testing and debugging can also help in identifying and fixing any potential issues before they affect the gameplay.

Suggested for: Input Direction confusion in snake game javascript

Replies
2
Views
931
Replies
9
Views
936
Replies
1
Views
965
Replies
4
Views
911
Replies
5
Views
1K
Replies
9
Views
1K
Replies
1
Views
685
Replies
7
Views
935
Replies
6
Views
874
Back
Top