بتل کد 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 بلرزان و ذره‌ها را بکش.

<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);
        if (lives === 0) {
          state = "over";
        }
      }
      if (meteor.y > 480) {
        meteor.y = -30;
        meteor.x = salam.random(0, 330);
      }
    }

    // juice: count down, move and remove here

    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 = "white";
    ctx.font = "24px sans-serif";
    ctx.fillText("Lives: " + lives, 10, 30);
    if (state === "over") {
      ctx.fillText("Tap to play again", 85, 240);
    }
  });
</script>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 17: صدا و جلوه

بیشتر بخوان: دیکشنری در پایتون: آموزش کامل کلید و مقدار با مثال