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

Ouch, Shake! چالش 4 از جلسه‌ی 17، متوسط

جاخالی با یک شهاب: شهاب درست بالای سر قهرمان می‌افتد و هر برخورد یک جان (lives) کم می‌کند. شمارش معکوس shake و لرزش صحنه آماده است.
کاری کن که در لحظه‌ی برخورد:
- shake بشود 0.4،
- صدای hit پخش شود.
اگر قهرمان کنار برود، نه صدایی بیاید و نه لرزشی.

<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 shake = 0;

  salam.loop(function (dt) {
    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;
    }
    if (meteor.y > 480) {
      meteor.y = -30;
    }
    shake = Math.max(0, shake - dt);

    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 = "white";
    ctx.font = "24px sans-serif";
    ctx.fillText("Lives: " + lives, 10, 30);
    ctx.restore();
  });
</script>

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

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