Discussion

Input Direction confusion in snake game JavaScript?

Here's the full code:

https://codepen.io/pelko/pen/jOpBmPN

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? (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?
2 points shivajikobardan 3 years ago 6 comments

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton