بتل کد Battle Codeدارد باز می‌شود…

جواب «Juicy Dodge» چالش 8 از جلسه‌ی 17

سؤال

جاخالیِ کامل، ولی هنوز بی‌آب! متغیرهای shake و particles ساخته شده‌اند. حالا خودت آب‌میوه‌اش را بریز. در لحظه‌ی برخورد:
- صدای hit پخش شود،
- shake = 0.3 شود،
- ۱۰ ذره با life: 1 از وسط قهرمان در particles ساخته شود.
وقتی آخرین جان رفت (state = "over")، صدای lose فقط یک بار بیاید.
در هر فریم (حتی بعد از باخت): shake با dt تا 0 کم شود، ذره‌ها حرکت کنند، life آن‌ها dt کم شود و تمام‌شده‌ها با filter پاک شوند. صحنه را هم با ctx.translate بلرزان و ذره‌ها را بکش.

راهنما

صدا، shake و for ساختن ذره‌ها داخل if (salam.hit(hero, meteor)) می‌روند، و salam.sound("lose") کنار state = "over". شمارش معکوس و filter ذره‌ها را جای کامنت «juice» بگذار، بیرون از if (state === "play"). برای کشیدن، از کارت «همه با هم» کمک بگیر: ctx.save()، ctx.translate(...)، ... ctx.restore().

یک جواب درست

<script>
  const ctx = salam.canvas(360, 480);
  salam.touchKeys(["ArrowLeft", "ArrowRight"]);

  const hero = { x: 160, y: 420, w: 40, h: 40 };
  const meteor = { x: 170, y: 0, w: 30, h: 30 };
  let lives = 3;
  let state = "play";
  let shake = 0;
  let particles = [];

  salam.tap(function () {
    if (state === "over") {
      lives = 3;
      meteor.y = -30;
      state = "play";
    }
  });

  salam.loop(function (dt) {
    if (state === "play") {
      if (salam.keys.ArrowLeft) hero.x = hero.x - 250 * dt;
      if (salam.keys.ArrowRight) hero.x = hero.x + 250 * dt;
      hero.x = Math.max(0, Math.min(320, hero.x));

      meteor.y = meteor.y + 300 * dt;
      if (salam.hit(hero, meteor)) {
        lives = lives - 1;
        meteor.y = -30;
        meteor.x = salam.random(0, 330);
        salam.sound("hit");
        shake = 0.3;
        for (let k = 0; k < 10; k++) {
          particles.push({ x: hero.x + 20, y: hero.y + 20, vx: salam.random(-150, 150), vy: salam.random(-150, 150), life: 1 });
        }
        if (lives === 0) {
          state = "over";
          salam.sound("lose");
        }
      }
      if (meteor.y > 480) {
        meteor.y = -30;
        meteor.x = salam.random(0, 330);
      }
    }

    shake = Math.max(0, shake - dt);
    for (const p of particles) {
      p.x = p.x + p.vx * dt;
      p.y = p.y + p.vy * dt;
      p.life = p.life - dt;
    }
    particles = particles.filter(function (p) {
      return p.life > 0;
    });

    ctx.save();
    if (shake > 0) {
      ctx.translate(salam.random(-6, 6), salam.random(-6, 6));
    }
    ctx.fillStyle = "#1b1440";
    ctx.fillRect(-10, -10, 380, 500);
    ctx.fillStyle = "gray";
    ctx.fillRect(meteor.x, meteor.y, meteor.w, meteor.h);
    ctx.fillStyle = "tomato";
    ctx.fillRect(hero.x, hero.y, hero.w, hero.h);
    ctx.fillStyle = "orange";
    for (const p of particles) {
      ctx.globalAlpha = p.life;
      ctx.fillRect(p.x - 3, p.y - 3, 6, 6);
    }
    ctx.globalAlpha = 1;
    ctx.fillStyle = "white";
    ctx.font = "24px sans-serif";
    ctx.fillText("Lives: " + lives, 10, 30);
    if (state === "over") {
      ctx.fillText("Tap to play again", 85, 240);
    }
    ctx.restore();
  });
</script>

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن