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

جواب «Best Score» چالش 6 از جلسه‌ی 10

سؤال

بازی «روی ستاره بزن» امتیاز می‌دهد. حالا بهترین رکورد را نگه دار:
- let best = salam.load("best", 0); بیرون از حلقه.
- هر وقت score از best بیشتر شد: best = score و salam.save("best", best).
- best را هم زیر امتیاز بنویس، مثلاً Best: 3.

راهنما

داخل if ستاره، بعد از score = score + 1; بنویس: if (score > best) { best = score; salam.save("best", best); }. در حلقه: ctx.fillText("Best: " + best, 10, 60);

یک جواب درست

<script>
  const ctx = salam.canvas(360, 480);
  const star = { x: 150, y: 200, w: 60, h: 60 };
  const starPic = salam.image("star");
  let score = 0;
  let best = salam.load("best", 0);

  salam.tap(function (x, y) {
    if (salam.hit({ x: x, y: y, w: 1, h: 1 }, star)) {
      score = score + 1;
      if (score > best) {
        best = score;
        salam.save("best", best);
      }
    }
  });

  salam.loop(function (dt) {
    ctx.fillStyle = "#1b1440";
    ctx.fillRect(0, 0, 360, 480);
    ctx.drawImage(starPic, star.x, star.y, star.w, star.h);
    ctx.fillStyle = "white";
    ctx.font = "24px sans-serif";
    ctx.fillText("Score: " + score, 10, 30);
    ctx.fillText("Best: " + best, 10, 60);
  });
</script>

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