Untitled

mail@pastecode.io avatar
unknown
javascript
3 years ago
1.1 kB
1
Indexable
Never
async function wrapper() {
  //await getInput();
  keypress(process.stdin);
  process.stdin.setRawMode(true);
  let game = new Game(20, 40)
  start(game);
}

const start = (game: Game) => {  
  // prompts is blocking ctrl+c event, so let's listen for it manually
  process.stdin.on("keypress", (str, key) => {
    if (key.ctrl && key.name === "c") {
      console.log(chalk.yellow("Bye!"));
      process.exit();
    }
  });

  let direction: Direction = "Right";

  process.stdin.on("keypress", function (ch, key) {
    switch (key.name) {
      case "up":
        if (direction == "Down") { break; }
        direction = "Up";
        break;
      case "right":
        if (direction == "Left") { break; }
        direction = "Right";
        break;
      case "left":
        if (direction == "Right") { break; }
        direction = "Left";
        break;
      case "down":
        if (direction == "Up") { break; }
        direction = "Down";
        break;
      default:
        break;
    }
  });

  setInterval(() => {
    game.tick(direction)
    draw(direction, game)    
  }, 1000 / FRAMES_PER_SECOND);
}

wrapper();