HelioAI's picture
LFM3-1.7B inference modules: experimental release
67ebcef verified
Raw History Blame Contribute Delete
4.02 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Snake</title>
<style>
html,body{margin:0;height:100%;background:#101814;font-family:"Segoe UI",sans-serif;color:#d7f5e0}
body{display:grid;place-items:center}
.wrap{text-align:center}
.hud{display:flex;justify-content:space-between;width:600px;margin:0 auto 10px;font-size:18px}
canvas{background:#16241c;border-radius:12px;box-shadow:0 0 0 3px #24402f,0 20px 50px rgba(0,0,0,.5)}
.hint{opacity:.6;font-size:13px;margin-top:10px}
</style>
</head>
<body>
<div class="wrap">
<div class="hud"><span>Score: <b id="score">0</b></span><span>Best: <b id="best">0</b></span></div>
<canvas id="c" width="600" height="420"></canvas>
<div class="hint">Arrow keys to steer · Space to restart</div>
</div>
<script>
const C = 20, COLS = 30, ROWS = 21;
const ctx = document.getElementById('c').getContext('2d');
const scoreEl = document.getElementById('score'), bestEl = document.getElementById('best');
let snake, dir, nextDir, food, score, best = 0, over;
function reset() {
snake = [{ x: 10, y: 10 }, { x: 9, y: 10 }, { x: 8, y: 10 }];
dir = nextDir = { x: 1, y: 0 };
score = 0; over = false;
placeFood();
}
function placeFood() {
do { food = { x: Math.floor(Math.random() * COLS), y: Math.floor(Math.random() * ROWS) }; }
while (snake.some(s => s.x === food.x && s.y === food.y));
}
addEventListener('keydown', e => {
const k = { ArrowUp: [0, -1], ArrowDown: [0, 1], ArrowLeft: [-1, 0], ArrowRight: [1, 0] }[e.key];
if (k && !(k[0] === -dir.x && k[1] === -dir.y)) { nextDir = { x: k[0], y: k[1] }; e.preventDefault(); }
if (e.key === ' ' && over) reset();
});
function tick() {
if (over) return;
dir = nextDir;
const head = { x: snake[0].x + dir.x, y: snake[0].y + dir.y };
if (head.x < 0 || head.y < 0 || head.x >= COLS || head.y >= ROWS || snake.some(s => s.x === head.x && s.y === head.y)) {
over = true; best = Math.max(best, score); bestEl.textContent = best; return;
}
snake.unshift(head);
if (head.x === food.x && head.y === food.y) { score++; scoreEl.textContent = score; placeFood(); }
else snake.pop();
}
function draw() {
ctx.clearRect(0, 0, 600, 420);
ctx.fillStyle = '#1b2d22';
for (let x = 0; x < COLS; x++) for (let y = 0; y < ROWS; y++) if ((x + y) % 2) ctx.fillRect(x * C, y * C, C, C);
ctx.fillStyle = '#ff5d5d';
ctx.beginPath(); ctx.arc(food.x * C + C / 2, food.y * C + C / 2, C * 0.38, 0, Math.PI * 2); ctx.fill();
snake.forEach((s, i) => {
ctx.fillStyle = i === 0 ? '#7cf2a4' : `hsl(145, 65%, ${55 - Math.min(25, i)}%)`;
ctx.beginPath(); ctx.roundRect(s.x * C + 1, s.y * C + 1, C - 2, C - 2, 6); ctx.fill();
});
if (over) {
ctx.fillStyle = 'rgba(0,0,0,.6)'; ctx.fillRect(0, 0, 600, 420);
ctx.fillStyle = '#fff'; ctx.textAlign = 'center';
ctx.font = 'bold 40px Segoe UI'; ctx.fillText('Game over', 300, 200);
ctx.font = '18px Segoe UI'; ctx.fillText(`Score ${score} — press Space to play again`, 300, 238);
}
}
// a simple autopilot steers towards the food until the player presses a key, so the demo is never idle
let human = false;
addEventListener('keydown', () => { human = true; }, { once: true });
function autopilot() {
if (human) return;
const h = snake[0], options = [[1, 0], [-1, 0], [0, 1], [0, -1]].filter(([x, y]) => !(x === -dir.x && y === -dir.y));
const safe = options.filter(([x, y]) => { const n = { x: h.x + x, y: h.y + y };
return n.x >= 0 && n.y >= 0 && n.x < COLS && n.y < ROWS && !snake.some(s => s.x === n.x && s.y === n.y); });
const pick = (safe.length ? safe : options).sort((a, b) =>
Math.abs(h.x + a[0] - food.x) + Math.abs(h.y + a[1] - food.y) - Math.abs(h.x + b[0] - food.x) - Math.abs(h.y + b[1] - food.y))[0];
nextDir = { x: pick[0], y: pick[1] };
}
reset();
let restarting = false;
setInterval(() => {
autopilot(); tick(); draw();
if (over && !human && !restarting) { restarting = true; setTimeout(() => { reset(); restarting = false; }, 1200); }
}, 110);
</script>
</body>
</html>