(function () { var rand = PFKit.rand, pick = PFKit.pick, clamp = PFKit.clamp; var W = 540, H = 960; var COLS = 18, ROWS = 25, CELL = 28; var BX = (W - COLS * CELL) / 2, BY = 176; var BW = COLS * CELL, BH = ROWS * CELL; var DIRS = { up: [0, -1], down: [0, 1], left: [-1, 0], right: [1, 0] }; var S; function reset() { var cx = 8, cy = 16; S = { body: [{ x: cx, y: cy }, { x: cx, y: cy + 1 }, { x: cx, y: cy + 2 }, { x: cx, y: cy + 3 }], prev: null, dir: "up", queue: [], step: 0.17, acc: 0, t: 0, food: null, bonus: null, bonusT: 9, eaten: 0, grow: 0, dead: null, combo: 0, lastEat: -9, pulse: 0, flash: 0, }; S.prev = S.body.map(function (p) { return { x: p.x, y: p.y }; }); S.food = freeCell(); } function occupied(x, y) { for (var i = 0; i < S.body.length; i++) if (S.body[i].x === x && S.body[i].y === y) return true; return false; } function freeCell() { for (var tries = 0; tries < 500; tries++) { var x = (Math.random() * COLS) | 0, y = (Math.random() * ROWS) | 0; if (occupied(x, y)) continue; if (S.food && S.food.x === x && S.food.y === y) continue; if (S.bonus && S.bonus.x === x && S.bonus.y === y) continue; return { x: x, y: y, t: 0 }; } return { x: 0, y: 0, t: 0 }; } function turn(K, d) { if (S.dead != null) return; var last = S.queue.length ? S.queue[S.queue.length - 1] : S.dir; var a = DIRS[d], b = DIRS[last]; if (a[0] === -b[0] && a[1] === -b[1]) return; // no reversing if (d === last) return; if (S.queue.length < 2) { S.queue.push(d); K.sfx("blip", 0.12); } } function cx(x) { return BX + x * CELL + CELL / 2; } function cy(y) { return BY + y * CELL + CELL / 2; } function die(K, why) { S.dead = 0; S.why = why; K.sfx("explode", 0.5); K.shake(14, 0.4); var h = S.body[0]; K.burst(cx(h.x), cy(h.y), { n: 40, colors: ["#ff3df2", "#3df8ff", "#ffffff", "#ffe14d"], speed: 360, gravity: 0, life: 0.9, size: 5 }); } function tick(K) { if (S.queue.length) S.dir = S.queue.shift(); var d = DIRS[S.dir], h = S.body[0]; var nx = h.x + d[0], ny = h.y + d[1]; S.prev = S.body.map(function (p) { return { x: p.x, y: p.y }; }); if (nx < 0 || ny < 0 || nx >= COLS || ny >= ROWS) { die(K, "wall"); return; } // tail moves away this tick unless growing var hitSelf = false; for (var i = 0; i < S.body.length - (S.grow > 0 ? 0 : 1); i++) if (S.body[i].x === nx && S.body[i].y === ny) hitSelf = true; if (hitSelf) { die(K, "self"); return; } S.body.unshift({ x: nx, y: ny }); if (S.grow > 0) { S.grow--; S.prev.push(S.prev[S.prev.length - 1]); } else S.body.pop(); if (S.food && nx === S.food.x && ny === S.food.y) { S.eaten++; S.combo = S.t - S.lastEat < 4 ? S.combo + 1 : 0; S.lastEat = S.t; var pts = 10 + Math.min(S.combo, 5) * 2; K.score += pts; S.grow += 2; S.step = Math.max(0.065, 0.17 - S.eaten * 0.0045); K.sfx("synth", 0.4); K.burst(cx(nx), cy(ny), { n: 20, colors: ["#3df8ff", "#b46bff", "#ffffff"], speed: 220, gravity: 0, life: 0.55, size: 4 }); K.float(cx(nx), cy(ny) - 24, "+" + pts, "#9ffcff", 22); S.pulse = 1; S.food = freeCell(); } if (S.bonus && nx === S.bonus.x && ny === S.bonus.y) { var bp = 50 + Math.ceil(S.bonus.life) * 5; K.score += bp; S.grow += 1; K.sfx("sparkle", 0.6); K.burst(cx(nx), cy(ny), { n: 34, colors: ["#ffe14d", "#ff3df2", "#ffffff"], speed: 300, gravity: 0, life: 0.8, size: 5 }); K.float(cx(nx), cy(ny) - 28, "奖励 +" + bp, "#ffe97a", 26); K.shake(4, 0.12); S.flash = 0.25; S.bonus = null; S.bonusT = rand(9, 14); } } function update(K, dt) { S.t += dt; if (S.pulse > 0) S.pulse = Math.max(0, S.pulse - dt * 3); if (S.flash > 0) S.flash -= dt; if (S.food) S.food.t += dt; if (S.dead != null) { S.dead += dt; if (S.dead > 1.1) K.over(K.score, "长度 " + S.body.length + " · 吃掉 " + S.eaten + " 颗能量球"); return; } // bonus food timer if (S.bonus) { S.bonus.life -= dt; S.bonus.t += dt; if (S.bonus.life <= 0) { S.bonus = null; S.bonusT = rand(8, 13); } } else { S.bonusT -= dt; if (S.bonusT <= 0) { S.bonus = freeCell(); S.bonus.life = 6; K.sfx("sparkle", 0.3); } } S.acc += dt; while (S.acc >= S.step && S.dead == null) { S.acc -= S.step; tick(K); } } // ---------- drawing ---------- var cache = null; function drawBoard(K, ctx) { if (!cache) { // static layers are rendered once at 2x; only the frame color animates per frame cache = document.createElement("canvas"); cache.width = W * 2; cache.height = H * 2; var c2 = cache.getContext("2d"), real = K.ctx; c2.scale(2, 2); K.ctx = c2; drawStatic(K, c2); K.ctx = real; } ctx.drawImage(cache, 0, 0, W, H); ctx.save(); ctx.strokeStyle = S && S.flash > 0 ? "#ffe14d" : "hsl(" + (300 + Math.sin(K.time) * 20) + ",100%," + (65 + (S ? S.pulse * 20 : 0)) + "%)"; ctx.lineWidth = 3; K.round(BX - 6, BY - 6, BW + 12, BH + 12, 14); ctx.stroke(); ctx.restore(); } function drawStatic(K, ctx) { K.cover("bg", 0, 0, W, H); var g = ctx.createLinearGradient(0, 0, 0, H); g.addColorStop(0, "rgba(10,2,30,0.55)"); g.addColorStop(0.5, "rgba(10,2,30,0.25)"); g.addColorStop(1, "rgba(10,2,30,0.55)"); ctx.fillStyle = g; ctx.fillRect(0, 0, W, H); // synth sun behind the title area ctx.save(); var sx = W / 2, sy = 128; var sg = ctx.createLinearGradient(0, sy - 70, 0, sy + 10); sg.addColorStop(0, "#ffe14d"); sg.addColorStop(1, "#ff3d9a"); ctx.globalAlpha = 0.4; ctx.shadowColor = "#ff3d9a"; ctx.shadowBlur = 30; ctx.fillStyle = sg; ctx.beginPath(); ctx.arc(sx, sy, 70, Math.PI, 0); ctx.fill(); ctx.shadowBlur = 0; // retro horizontal cuts through the sun ctx.globalAlpha = 1; ctx.fillStyle = "rgba(14,4,36,0.95)"; for (var c = 0; c < 5; c++) ctx.fillRect(sx - 72, sy - 34 + c * 8, 144, 1.5 + c * 0.9); ctx.restore(); // board ctx.save(); ctx.fillStyle = "rgba(8,2,26,0.72)"; K.round(BX - 6, BY - 6, BW + 12, BH + 12, 14); ctx.fill(); ctx.strokeStyle = "rgba(61,248,255,0.07)"; ctx.lineWidth = 1; ctx.beginPath(); for (var x = 1; x < COLS; x++) { ctx.moveTo(BX + x * CELL + 0.5, BY); ctx.lineTo(BX + x * CELL + 0.5, BY + BH); } for (var y = 1; y < ROWS; y++) { ctx.moveTo(BX, BY + y * CELL + 0.5); ctx.lineTo(BX + BW, BY + y * CELL + 0.5); } ctx.stroke(); ctx.shadowColor = "#ff3df2"; ctx.shadowBlur = 20; ctx.strokeStyle = "#ff5af4"; ctx.lineWidth = 3; K.round(BX - 6, BY - 6, BW + 12, BH + 12, 14); ctx.stroke(); ctx.restore(); } function segPos(i, k) { var a = S.prev[i] || S.body[i], b = S.body[i]; return { x: cx(a.x + (b.x - a.x) * k), y: cy(a.y + (b.y - a.y) * k) }; } function drawSnake(K, ctx) { var k = S.dead != null ? 1 : clamp(S.acc / S.step, 0, 1); var n = S.body.length, pts = [], i; for (i = 0; i < n; i++) pts.push(segPos(i, k)); var alive = S.dead == null; var fade = alive ? 1 : Math.max(0, 1 - S.dead * 1.2); ctx.save(); ctx.globalAlpha = fade; ctx.lineCap = "round"; ctx.lineJoin = "round"; // outer glow, drawn as a path segment by segment so the color can shift along the body ctx.globalCompositeOperation = "lighter"; // one blurred halo for the whole body (per-segment shadowBlur is far too slow) ctx.shadowColor = "#b46bff"; ctx.shadowBlur = 16; ctx.strokeStyle = "rgba(150,110,255,0.35)"; ctx.lineWidth = CELL * 0.9; ctx.beginPath(); ctx.moveTo(pts[0].x, pts[0].y); for (i = 1; i < n; i++) ctx.lineTo(pts[i].x, pts[i].y); ctx.stroke(); ctx.shadowBlur = 0; // colored body, shifting cyan head → magenta tail for (i = n - 1; i > 0; i--) { var t = i / Math.max(1, n - 1); var hue = 185 + t * 125; ctx.strokeStyle = "hsla(" + hue + ",100%,70%,0.95)"; ctx.lineWidth = CELL * 0.5 - t * 4; ctx.beginPath(); ctx.moveTo(pts[i].x, pts[i].y); ctx.lineTo(pts[i - 1].x, pts[i - 1].y); ctx.stroke(); } // white core ctx.strokeStyle = "rgba(255,255,255,0.75)"; ctx.lineWidth = 3; ctx.beginPath(); ctx.moveTo(pts[0].x, pts[0].y); for (i = 1; i < n; i++) ctx.lineTo(pts[i].x, pts[i].y); ctx.stroke(); ctx.globalCompositeOperation = "source-over"; // head var hp = pts[0], d = DIRS[S.dir]; ctx.shadowColor = "#3df8ff"; ctx.shadowBlur = 24; var hg = ctx.createRadialGradient(hp.x - 3, hp.y - 3, 1, hp.x, hp.y, CELL * 0.6); hg.addColorStop(0, "#ffffff"); hg.addColorStop(0.5, "#7ffbff"); hg.addColorStop(1, "#1fa8ff"); ctx.fillStyle = hg; ctx.beginPath(); ctx.arc(hp.x, hp.y, CELL * 0.56, 0, Math.PI * 2); ctx.fill(); ctx.shadowBlur = 0; // eyes var px = -d[1], py = d[0]; ctx.fillStyle = "#1a0638"; for (var s = -1; s <= 1; s += 2) { ctx.beginPath(); ctx.arc(hp.x + d[0] * 5 + px * s * 6, hp.y + d[1] * 5 + py * s * 6, 3, 0, Math.PI * 2); ctx.fill(); } ctx.restore(); } function drawOrb(K, ctx, f, size, tint) { var x = cx(f.x), y = cy(f.y) + Math.sin(f.t * 4) * 2; var p = 1 + Math.sin(f.t * 5) * 0.08; ctx.save(); ctx.globalCompositeOperation = "lighter"; var g = ctx.createRadialGradient(x, y, 2, x, y, size * 1.2); g.addColorStop(0, tint); g.addColorStop(1, "rgba(0,0,0,0)"); ctx.fillStyle = g; ctx.beginPath(); ctx.arc(x, y, size * 1.2, 0, Math.PI * 2); ctx.fill(); ctx.restore(); K.draw("orb", x, y, size * p, null, Math.sin(f.t * 2) * 0.2); } function drawBonus(K, ctx, b) { var x = cx(b.x), y = cy(b.y); var blink = b.life < 2 && Math.floor(b.t * 10) % 2 === 0; ctx.save(); ctx.translate(x, y); ctx.globalAlpha = blink ? 0.35 : 1; ctx.shadowColor = "#ffe14d"; ctx.shadowBlur = 26; ctx.rotate(b.t * 2); ctx.fillStyle = "#ffe14d"; ctx.beginPath(); for (var i = 0; i < 10; i++) { var r = i % 2 ? 7 : 17, a = i * Math.PI / 5 - Math.PI / 2; ctx[i ? "lineTo" : "moveTo"](Math.cos(a) * r, Math.sin(a) * r); } ctx.closePath(); ctx.fill(); ctx.fillStyle = "#ffffff"; ctx.beginPath(); ctx.arc(0, 0, 4, 0, Math.PI * 2); ctx.fill(); ctx.restore(); // timer ring ctx.save(); ctx.strokeStyle = "rgba(255,225,77,0.85)"; ctx.lineWidth = 2.5; ctx.beginPath(); ctx.arc(x, y, 22, -Math.PI / 2, -Math.PI / 2 + Math.PI * 2 * (b.life / 6)); ctx.stroke(); ctx.restore(); } function draw(K, ctx) { drawBoard(K, ctx); if (!S) return; if (S.food) drawOrb(K, ctx, S.food, 34, "rgba(120,200,255,0.55)"); if (S.bonus) drawBonus(K, ctx, S.bonus); drawSnake(K, ctx); // HUD K.plaque(18, 14, "得分", K.score, "left"); K.panel(168, 14, 120, 52, 14); K.text("长度", 228, 29, { size: 12, color: K.theme.sub, stroke: false, weight: "600" }); K.text(String(S.body.length), 228, 49, { size: 22, color: K.theme.text, stroke: false, weight: "800" }); K.panel(296, 14, 172, 52, 14); K.text("速度", 382, 29, { size: 12, color: K.theme.sub, stroke: false, weight: "600" }); var lv = clamp((0.17 - S.step) / (0.17 - 0.065), 0, 1); ctx.fillStyle = "rgba(255,255,255,0.12)"; K.round(316, 44, 132, 8, 4); ctx.fill(); var sg = ctx.createLinearGradient(316, 0, 448, 0); sg.addColorStop(0, "#3df8ff"); sg.addColorStop(1, "#ff3df2"); ctx.fillStyle = sg; K.round(316, 44, Math.max(8, 132 * lv), 8, 4); ctx.fill(); if (S.combo > 0 && S.t - S.lastEat < 4 && S.dead == null) { ctx.globalAlpha = clamp(4 - (S.t - S.lastEat), 0, 1); K.text("连吃 ×" + (S.combo + 1), W / 2, BY - 26, { size: 20, color: "#ffe97a", stroke: false, glow: "#ff3df2" }); ctx.globalAlpha = 1; } if (K.state === "play" && S.t < 3 && S.dead == null) { ctx.globalAlpha = Math.min(1, (3 - S.t) * 1.5); K.text("滑动屏幕或方向键转向", W / 2, BY + BH - 60, { size: 22, color: "#ffffff", stroke: false, glow: "#3df8ff" }); ctx.globalAlpha = 1; } K.text("撞墙或咬到自己就会结束", W / 2, BY + BH + 36, { size: 14, color: "rgba(220,200,255,0.55)", stroke: false, weight: "600" }); } PFKit.start({ width: W, height: H, title: "霓虹贪吃蛇", help: "滑动屏幕或用方向键 / WASD 转向\n吃能量球变长,越吃越快\n金色星星限时出现,奖励更多分", scoreLabel: "得分", overTitle: "信号中断", theme: { accent: "#ff3df2", accentText: "#ffffff", text: "#f6ecff", sub: "rgba(210,190,255,0.78)", panel: "rgba(22,6,48,0.85)", border: "rgba(255,61,242,0.45)", backdrop: "#12042a" }, backdrop: "bg", assets: { bg: "bg.jpg", orb: "orb.png" }, sounds: ["synth", "blip", "sparkle", "explode", "lose"], startSound: "synth", reset: function () { reset(); }, update: update, draw: draw, onSwipe: function (K, d) { turn(K, d); }, onKey: function (K, k) { var m = { ArrowUp: "up", ArrowDown: "down", ArrowLeft: "left", ArrowRight: "right", w: "up", s: "down", a: "left", d: "right", W: "up", S: "down", A: "left", D: "right" }; if (m[k]) turn(K, m[k]); }, }); })();