Input Direction confusion in snake game javascript

Click For Summary

Discussion Overview

The discussion revolves around the implementation of input handling in a JavaScript snake game, specifically focusing on the variable inputDir and its representation of movement direction versus grid coordinates. Participants explore the implications of using similar naming conventions for different purposes in the code.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant questions the purpose of the inputDir variable, seeking clarification on its initial assignment of { x: 0, y: 1 }.
  • Another participant suggests that inputDir could represent either "input directory" or "input direction," indicating uncertainty about its naming.
  • Concerns are raised about the logic of setting inputDir before determining which key is pressed, with suggestions for a default value when non-arrow keys are pressed.
  • Participants discuss the distinction between coordinates (e.g., (5, 6)) and movement vectors (e.g., (0, -1)), questioning the use of similar variable names for different concepts.
  • Clarifications are made regarding the terminology, with one participant noting that x and y are object keys rather than variable names, suggesting that better labeling could enhance clarity.

Areas of Agreement / Disagreement

Participants express differing views on the clarity and logic of the code structure, particularly regarding the naming conventions and the representation of movement versus position. The discussion remains unresolved with no consensus on the best approach.

Contextual Notes

There are unresolved questions about the implications of using similar naming for different concepts in programming, as well as the potential for confusion in understanding the code's functionality.

shivajikobardan
Messages
637
Reaction score
54
TL;DR
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

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K