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

جواب «Touching?» چالش 1 از جلسه‌ی 9

سؤال

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

راهنما

به جای خط // touching = ... بنویس 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 = salam.hit(hero, star);

    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>

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن