namastejs
Version:
A spiritual greeting from your JavaScript code. Because every function deserves a 'Namaste ๐'
94 lines (74 loc) โข 2.57 kB
JavaScript
const { style } = require("../../../theme");
/* โโโโโโโโโโโโโโโ TERMINAL HELPERS โโโโโโโโโโโโโโโ */
function resetScreen() {
process.stdout.write("\x1b[H\x1b[J");
}
function hideCursor() {
process.stdout.write("\x1b[?25l");
}
function showCursor() {
process.stdout.write("\x1b[?25h");
}
/* โโโโโโโโโโโโโโโ RENDER โโโโโโโโโโโโโโโ */
function render(state, config) {
hideCursor();
resetScreen();
const { WIDTH, HEIGHT } = config;
let out = "";
/* โโโโโ Top Border โโโโโ */
out += style.gray(`โ${"โ".repeat(WIDTH)}โ\n`);
for (let y = 0; y < HEIGHT; y++) {
out += style.gray("โ");
for (let x = 0; x < WIDTH; x++) {
const isCollision =
state.status === "GAME_OVER" &&
state.collision &&
state.collision.x === x &&
state.collision.y === y;
const isHead = state.snake[0].x === x && state.snake[0].y === y;
const isBody = state.snake.some(
(p, i) => i !== 0 && p.x === x && p.y === y
);
if (isCollision) {
out += style.bgBrightRed(style.bold("X"));
} else if (isHead) {
out += style.brightGreen(style.bold("โ"));
} else if (isBody) {
out += style.green("โ");
} else if (state.food.x === x && state.food.y === y) {
out += style.red("โ");
} else {
out += style.dim("ยท");
}
}
out += style.gray("โ\n");
}
/* โโโโโ Bottom Border โโโโโ */
out += style.gray(`โ${"โ".repeat(WIDTH)}โ\n`);
/* โโโโโ HUD โโโโโ */
out +=
style.cyan(" Score: ") +
style.brightYellow(state.score.toString()) +
style.cyan(" Speed: ") +
style.brightYellow(state.speed.toString()) +
"\n";
out += style.gray(" โ โ โ โ Move Space Pause/Resume Ctrl+C Exit");
/* โโโโโ Overlays โโโโโ */
if (state.paused) {
out += "\n\n" + style.inverse(style.bold(" โธ PAUSED "));
}
if (state.status === "GAME_OVER") {
out +=
"\n\n" +
style.bold(style.red(" ๐ GAME OVER ๐ ")) +
"\n" +
style.gold(` Final Score: ${state.score} `) +
"\n" +
style.gray(" Press Ctrl+C to exit ");
}
process.stdout.write(out);
}
module.exports = {
render,
showCursor,
};