(function () { var W = 540, H = 960; var TIME = 45; var LIVES = 3; var COLS = [104, 270, 436]; var ROWS = [430, 615, 800]; // y of each grave's center var GRAVE_W = 150; var KEYMAP = { "7": 0, "8": 1, "9": 2, "4": 3, "5": 4, "6": 5, "1": 6, "2": 7, "3": 8, q: 0, w: 1, e: 2, a: 3, s: 4, d: 5, z: 6, x: 7, c: 8 }; var rand = PFKit.rand, clamp = PFKit.clamp; var S = {}; var fog = [], stars = []; function graveH(K) { return GRAVE_W * K.aspect("grave"); } function creatureY(h, up) { return h.y + 20 - 140 * up; } function reset(K) { S.holes = []; for (var i = 0; i < 9; i++) S.holes.push({ i: i, x: COLS[i % 3], y: ROWS[(i / 3) | 0], m: null, wob: 0 }); S.left = TIME; S.lives = LIVES; S.combo = 0; S.best = 0; S.hits = 0; S.spawnT = 0.6; S.splats = []; S.flyers = []; S.hurt = 0; S.lastTick = TIME; K.score = 0; } function init(K) { for (var i = 0; i < 6; i++) fog.push({ x: rand(0, W), y: rand(560, 940), r: rand(110, 190), v: rand(8, 20) * (Math.random() < 0.5 ? -1 : 1) }); for (var j = 0; j < 22; j++) stars.push({ x: rand(0, W), y: rand(80, 300), p: rand(0, 6), s: rand(1, 2.4) }); } // difficulty 0..1 over the run function diff() { return clamp(1 - S.left / TIME, 0, 1); } function spawn(K) { var free = []; for (var i = 0; i < 9; i++) if (!S.holes[i].m) free.push(S.holes[i]); if (!free.length) return; var h = PFKit.pick(free); var d = diff(); var r = Math.random(); var kind = "ghost"; if (r < 0.1 + d * 0.06) kind = "pumpkin"; else if (r < 0.24 + d * 0.12) kind = "bomb"; var stay = (1.25 - d * 0.6) * (kind === "pumpkin" ? 0.75 : 1); h.m = { kind: kind, t: 0, rise: 0.16, stay: stay, sink: 0.18, state: "up", up: 0, seed: Math.random() * 10 }; } function whack(K, h) { var m = h.m; if (!m || m.state === "hit" || m.up < 0.35) return false; m.state = "hit"; m.t = 0; var top = creatureY(h, m.up); if (m.kind === "bomb") { S.lives--; S.combo = 0; S.hurt = 0.5; K.sfx("bonk", 0.7); K.shake(14, 0.35); K.burst(h.x, top, { n: 30, speed: 320, gravity: 200, colors: ["#3a2a4a", "#ff8a1a", "#ffd23f", "#ffffff"], size: 6, life: 0.8 }); K.float(h.x, top - 40, "-1 生命", "#ff6a5a", 28); if (S.lives <= 0) { S.endT = 0.6; } return true; } S.combo++; S.hits++; if (S.combo > S.best) S.best = S.combo; var mul = Math.min(5, 1 + Math.floor(S.combo / 5)); var pts = (m.kind === "pumpkin" ? 30 : 10) * mul; K.score += pts; if (m.kind === "pumpkin") { K.sfx("sparkle", 0.6); K.burst(h.x, top, { n: 28, speed: 300, gravity: 260, colors: ["#ffd23f", "#ff9a1a", "#fff3b0"], size: 5, life: 0.9 }); K.float(h.x, top - 50, "金南瓜 +" + pts, "#ffd23f", 30); } else { K.sfx(S.combo % 5 === 0 ? "sparkle" : "pop", 0.55); K.sfx("boo", 0.25); K.burst(h.x, top, { n: 18, speed: 240, gravity: 120, colors: ["#b6ff5a", "#e9dcff", "#ffffff"], size: 5, life: 0.7 }); K.float(h.x, top - 50, "+" + pts, mul > 1 ? "#b6ff5a" : "#ffffff", 28); } if (S.combo > 0 && S.combo % 5 === 0) K.float(W / 2, 250, "连击 ×" + S.combo + "!", "#ff9a1a", 34); S.flyers.push({ kind: m.kind, x: h.x, y: top, vx: rand(-160, 160), vy: -380, rot: 0, vr: rand(-8, 8), life: 0.7 }); K.shake(4, 0.1); return true; } function tapAt(K, x, y) { var best = null, bd = 1e9; for (var i = 0; i < 9; i++) { var h = S.holes[i]; if (!h.m || h.m.state === "hit") continue; var cy = creatureY(h, h.m.up); var dx = x - h.x, dy = y - cy; // generous hit box covering the creature and the grave if (Math.abs(dx) < 82 && dy > -95 && dy < 110) { var d = dx * dx + dy * dy; if (d < bd) { bd = d; best = h; } } } S.splats.push({ x: x, y: y, t: 0, hit: !!best }); if (best) whack(K, best); else K.sfx("click", 0.25); } function update(K, dt) { S.left -= dt; S.hurt = Math.max(0, S.hurt - dt); if (S.left <= 10 && S.left > 0 && Math.ceil(S.left) !== S.lastTick) { S.lastTick = Math.ceil(S.left); K.sfx("click", 0.3); } if (S.endT != null) { S.endT -= dt; if (S.endT <= 0) return K.over(K.score, "生命耗尽 · 最高连击 " + S.best); } if (S.left <= 0) { S.left = 0; return K.over(K.score, "敲中 " + S.hits + " 只 · 最高连击 " + S.best); } var d = diff(); S.spawnT -= dt; if (S.spawnT <= 0) { spawn(K); if (Math.random() < d * 0.45) spawn(K); S.spawnT = rand(0.75, 1.0) - d * 0.45; } for (var i = 0; i < 9; i++) { var h = S.holes[i], m = h.m; h.wob = Math.max(0, h.wob - dt * 3); if (!m) continue; m.t += dt; if (m.state === "up") { m.up = Math.min(1, m.t / m.rise); if (m.up >= 1) { m.state = "stay"; m.t = 0; } } else if (m.state === "stay") { if (m.t >= m.stay) { m.state = "down"; m.t = 0; } } else if (m.state === "down") { m.up = Math.max(0, 1 - m.t / m.sink); if (m.up <= 0) { if (m.kind === "ghost" && S.combo > 0) { if (S.combo >= 3) K.float(h.x, h.y - 120, "溜走了", "#c9b6ff", 20); S.combo = 0; } h.m = null; } } else if (m.state === "hit") { h.wob = 1; h.m = null; } } for (var f = S.flyers.length - 1; f >= 0; f--) { var fl = S.flyers[f]; fl.vy += 1000 * dt; fl.x += fl.vx * dt; fl.y += fl.vy * dt; fl.rot += fl.vr * dt; fl.life -= dt; if (fl.life <= 0) S.flyers.splice(f, 1); } updFx(dt); } function updFx(dt) { for (var s = S.splats.length - 1; s >= 0; s--) { S.splats[s].t += dt; if (S.splats[s].t > 0.4) S.splats.splice(s, 1); } for (var i = 0; i < fog.length; i++) { var f = fog[i]; f.x += f.v * dt; if (f.x > W + f.r) f.x = -f.r; if (f.x < -f.r) f.x = W + f.r; } } function idle(K, dt) { updFx(dt); } // ---- drawing ---- function drawBomb(K, ctx, x, y, r, t) { ctx.save(); ctx.translate(x, y); // glow var g = ctx.createRadialGradient(0, 0, r * 0.6, 0, 0, r * 1.6); g.addColorStop(0, "rgba(255,80,60,0.35)"); g.addColorStop(1, "rgba(255,80,60,0)"); ctx.fillStyle = g; ctx.beginPath(); ctx.arc(0, 0, r * 1.6, 0, Math.PI * 2); ctx.fill(); // body var b = ctx.createRadialGradient(-r * 0.35, -r * 0.4, r * 0.1, 0, 0, r); b.addColorStop(0, "#5a4a70"); b.addColorStop(1, "#1a1226"); ctx.fillStyle = b; ctx.strokeStyle = "#0c0814"; ctx.lineWidth = 4; ctx.beginPath(); ctx.arc(0, 0, r, 0, Math.PI * 2); ctx.fill(); ctx.stroke(); // cap + fuse ctx.fillStyle = "#3a2e4a"; ctx.fillRect(-r * 0.28, -r - 8, r * 0.56, 12); ctx.strokeRect(-r * 0.28, -r - 8, r * 0.56, 12); ctx.strokeStyle = "#c8a46a"; ctx.lineWidth = 4; ctx.lineCap = "round"; ctx.beginPath(); ctx.moveTo(0, -r - 8); ctx.quadraticCurveTo(r * 0.3, -r - 30, r * 0.55, -r - 24); ctx.stroke(); // spark var sp = 6 + Math.sin(t * 40) * 3; ctx.fillStyle = "#ffd23f"; ctx.shadowColor = "#ff8a1a"; ctx.shadowBlur = 14; ctx.beginPath(); for (var i = 0; i < 10; i++) { var a = (i / 10) * Math.PI * 2, rr = i % 2 ? sp * 0.45 : sp; ctx.lineTo(r * 0.55 + Math.cos(a) * rr, -r - 24 + Math.sin(a) * rr); } ctx.closePath(); ctx.fill(); ctx.shadowBlur = 0; // skull face ctx.fillStyle = "#f2ecff"; ctx.beginPath(); ctx.ellipse(0, -r * 0.08, r * 0.5, r * 0.44, 0, 0, Math.PI * 2); ctx.fill(); ctx.fillRect(-r * 0.28, r * 0.2, r * 0.56, r * 0.26); ctx.fillStyle = "#1a1226"; ctx.beginPath(); ctx.ellipse(-r * 0.2, -r * 0.1, r * 0.13, r * 0.15, 0, 0, Math.PI * 2); ctx.fill(); ctx.beginPath(); ctx.ellipse(r * 0.2, -r * 0.1, r * 0.13, r * 0.15, 0, 0, Math.PI * 2); ctx.fill(); ctx.beginPath(); ctx.moveTo(0, r * 0.06); ctx.lineTo(-r * 0.06, r * 0.16); ctx.lineTo(r * 0.06, r * 0.16); ctx.closePath(); ctx.fill(); ctx.lineWidth = 2; ctx.strokeStyle = "#1a1226"; for (var k = -1; k <= 1; k++) { ctx.beginPath(); ctx.moveTo(k * r * 0.12, r * 0.22); ctx.lineTo(k * r * 0.12, r * 0.44); ctx.stroke(); } // shine ctx.fillStyle = "rgba(255,255,255,0.35)"; ctx.beginPath(); ctx.ellipse(-r * 0.55, -r * 0.5, r * 0.14, r * 0.22, -0.6, 0, Math.PI * 2); ctx.fill(); ctx.restore(); } function drawCreature(K, ctx, kind, x, y, t, rot, alpha) { var bob = Math.sin(t * 5) * 3; if (kind === "ghost") K.draw("ghost", x, y + bob, 128, null, rot + Math.sin(t * 3) * 0.06, alpha); else if (kind === "pumpkin") { ctx.save(); ctx.globalAlpha = alpha == null ? 1 : alpha; var g = ctx.createRadialGradient(x, y, 20, x, y, 90); g.addColorStop(0, "rgba(255,210,63,0.55)"); g.addColorStop(1, "rgba(255,210,63,0)"); ctx.fillStyle = g; ctx.beginPath(); ctx.arc(x, y, 90, 0, Math.PI * 2); ctx.fill(); ctx.restore(); K.draw("pumpkin", x, y + bob, 112, null, rot, alpha); // glints ctx.save(); ctx.globalAlpha = (alpha == null ? 1 : alpha) * (0.5 + 0.5 * Math.sin(t * 8)); ctx.fillStyle = "#fff6c0"; star(ctx, x + 38, y - 36 + bob, 7); star(ctx, x - 42, y - 10 + bob, 5); ctx.restore(); } else { ctx.save(); if (alpha != null) ctx.globalAlpha = alpha; ctx.translate(x, y + bob); ctx.rotate(rot); drawBomb(K, ctx, 0, 0, 40, t); ctx.restore(); } } function star(ctx, x, y, r) { ctx.beginPath(); for (var i = 0; i < 8; i++) { var a = (i / 8) * Math.PI * 2, rr = i % 2 ? r * 0.3 : r; ctx.lineTo(x + Math.cos(a) * rr, y + Math.sin(a) * rr); } ctx.closePath(); ctx.fill(); } function drawHole(K, ctx, h) { var gh = graveH(K); var m = h.m; var gy = h.y; // soil mound ctx.fillStyle = "rgba(20,10,35,0.55)"; ctx.beginPath(); ctx.ellipse(h.x, gy + gh / 2 - 6, GRAVE_W * 0.56, 16, 0, 0, Math.PI * 2); ctx.fill(); if (m) { // creature rises from behind the headstone; clip so it never shows below the stone's base var cy = creatureY(h, easeOut(m.up)); ctx.save(); ctx.beginPath(); ctx.rect(h.x - 100, gy - 260, 200, 260 - gh * 0.1); ctx.clip(); // eerie glow behind grave var gl = ctx.createRadialGradient(h.x, gy - gh * 0.3, 10, h.x, gy - gh * 0.3, 110); var gc = m.kind === "bomb" ? "255,90,70" : m.kind === "pumpkin" ? "255,200,60" : "160,255,90"; gl.addColorStop(0, "rgba(" + gc + "," + 0.35 * m.up + ")"); gl.addColorStop(1, "rgba(" + gc + ",0)"); ctx.fillStyle = gl; ctx.fillRect(h.x - 110, gy - 220, 220, 240); drawCreature(K, ctx, m.kind, h.x, cy, m.t + m.seed, 0); ctx.restore(); } var w = 1 + Math.sin(h.wob * 20) * 0.04 * h.wob; K.draw("grave", h.x, gy, GRAVE_W * w, gh / w); } function easeOut(k) { return 1 - (1 - k) * (1 - k) * (1 - k) + Math.sin(k * Math.PI) * 0.08; } function draw(K, ctx) { K.cover("bg", 0, 0, W, H); // twinkles for (var s = 0; s < stars.length; s++) { var st = stars[s]; ctx.globalAlpha = 0.3 + 0.7 * Math.abs(Math.sin(K.time * 1.5 + st.p)); ctx.fillStyle = "#fff6d8"; star(ctx, st.x, st.y, st.s * 2); } ctx.globalAlpha = 1; var top = ctx.createLinearGradient(0, 0, 0, 220); top.addColorStop(0, "rgba(28,12,58,0.65)"); top.addColorStop(1, "rgba(28,12,58,0)"); ctx.fillStyle = top; ctx.fillRect(0, 0, W, 220); // darker floor so graves pop var fl = ctx.createLinearGradient(0, 300, 0, H); fl.addColorStop(0, "rgba(30,14,60,0)"); fl.addColorStop(1, "rgba(30,14,60,0.45)"); ctx.fillStyle = fl; ctx.fillRect(0, 300, W, H - 300); for (var i = 0; i < 9; i++) drawHole(K, ctx, S.holes[i]); // ground fog for (var f = 0; f < fog.length; f++) { var fg = fog[f]; var rg = ctx.createRadialGradient(fg.x, fg.y, 0, fg.x, fg.y, fg.r); rg.addColorStop(0, "rgba(170,255,150,0.13)"); rg.addColorStop(1, "rgba(170,255,150,0)"); ctx.fillStyle = rg; ctx.fillRect(fg.x - fg.r, fg.y - fg.r, fg.r * 2, fg.r * 2); } for (var q = 0; q < S.flyers.length; q++) { var fy = S.flyers[q]; drawCreature(K, ctx, fy.kind, fy.x, fy.y, K.time, fy.rot, Math.max(0, fy.life / 0.7)); } for (var p = 0; p < S.splats.length; p++) { var sp = S.splats[p], k = sp.t / 0.4; ctx.strokeStyle = sp.hit ? "rgba(182,255,90," + (1 - k) + ")" : "rgba(220,200,255," + (1 - k) * 0.6 + ")"; ctx.lineWidth = 5 * (1 - k) + 1; ctx.beginPath(); ctx.arc(sp.x, sp.y, 14 + k * 40, 0, Math.PI * 2); ctx.stroke(); } if (S.hurt > 0) { var v = ctx.createRadialGradient(W / 2, H / 2, H * 0.3, W / 2, H / 2, H * 0.7); v.addColorStop(0, "rgba(255,40,40,0)"); v.addColorStop(1, "rgba(255,40,40," + S.hurt + ")"); ctx.fillStyle = v; ctx.fillRect(0, 0, W, H); } if (K.state === "play") drawHud(K, ctx); } function heart(ctx, x, y, s, on) { ctx.save(); ctx.translate(x, y); ctx.scale(s / 20, s / 20); ctx.beginPath(); ctx.moveTo(0, 7); ctx.bezierCurveTo(-12, -1, -10, -12, 0, -6); ctx.bezierCurveTo(10, -12, 12, -1, 0, 7); ctx.fillStyle = on ? "#b6ff5a" : "rgba(255,255,255,0.15)"; ctx.fill(); ctx.lineWidth = 2; ctx.strokeStyle = on ? "#2d5a10" : "rgba(255,255,255,0.3)"; ctx.stroke(); ctx.restore(); } function drawHud(K, ctx) { var T = K.theme; K.panel(16, 14, 112, 56, 16); K.text("时间", 72, 30, { size: 12, color: T.sub, stroke: false, weight: "600" }); var low = S.left <= 10; K.text(Math.ceil(S.left) + "s", 72, 52, { size: 24, color: low ? (Math.floor(K.time * 4) % 2 ? "#ff9a1a" : "#ffd23f") : T.text, stroke: false, weight: "900" }); K.panel(W / 2 - 80, 14, 160, 56, 16); K.text("得分", W / 2, 30, { size: 12, color: T.sub, stroke: false, weight: "600" }); K.text(String(K.score), W / 2, 52, { size: 26, color: T.accent, stroke: false, weight: "900" }); K.panel(W / 2 + 92, 14, 104, 56, 16); for (var i = 0; i < LIVES; i++) heart(ctx, W / 2 + 116 + i * 28, 43, 28, i < S.lives); if (S.combo >= 2) { var mul = Math.min(5, 1 + Math.floor(S.combo / 5)); K.panel(W / 2 - 70, 80, 140, 32, 16); K.text("连击 " + S.combo + (mul > 1 ? " ×" + mul : ""), W / 2, 97, { size: 16, color: mul > 1 ? "#ff9a1a" : T.text, stroke: false, weight: "800" }); } if (S.left > TIME - 4) { K.text("敲幽灵,别碰骷髅炸弹!", W / 2, 925, { size: 20, color: "#e9dcff", stroke: "#1c0c3a", strokeWidth: 5, weight: "800" }); } } PFKit.start({ width: W, height: H, title: "幽灵敲敲乐", help: "点击冒出墓碑的小幽灵得分\n金南瓜分数更高,骷髅炸弹会扣生命\n键盘:数字键 1-9 或 QWE/ASD/ZXC", scoreLabel: "得分", overTitle: "天亮啦", theme: { accent: "#b6ff5a", accentText: "#1c0c3a", text: "#f4ecff", sub: "rgba(214,196,255,0.78)", panel: "rgba(40,18,78,0.86)", border: "rgba(182,255,90,0.35)", backdrop: "#1c0c3a", }, backdrop: "bg", assets: { bg: "bg.jpg", ghost: "ghost.png", pumpkin: "pumpkin.png", grave: "grave.png" }, sounds: ["boo", "pop", "bonk", "sparkle", "click"], startSound: "boo", overSound: "boo", init: init, reset: reset, update: update, idle: idle, draw: draw, onDown: function (K, x, y) { tapAt(K, x, y); }, onKey: function (K, k) { var i = KEYMAP[String(k).toLowerCase()]; if (i == null) return; var h = S.holes[i]; tapAt(K, h.x, creatureY(h, h.m ? h.m.up : 1)); }, }); })();