UNPKG

namastejs

Version:

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

94 lines (74 loc) โ€ข 2.57 kB
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, };