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

جواب «Three Levels» چالش 7 از جلسه‌ی 15

سؤال

بازی را سه مرحله‌ای کن:
- مرحله‌ی ۱ (اول بازی): starColor "gold"، speed ۱۰۰.
- با امتیاز ۳: level ۲، starColor "tomato"، speed ۱۶۰، و پیام "Level 2!".
- با امتیاز ۶: level ۳، starColor "violet"، speed ۲۲۰، و پیام "Level 3!".
متن پیام را در message بگذار و آن را مثل قبل با messageTime فقط یک ثانیه نشان بده.

راهنما

دو if بنویس: if (score >= 3 && level === 1) { ... } و if (score >= 6 && level === 2) { ... }. در هر کدام level، starColor، speed، message و messageTime = 1 را عوض کن. برای کشیدن پیام: ctx.fillText(message, 90, 250).

یک جواب درست

<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;
  let starColor = "gold";
  let message = "";
  let messageTime = 0;

  salam.tap(function (x, y) {
    if (salam.hit({ x: x, y: y, w: 1, h: 1 }, star)) {
      score = score + 1;
      star.y = 0;
      if (score >= 3 && level === 1) {
        level = 2;
        starColor = "tomato";
        speed = 160;
        message = "Level 2!";
        messageTime = 1;
      }
      if (score >= 6 && level === 2) {
        level = 3;
        starColor = "violet";
        speed = 220;
        message = "Level 3!";
        messageTime = 1;
      }
    }
  });

  salam.loop(function (dt) {
    messageTime = Math.max(0, messageTime - 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 = starColor;
    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);
    if (messageTime > 0) {
      ctx.font = "48px sans-serif";
      ctx.fillText(message, 90, 250);
    }
  });
</script>

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