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

Catch the Star چالش 6 از جلسه‌ی 9، متوسط

ستاره (star) با سرعت ۱۵۰ پیکسل در ثانیه می‌افتد و قهرمان (hero) پایین صحنه است.
هر بار که قهرمان ستاره را گرفت یا ستاره از پایین صحنه بیرون رفت (star.y > 480)، ستاره را دوباره بالا ببر: star.y = 0، با یک x تصادفی: star.x = salam.random(0, 330).

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

  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(360 - hero.w, hero.x));

    star.y = star.y + 150 * dt;

    ctx.fillStyle = "#1b1440";
    ctx.fillRect(0, 0, 360, 480);
    ctx.drawImage(starPic, star.x, star.y, star.w, star.h);
    ctx.drawImage(heroPic, hero.x, hero.y, hero.w, hero.h);
  });
</script>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 9: برخورد · چالش بعدی: Aim with the Paddle

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