namastejs
Version:
A spiritual greeting from your JavaScript code. Because every function deserves a 'Namaste š'
54 lines (42 loc) ⢠1.29 kB
JavaScript
const { showCursor } = require("./render");
const { style } = require("../../../theme");
function exitGame(state) {
showCursor();
process.stdout.write("\x1Bc");
console.log(style.red("\nš GAME OVER š"));
console.log(style.gold(`š Final Score: ${state.score}`));
console.log(style.cyan("š Thanks for playing Namaste Snake š\n"));
process.exit();
}
function setupInput(state) {
if (!process.stdin.isTTY) return;
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding("utf8");
process.stdin.on("data", (key) => {
if (key === "\u0003") {
exitGame(state); // Ctrl + C
}
if (state.status === "GAME_OVER") return;
if (key === " ") {
state.paused = !state.paused;
return;
}
if (state.paused) return;
switch (key) {
case "\u001B[A":
if (state.dir !== "DOWN") state.dir = "UP";
break;
case "\u001B[B":
if (state.dir !== "UP") state.dir = "DOWN";
break;
case "\u001B[D":
if (state.dir !== "RIGHT") state.dir = "LEFT";
break;
case "\u001B[C":
if (state.dir !== "LEFT") state.dir = "RIGHT";
break;
}
});
}
module.exports = { setupInput };