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

No More Lives چالش 5 از جلسه‌ی 10، متوسط

بازی شهاب با سه جان آماده است، ولی وقتی جان‌ها تمام می‌شود بازی ادامه پیدا می‌کند و شهاب هنوز می‌افتد.
وقتی lives به 0 رسید، همه چیز بایستد: شهاب دیگر نیفتد، قهرمان راه نرود و lives منفی نشود. وسط صحنه هم Game Over بنویس.

<script>
  const ctx = salam.canvas(360, 480);
  const hero = { x: 150, y: 420, w: 60, h: 40 };
  const meteor = { x: 160, y: 0, w: 40, h: 40 };
  const heroPic = salam.image("hero");
  const meteorPic = salam.image("meteor");
  const heart = salam.image("heart");
  let lives = 3;
  salam.touchKeys(["ArrowLeft", "ArrowRight"]);

  salam.loop(function (dt) {
    if (salam.keys.ArrowLeft) hero.x = hero.x - 300 * dt;
    if (salam.keys.ArrowRight) hero.x = hero.x + 300 * dt;
    hero.x = Math.max(0, Math.min(360 - hero.w, hero.x));

    meteor.y = meteor.y + 240 * dt;
    if (salam.hit(meteor, hero)) {
      lives = lives - 1;
      meteor.y = 0;
    }
    if (meteor.y > 480) meteor.y = 0;

    ctx.fillStyle = "#1b1440";
    ctx.fillRect(0, 0, 360, 480);
    ctx.drawImage(meteorPic, meteor.x, meteor.y, meteor.w, meteor.h);
    ctx.drawImage(heroPic, hero.x, hero.y, hero.w, hero.h);
    for (let i = 0; i < lives; i++) {
      ctx.drawImage(heart, 330 - i * 30, 10, 24, 24);
    }
  });
</script>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 10: امتیاز و جان · چالش بعدی: Best Score

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