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

Touching? چالش 1 از جلسه‌ی 9، آسان

قهرمان (hero) با کلیدهای چپ و راست راه می‌رود و ستاره (star) کمی آن طرف‌تر ایستاده. متغیر touching هنوز همیشه false است.
داخل حلقه، هر فریم touching را برابر جواب salam.hit(hero, star) کن تا وقتی قهرمان به ستاره می‌رسد آسمان طلایی شود، و وقتی دور می‌شود دوباره آبی.

<script>
  const ctx = salam.canvas(360, 480);
  const hero = { x: 40, y: 380, w: 40, h: 40 };
  const star = { x: 200, y: 380, w: 40, h: 40 };
  let touching = false;
  salam.touchKeys(["ArrowLeft", "ArrowRight"]);

  salam.loop(function (dt) {
    if (salam.keys.ArrowLeft) hero.x = hero.x - 200 * dt;
    if (salam.keys.ArrowRight) hero.x = hero.x + 200 * dt;
    // touching = ...

    if (touching) {
      ctx.fillStyle = "gold";
    } else {
      ctx.fillStyle = "skyblue";
    }
    ctx.fillRect(0, 0, 360, 480);
    ctx.fillStyle = "tomato";
    ctx.fillRect(hero.x, hero.y, hero.w, hero.h);
    ctx.fillStyle = "white";
    ctx.fillRect(star.x, star.y, star.w, star.h);
  });
</script>

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

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