(function () { var rand = PFKit.rand, clamp = PFKit.clamp; var W = 540, H = 960; var CW = 150, CH = 650; // coral draw size (CH recomputed from the image aspect) var HITW = 92; // forgiving hit width of a coral column var SPACING = 310; // horizontal distance between columns var FLOOR = H - 34; var S; var bubbles = [], motes = []; function mkAmbient() { bubbles = []; motes = []; for (var i = 0; i < 26; i++) bubbles.push({ x: rand(0, W), y: rand(0, H), r: rand(2, 7), sp: rand(30, 80), ph: rand(0, 6), z: rand(0.3, 1) }); for (var j = 0; j < 40; j++) motes.push({ x: rand(0, W), y: rand(0, H), r: rand(0.8, 2.2), ph: rand(0, 6) }); } function reset() { S = { fish: { x: 165, y: H * 0.42, vy: 0, rot: 0, wig: 0 }, cols: [], pearls: [], trail: [], t: 0, ready: true, readyT: 0, run: 0, nextX: W + 120, passed: 0, pearlsN: 0, dead: null, lastC: H * 0.45, }; } function gapH() { return Math.max(205, 290 - S.passed * 3.2); } function speed() { return Math.min(300, 185 + S.passed * 4); } function spawnCol() { var g = gapH(); var lo = g / 2 + 120, hi = FLOOR - g / 2 - 110; var c = clamp(S.lastC + rand(-250, 250), lo, hi); var col = { x: S.nextX, c: c, g: g, passed: false, sway: rand(0, 6) }; S.cols.push(col); if (S.cols.length > 1 && Math.random() < 0.4) { var prev = S.cols[S.cols.length - 2]; S.pearls.push({ x: (prev.x + col.x) / 2, y: (prev.c + c) / 2 + rand(-50, 50), t: rand(0, 6) }); } S.lastC = c; S.nextX += SPACING; } function flap(K) { if (S.dead != null) return; if (S.ready) S.ready = false; S.fish.vy = -500; S.fish.wig = 1; K.sfx("bubble", 0.35); for (var i = 0; i < 5; i++) S.trail.push({ x: S.fish.x - 30, y: S.fish.y + rand(-6, 10), vx: rand(-80, -30), vy: rand(-40, 20), r: rand(3, 7), life: rand(0.6, 1.1) }); } function die(K, why) { if (S.dead != null) return; S.dead = 0; S.fish.vy = -320; K.sfx("splash", 0.6); K.shake(10, 0.3); K.burst(S.fish.x, S.fish.y, { n: 24, colors: ["#ffffff", "#bff5ff", "#ff9a7a"], speed: 240, gravity: -120, life: 0.8, size: 5 }); S.why = why; } function update(K, dt) { var f = S.fish, i; S.t += dt; for (i = S.trail.length - 1; i >= 0; i--) { var b = S.trail[i]; b.life -= dt; b.x += b.vx * dt; b.y += (b.vy - 60) * dt; b.vy *= 0.96; if (b.life <= 0) S.trail.splice(i, 1); } if (f.wig > 0) f.wig = Math.max(0, f.wig - dt * 3); if (S.ready) { S.readyT += dt; f.y = H * 0.42 + Math.sin(S.t * 3) * 14; f.rot = Math.sin(S.t * 3) * 0.08; S.run += 60 * dt; if (S.readyT > 2.5) flap(K); // don't wait forever return; } if (S.dead != null) { S.dead += dt; f.vy += 1500 * dt; f.y = Math.min(FLOOR - 10, f.y + f.vy * dt); f.rot = Math.min(Math.PI * 0.6, f.rot + dt * 5); if (S.dead > 1) K.over(K.score, "穿过 " + S.passed + " 道珊瑚 · 珍珠 " + S.pearlsN); return; } var sp = speed(); S.run += sp * dt; f.vy = Math.min(f.vy + 1450 * dt, 720); f.y += f.vy * dt; f.rot += (clamp(f.vy / 900, -0.45, 0.9) - f.rot) * Math.min(1, dt * 8); if (f.y < 24) { f.y = 24; f.vy = Math.max(f.vy, 0); } while (S.nextX < W + 200 + SPACING) spawnCol(); for (i = S.cols.length - 1; i >= 0; i--) { var c = S.cols[i]; c.x -= sp * dt; if (c.x < -CW) { S.cols.splice(i, 1); continue; } if (!c.passed && c.x < f.x) { c.passed = true; S.passed++; K.score += 1; K.sfx("pop", 0.4); K.float(f.x + 30, f.y - 40, "+1", "#ffffff", 26); } // collision: fish ~ ellipse, approximate with a circle r=21 var r = 21; if (Math.abs(f.x - c.x) < HITW / 2 + r - 6) { if (f.y - r + 4 < c.c - c.g / 2 || f.y + r - 4 > c.c + c.g / 2) die(K, "coral"); } } S.nextX -= sp * dt; for (i = S.pearls.length - 1; i >= 0; i--) { var p = S.pearls[i]; p.x -= sp * dt; p.t += dt; if (p.x < -60) { S.pearls.splice(i, 1); continue; } var dx = p.x - f.x, dy = p.y + Math.sin(p.t * 2) * 8 - f.y; if (dx * dx + dy * dy < 46 * 46) { S.pearls.splice(i, 1); S.pearlsN++; K.score += 3; K.sfx("coin", 0.55); K.burst(p.x, p.y, { n: 22, colors: ["#ffffff", "#ffe6f4", "#bff5ff", "#ffd6a8"], speed: 220, gravity: 0, life: 0.7, size: 4 }); K.float(p.x, p.y - 34, "珍珠 +3", "#fff2c4", 24); } } if (f.y > FLOOR - 18) { f.y = FLOOR - 18; die(K, "floor"); } } // ---------- drawing ---------- function drawWater(K, ctx, run) { var im = K.img("bg"); // slow horizontal parallax on a slightly oversized cover var ow = 640, ox = -50 + Math.sin(run * 0.0015) * 50; var s = Math.max(ow / im.width, H / im.height); var dw = im.width * s, dh = im.height * s; ctx.drawImage(im, ox, H - dh, dw, dh); // light rays ctx.save(); ctx.globalCompositeOperation = "lighter"; for (var i = 0; i < 5; i++) { var x = 60 + i * 120 + Math.sin(K.time * 0.4 + i) * 30; var a = 0.05 + 0.04 * Math.sin(K.time * 0.7 + i * 1.7); var g = ctx.createLinearGradient(0, 0, 0, H * 0.8); g.addColorStop(0, "rgba(220,255,250," + a * 2 + ")"); g.addColorStop(1, "rgba(220,255,250,0)"); ctx.fillStyle = g; ctx.beginPath(); ctx.moveTo(x - 22, 0); ctx.lineTo(x + 22, 0); ctx.lineTo(x + 140, H * 0.8); ctx.lineTo(x + 40, H * 0.8); ctx.closePath(); ctx.fill(); } // drifting motes ctx.fillStyle = "rgba(255,255,255,0.5)"; for (var j = 0; j < motes.length; j++) { var m = motes[j]; var mx = ((m.x - run * 0.15) % W + W) % W, my = m.y + Math.sin(K.time + m.ph) * 6; ctx.globalAlpha = 0.25 + 0.25 * Math.sin(K.time * 1.3 + m.ph); ctx.beginPath(); ctx.arc(mx, my, m.r, 0, Math.PI * 2); ctx.fill(); } ctx.restore(); } function drawBubble(ctx, x, y, r, a) { ctx.save(); ctx.globalAlpha = a; ctx.strokeStyle = "rgba(255,255,255,0.85)"; ctx.lineWidth = Math.max(1, r * 0.22); ctx.fillStyle = "rgba(200,250,255,0.18)"; ctx.beginPath(); ctx.arc(x, y, r, 0, Math.PI * 2); ctx.fill(); ctx.stroke(); ctx.fillStyle = "rgba(255,255,255,0.9)"; ctx.beginPath(); ctx.arc(x - r * 0.35, y - r * 0.35, r * 0.25, 0, Math.PI * 2); ctx.fill(); ctx.restore(); } function drawCol(K, ctx, c) { var im = K.img("coral"); var top = c.c - c.g / 2, bot = c.c + c.g / 2; var sw = Math.sin(K.time * 1.2 + c.sway) * 0.012; // bottom column, branches pointing up toward the gap ctx.save(); ctx.translate(c.x, bot); ctx.transform(1, 0, sw, 1, 0, 0); ctx.drawImage(im, -CW / 2, -6, CW, CH); ctx.restore(); // top column, flipped so its branches point down ctx.save(); ctx.translate(c.x, top); ctx.scale(1, -1); ctx.transform(1, 0, -sw, 1, 0, 0); ctx.drawImage(im, -CW / 2, -6, CW, CH); ctx.restore(); } function draw(K, ctx) { var run = S ? S.run : K.time * 60, i; drawWater(K, ctx, run); // background bubbles for (i = 0; i < bubbles.length; i++) { var b = bubbles[i]; var by = ((b.y - K.time * b.sp) % (H + 40) + H + 40) % (H + 40) - 20; var bx = ((b.x - run * 0.3 * b.z + Math.sin(K.time + b.ph) * 10) % W + W) % W; drawBubble(ctx, bx, by, b.r * b.z, 0.35 * b.z); } if (!S) return; for (i = 0; i < S.cols.length; i++) drawCol(K, ctx, S.cols[i]); // pearls for (i = 0; i < S.pearls.length; i++) { var p = S.pearls[i], py = p.y + Math.sin(p.t * 2) * 8; ctx.save(); ctx.globalCompositeOperation = "lighter"; var g = ctx.createRadialGradient(p.x, py, 2, p.x, py, 44); g.addColorStop(0, "rgba(255,250,230,0.55)"); g.addColorStop(1, "rgba(255,200,240,0)"); ctx.fillStyle = g; ctx.beginPath(); ctx.arc(p.x, py, 44, 0, Math.PI * 2); ctx.fill(); ctx.restore(); K.draw("pearl", p.x, py, 52, null, Math.sin(p.t * 1.5) * 0.1); } // trail bubbles for (i = 0; i < S.trail.length; i++) { var t = S.trail[i]; drawBubble(ctx, t.x, t.y, t.r, Math.min(1, t.life * 1.5)); } // fish var f = S.fish; var sq = 1 + Math.sin(K.time * 14) * 0.03 + f.wig * 0.08; ctx.save(); ctx.translate(f.x, f.y); ctx.rotate(f.rot); ctx.scale(sq, 2 - sq); K.draw("fish", 0, 0, 80, null); ctx.restore(); // HUD if (K.state === "play" || K.state === "over") { K.text(String(K.score), W / 2, 92, { size: 64, color: "#ffffff", stroke: "rgba(16,80,100,0.75)", strokeWidth: 8 }); } K.panel(16, 16, 112, 44, 22); K.draw("pearl", 42, 38, 30); K.text("× " + S.pearlsN, 88, 39, { size: 20, color: K.theme.text, stroke: false, weight: "800" }); if (S.ready && K.state === "play") { var a = 0.75 + 0.25 * Math.sin(K.time * 6); ctx.globalAlpha = a; K.text("点击屏幕向上游", W / 2, H * 0.6, { size: 26, color: "#ffffff", stroke: "rgba(16,80,100,0.7)", strokeWidth: 6 }); K.text("穿过珊瑚缝隙 · 收集珍珠", W / 2, H * 0.6 + 40, { size: 17, color: "#e9fffb", stroke: "rgba(16,80,100,0.6)", strokeWidth: 4, weight: "700" }); ctx.globalAlpha = 1; } } PFKit.start({ width: W, height: H, title: "珊瑚游鱼", help: "点击屏幕、空格或↑键向上游\n穿过珊瑚缝隙得 1 分\n吃到珍珠额外 +3 分", scoreLabel: "得分", overTitle: "小鱼累啦", theme: { accent: "#ff7f6b", accentText: "#ffffff", text: "#0f4c5c", sub: "rgba(15,76,92,0.72)", panel: "rgba(240,252,250,0.9)", border: "rgba(255,255,255,0.85)", backdrop: "#4cb8c4" }, backdrop: "bg", assets: { bg: "bg.jpg", fish: "fish.png", coral: "coral.png", pearl: "pearl.png" }, sounds: ["bubble", "splash", "pop", "coin", "lose"], startSound: "bubble", overSound: "lose", init: function (K) { mkAmbient(); CH = CW * K.aspect("coral"); }, reset: function () { reset(); }, update: update, draw: draw, idle: function (K, dt) { if (S && K.state === "title") { S.run += 60 * dt; S.fish.y = H * 0.42 + Math.sin(K.time * 3) * 14; } }, onDown: function (K) { flap(K); }, onKey: function (K, k) { if (k === " " || k === "ArrowUp" || k === "w" || k === "W") flap(K); }, }); })();