(function () { var W = 540, H = 960; var G = 1500; // gravity var JUMP = 900; // normal bounce speed (~270px high) var SUPER = 1550; // carrot boost var BW = 74; // bunny draw width var CW = 118; // cloud width var rand = PFKit.rand, clamp = PFKit.clamp; var S = {}; var puffs = []; function bunnyH(K) { return BW * K.aspect("bunny"); } function cloudH(K, w) { return w * K.aspect("cloud"); } function level() { return clamp(-S.topY / 16000, 0, 1); } // 0..1 difficulty by height function addCloud(K, y) { var d = level(); var r = Math.random(); var kind = "plain"; if (S.clouds.length > 3) { if (r < 0.08 + d * 0.3) kind = "vanish"; else if (r < 0.18 + d * 0.5) kind = "move"; } var w = CW * (1 - d * 0.22) * rand(0.92, 1.08); var c = { x: rand(w / 2 + 6, W - w / 2 - 6), y: y, w: w, kind: kind, t: rand(0, 6), sp: rand(0.8, 1.3) * (1 + d * 0.9), amp: rand(60, 150), squish: 0, gone: 0 }; c.x0 = clamp(c.x, c.amp * 0 + w / 2, W - w / 2); if (kind === "move") c.x0 = rand(w / 2 + c.amp * 0.5, W - w / 2 - c.amp * 0.5); if (kind !== "vanish" && Math.random() < 0.1 + d * 0.04) c.carrot = { got: false }; S.clouds.push(c); // a vanishing cloud gets a solid backup nearby so the route never depends on luck alone if (kind === "vanish" && Math.random() < 0.6 - d * 0.4) { var bx = c.x + (c.x > W / 2 ? -1 : 1) * rand(150, 220); S.clouds.push({ x: clamp(bx, w / 2, W - w / 2), y: y + rand(-30, 30), w: w, kind: "plain", t: 0, sp: 0, amp: 0, squish: 0, gone: 0 }); } } function fill(K) { while (S.topY > S.camY - 160) { var d = level(); var gap = rand(85, 125) + d * 120; // max reach is ~270 S.topY -= Math.min(gap, 235); addCloud(K, S.topY); } } function reset(K) { S.clouds = [{ x: W / 2, y: 860, w: 190, kind: "plain", t: 0, sp: 0, amp: 0, squish: 0, gone: 0, base: true }]; S.topY = 860; S.camY = 0; S.p = { x: W / 2, y: 860, vx: 0, vy: -JUMP, face: 1, squash: 0, boost: 0 }; S.maxH = 0; S.carrots = 0; S.trail = []; S.targetX = null; S.dying = 0; S.milestone = 100; fill(K); K.score = 0; } function init(K) { for (var i = 0; i < 7; i++) puffs.push({ x: rand(0, W), y: rand(0, H), s: rand(1.6, 2.6), v: rand(4, 14), a: rand(0.1, 0.2) }); } function bounce(K, c, p) { p.vy = -JUMP; p.squash = 1; c.squish = 1; p.y = c.y - cloudH(K, c.w) * 0.3; K.sfx("boing", 0.35); K.burst(p.x, p.y + 4, { n: 8, speed: 120, gravity: 60, colors: ["#ffffff", "#ffd1e8", "#fff3b0"], size: 5, life: 0.5, up: 30 }); if (c.kind === "vanish") { c.gone = 0.001; K.sfx("whoosh", 0.3); } } function update(K, dt) { var p = S.p; // horizontal control: drag target or arrow keys var ax = 0; if (K.keys["ArrowLeft"] || K.keys["a"] || K.keys["A"]) ax -= 1; if (K.keys["ArrowRight"] || K.keys["d"] || K.keys["D"]) ax += 1; if (ax) { S.targetX = null; p.vx += ax * 2600 * dt; p.vx = clamp(p.vx, -520, 520); } else if (S.targetX != null) { var dx = S.targetX - p.x; p.vx += (clamp(dx * 9, -560, 560) - p.vx) * Math.min(1, dt * 12); } else p.vx *= Math.pow(0.02, dt); if (Math.abs(p.vx) > 30) p.face = p.vx > 0 ? 1 : -1; p.x += p.vx * dt; if (p.x < 0) p.x += W; if (p.x > W) p.x -= W; var py = p.y; p.vy += G * dt; p.y += p.vy * dt; p.squash = Math.max(0, p.squash - dt * 4); p.boost = Math.max(0, p.boost - dt); var i, c; for (i = 0; i < S.clouds.length; i++) { c = S.clouds[i]; c.t += dt; c.squish = Math.max(0, c.squish - dt * 3.5); if (c.kind === "move") c.x = c.x0 + Math.sin(c.t * c.sp) * c.amp * 0.5; if (c.gone) c.gone += dt; if (S.dying || c.gone || p.vy <= 0) continue; var top = c.y - cloudH(K, c.w) * 0.3; if (py <= top && p.y >= top && Math.abs(p.x - c.x) < c.w * 0.45 + 16) bounce(K, c, p); } // carrots for (i = 0; i < S.clouds.length; i++) { c = S.clouds[i]; if (!c.carrot || c.carrot.got) continue; var cy = c.y - 62, cx = c.x; if (Math.abs(p.x - cx) < 44 && Math.abs(p.y - bunnyH(K) * 0.45 - cy) < 70) { c.carrot.got = true; S.carrots++; p.vy = -SUPER; p.boost = 1.0; p.squash = 1; K.sfx("coin", 0.6); K.sfx("jump", 0.4); K.float(cx, cy - 30, "胡萝卜 +25", "#ff9a3c", 26); K.burst(cx, cy, { n: 22, speed: 260, gravity: 200, colors: ["#ff9a3c", "#ffd84a", "#8be37a", "#ffffff"], size: 5, life: 0.8 }); } } // camera follows upward only var want = p.y - 400; if (want < S.camY) S.camY += (want - S.camY) * Math.min(1, dt * 10); var h = Math.max(0, Math.floor((860 - p.y) / 10)); if (h > S.maxH) S.maxH = h; K.score = S.maxH + S.carrots * 25; if (S.maxH >= S.milestone) { K.float(W / 2, 220, S.milestone + " 米!", "#ff7fb6", 32); K.sfx("coin", 0.35); S.milestone += 100; } fill(K); // drop clouds far below the camera for (i = S.clouds.length - 1; i >= 0; i--) { c = S.clouds[i]; if (c.y - S.camY > H + 120 || c.gone > 0.6) S.clouds.splice(i, 1); } // rainbow trail S.trail.push({ x: p.x, y: p.y - bunnyH(K) * 0.4, t: 0, b: p.boost > 0 }); for (i = S.trail.length - 1; i >= 0; i--) { S.trail[i].t += dt; if (S.trail[i].t > 0.35) S.trail.splice(i, 1); } if (p.y - S.camY > H + 40 && !S.dying) { S.dying = 0.9; K.sfx("whoosh", 0.5); K.shake(6, 0.25); } if (S.dying) { S.dying -= dt; if (S.dying <= 0) K.over(K.score, "跳到 " + S.maxH + " 米 · 胡萝卜 " + S.carrots + " 根"); } movePuffs(dt); } function movePuffs(dt) { for (var i = 0; i < puffs.length; i++) { var q = puffs[i]; q.x += q.v * dt; if (q.x > W + 140) { q.x = -140; q.y = rand(0, H); } } } function idle(K, dt) { movePuffs(dt); if (K.state === "title") { // little hop on the start cloud behind the title card var p = S.p; p.vy += G * dt; p.y += p.vy * dt; p.squash = Math.max(0, p.squash - dt * 4); if (p.y >= 860 - cloudH(K, 190) * 0.3) { p.y = 860 - cloudH(K, 190) * 0.3; p.vy = -620; p.squash = 1; S.clouds[0].squish = 1; } S.clouds[0].squish = Math.max(0, S.clouds[0].squish - dt * 3.5); } } // ---- drawing ---- function drawBg(K, ctx) { K.cover("bg", 0, 0, W, H); // sky deepens toward twilight as you climb var k = clamp(S.maxH / 1500, 0, 1); if (k > 0) { var g = ctx.createLinearGradient(0, 0, 0, H); g.addColorStop(0, "rgba(90,80,170," + k * 0.55 + ")"); g.addColorStop(1, "rgba(150,120,200," + k * 0.15 + ")"); ctx.fillStyle = g; ctx.fillRect(0, 0, W, H); } // parallax cloud puffs for (var i = 0; i < puffs.length; i++) { var q = puffs[i]; var y = ((q.y - S.camY * 0.12 * q.s) % (H + 200) + H + 200) % (H + 200) - 100; K.draw("cloud", q.x, y, 160 * q.s, null, 0, q.a); } } function drawCloud(K, ctx, c, sy) { var h = cloudH(K, c.w); var sq = Math.sin(c.squish * Math.PI) * 0.12; var alpha = 1; if (c.kind === "vanish") alpha = c.gone ? Math.max(0, 1 - c.gone / 0.5) : 0.62 + Math.sin(c.t * 4) * 0.12; ctx.save(); ctx.translate(c.x, sy + c.squish * 6); ctx.scale(1 + sq, 1 - sq); if (c.gone) ctx.scale(1 + c.gone, 1 + c.gone * 0.6); ctx.globalAlpha = alpha; if (c.kind === "move") { ctx.shadowColor = "rgba(120,200,255,0.9)"; ctx.shadowBlur = 16; } else if (c.kind === "vanish") { ctx.shadowColor = "rgba(255,150,210,0.9)"; ctx.shadowBlur = 14; } var im = K.img("cloud"); if (im && im.width) ctx.drawImage(im, -c.w / 2, -h / 2, c.w, h); ctx.restore(); if (c.kind === "move") { // little wind arrows ctx.save(); ctx.globalAlpha = 0.7; ctx.strokeStyle = "#6ab8ff"; ctx.lineWidth = 3; ctx.lineCap = "round"; var ex = c.w / 2 + 8; ctx.beginPath(); ctx.moveTo(c.x - ex, sy - 4); ctx.lineTo(c.x - ex - 7, sy + 2); ctx.lineTo(c.x - ex, sy + 8); ctx.moveTo(c.x + ex, sy - 4); ctx.lineTo(c.x + ex + 7, sy + 2); ctx.lineTo(c.x + ex, sy + 8); ctx.stroke(); ctx.restore(); } else if (c.kind === "vanish" && !c.gone) { ctx.fillStyle = "rgba(255,255,255," + (0.5 + 0.5 * Math.sin(c.t * 6)) + ")"; sparkle(ctx, c.x + c.w * 0.3, sy - h * 0.35, 5); sparkle(ctx, c.x - c.w * 0.25, sy - h * 0.1, 4); } } function sparkle(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 drawCarrot(K, ctx, c, sy) { var x = c.x, y = sy - 62 + Math.sin(K.time * 3 + c.t) * 6; var g = ctx.createRadialGradient(x, y, 4, x, y, 44); g.addColorStop(0, "rgba(255,230,120,0.65)"); g.addColorStop(1, "rgba(255,230,120,0)"); ctx.fillStyle = g; ctx.beginPath(); ctx.arc(x, y, 44, 0, Math.PI * 2); ctx.fill(); K.draw("carrot", x, y, 46, null, Math.sin(K.time * 2 + c.t) * 0.15); } function drawBunny(K, ctx, p, sy) { var bh = bunnyH(K); var s = Math.sin(p.squash * Math.PI); var sx = 1 + s * 0.16, syc = 1 - s * 0.14; if (p.vy < -300 && p.squash < 0.5) { sx = 0.94; syc = 1.07; } ctx.save(); // soft shadow glow under the boost if (p.boost > 0) { ctx.shadowColor = "rgba(255,200,80,0.9)"; ctx.shadowBlur = 24 * p.boost; } ctx.translate(p.x, sy); ctx.rotate(clamp(p.vx / 2400, -0.22, 0.22)); ctx.scale(sx * p.face, syc); var im = K.img("bunny"); if (im && im.width) ctx.drawImage(im, -BW / 2, -bh, BW, bh); ctx.restore(); } function draw(K, ctx) { drawBg(K, ctx); var i, c; // trail for (i = 0; i < S.trail.length; i++) { var t = S.trail[i], k = 1 - t.t / 0.35; ctx.globalAlpha = k * (t.b ? 0.8 : 0.35); ctx.fillStyle = t.b ? ["#ff8fc4", "#ffd84a", "#8fd8ff"][i % 3] : "#ffffff"; ctx.beginPath(); ctx.arc(t.x, t.y - S.camY, (t.b ? 14 : 8) * k, 0, Math.PI * 2); ctx.fill(); } ctx.globalAlpha = 1; for (i = 0; i < S.clouds.length; i++) { c = S.clouds[i]; var sy = c.y - S.camY; if (sy < -80 || sy > H + 80) continue; drawCloud(K, ctx, c, sy); if (c.carrot && !c.carrot.got) drawCarrot(K, ctx, c, sy); } var p = S.p; var psy = p.y - S.camY; drawBunny(K, ctx, p, Math.min(psy, H + 300)); // wrap ghost: show the bunny on the other edge while crossing if (p.x < BW / 2) { ctx.save(); ctx.globalAlpha = 0.9; var ox = p.x; p.x += W; drawBunny(K, ctx, p, psy); p.x = ox; ctx.restore(); } else if (p.x > W - BW / 2) { var ox2 = p.x; p.x -= W; drawBunny(K, ctx, p, psy); p.x = ox2; } if (K.state === "play") drawHud(K, ctx); } function drawHud(K, ctx) { var T = K.theme; K.panel(16, 14, 140, 56, 18); K.text("高度", 86, 30, { size: 12, color: T.sub, stroke: false, weight: "700" }); K.text(S.maxH + " 米", 86, 52, { size: 22, color: T.text, stroke: false, weight: "900" }); K.panel(168, 14, 118, 56, 18); K.draw("carrot", 196, 42, 30); K.text("× " + S.carrots, 244, 43, { size: 20, color: "#ff8a3c", stroke: false, weight: "900" }); K.panel(298, 14, 150, 56, 18); K.text("得分", 373, 30, { size: 12, color: T.sub, stroke: false, weight: "700" }); K.text(String(K.score), 373, 52, { size: 22, color: T.accent, stroke: false, weight: "900" }); if (S.maxH < 15) K.text("按住拖动 / ←→ 键左右移动", W / 2, H - 40, { size: 19, color: "#5a4a8a", stroke: "rgba(255,255,255,0.9)", strokeWidth: 5, weight: "800" }); } PFKit.start({ width: W, height: H, title: "云端跳跳", help: "小兔会自动弹跳,按住屏幕左右拖动控制方向\n键盘 ←→ 或 A/D 移动,穿过左右边缘会绕到另一侧\n吃胡萝卜超级跳!粉色云只能踩一次,掉下去就结束", scoreLabel: "得分", overTitle: "掉下云端啦", theme: { accent: "#ff6fae", accentText: "#ffffff", text: "#4a3a7a", sub: "rgba(90,74,138,0.75)", panel: "rgba(255,255,255,0.86)", border: "rgba(255,170,210,0.7)", backdrop: "#bfe0ff", }, backdrop: "bg", assets: { bg: "bg.jpg", bunny: "bunny.png", cloud: "cloud.png", carrot: "carrot.png" }, sounds: ["boing", "jump", "coin", "whoosh"], startSound: "jump", overSound: "whoosh", init: init, reset: reset, update: update, idle: idle, draw: draw, onDown: function (K, x) { S.targetX = x; }, onMove: function (K, x, y, down) { if (down) S.targetX = x; }, onUp: function () { S.targetX = null; }, }); })();