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

Three Hearts چالش 3 از جلسه‌ی 10، آسان

شهاب (meteor) با سرعت ۲۴۰ پیکسل در ثانیه می‌افتد. اگر به قهرمان بخورد، به بالا برمی‌گردد.
- متغیر lives را با 3 بساز.
- هر بار که شهاب به قهرمان خورد، lives یکی کم شود.
- بالای صحنه به تعداد lives قلب بکش، با salam.image("heart") و یک for.

<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");
  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)) {
      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);
  });
</script>

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

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