UNPKG

namastejs

Version:

A spiritual greeting from your JavaScript code. Because every function deserves a 'Namaste šŸ™'

54 lines (42 loc) • 1.29 kB
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 };