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

جواب «Level by Formula» چالش 5 از جلسه‌ی 15

سؤال

به جای یک if برای هر مرحله، از یک فرمول استفاده کن. بعد از هر امتیاز:
- level = Math.floor(score / 5) + 1; (امتیاز ۰ تا ۴ مرحله‌ی ۱، ۵ تا ۹ مرحله‌ی ۲، …)
- speed = 60 + level * 40; (مرحله‌ی ۱: ۱۰۰، مرحله‌ی ۲: ۱۴۰، مرحله‌ی ۳: ۱۸۰)

راهنما

در if (salam.hit(...)) آن if مرحله را پاک کن و به جایش این دو خط را بنویس: level = Math.floor(score / 5) + 1; و speed = 60 + level * 40;

یک جواب درست

<script>
  const ctx = salam.canvas(360, 480);
  const star = { x: 160, y: 0, w: 40, h: 40 };
  let score = 0;
  let speed = 100;
  let level = 1;

  salam.tap(function (x, y) {
    if (salam.hit({ x: x, y: y, w: 1, h: 1 }, star)) {
      score = score + 1;
      star.y = 0;
      level = Math.floor(score / 5) + 1;
      speed = 60 + level * 40;
    }
  });

  salam.loop(function (dt) {
    star.y = star.y + speed * dt;
    if (star.y > 480) star.y = 0;

    ctx.fillStyle = "midnightblue";
    ctx.fillRect(0, 0, 360, 480);
    ctx.fillStyle = "gold";
    ctx.fillRect(star.x, star.y, star.w, star.h);
    ctx.fillStyle = "white";
    ctx.font = "20px sans-serif";
    ctx.fillText("Score: " + score + "  Level: " + level, 10, 30);
  });
</script>

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