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

جلسه‌ی 10: امتیاز و جان Score and Lives

بازی‌ات امتیاز می‌شمارد و روی صحنه می‌نویسد، با قلب‌ها جان نشان می‌دهد، و بهترین رکورد را با salam.save و salam.load یادش می‌ماند.

در این جلسه یاد می‌گیری

درس

بازی بدون امتیاز مثل مسابقه بدون داور است! امتیاز فقط یک متغیر است: let score = 0; بیرون از حلقه. هر بار که چیزی خوب اتفاق می‌افتد، score = score + 1. برای نوشتنش روی صحنه از fillText استفاده کن، داخل حلقه تا هر فریم عدد تازه کشیده شود. "Score: " + score متن و عدد را به هم می‌چسباند: Score: 3. روی ستاره بزن.

<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;

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

  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);
  });
</script>

یادت هست حلقه ۶۰ بار در ثانیه اجرا می‌شود؟ اگر وقتی قهرمان سکه را می‌گیرد سکه را جابه‌جا نکنی، تا وقتی رویش ایستاده هر فریم یک امتیاز می‌گیرد: در یک ثانیه ۶۰ امتیاز! پس در همان if برخورد، هم امتیاز بده و هم سکه را ببر جای دیگر.

<script>
  const ctx = salam.canvas(360, 480);
  const hero = { x: 40, y: 380, w: 40, h: 40 };
  const coin = { x: 250, y: 390, w: 30, h: 30 };
  const heroPic = salam.image("hero");
  const coinPic = salam.image("coin");
  let score = 0;
  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;
    hero.x = Math.max(0, Math.min(320, hero.x));

    if (salam.hit(hero, coin)) {
      score = score + 1;               // one point...
      coin.x = salam.random(0, 330);   // ...and the coin moves away
    }

    ctx.fillStyle = "skyblue";
    ctx.fillRect(0, 0, 360, 480);
    ctx.drawImage(coinPic, coin.x, coin.y, coin.w, coin.h);
    ctx.drawImage(heroPic, hero.x, hero.y, hero.w, hero.h);
    ctx.fillStyle = "navy";
    ctx.font = "24px sans-serif";
    ctx.fillText("Score: " + score, 10, 30);
  });
</script>

جان: let lives = 3;. هر بار که بد می‌شود (شهاب به قهرمان می‌خورد، ستاره از دست می‌رود)، lives = lives - 1. جان را با قلب نشان بده: عکس salam.image("heart") را یک بار بیرون از حلقه بگیر، بعد با یک for به تعداد lives قلب بکش، هر کدام ۳۰ پیکسل کنار قبلی. با کلیدها از جلوی شهاب کنار برو!

<script>
  const ctx = salam.canvas(360, 480);
  const hero = { x: 150, y: 420, w: 60, h: 40 };
  const meteor = { x: 160, y: 0, w: 40, h: 40 };
  const heart = salam.image("heart");
  let lives = 3;
  salam.touchKeys(["ArrowLeft", "ArrowRight"]);

  salam.loop(function (dt) {
    if (salam.keys.ArrowLeft) hero.x = hero.x - 300 * dt;
    if (salam.keys.ArrowRight) hero.x = hero.x + 300 * dt;
    hero.x = Math.max(0, Math.min(300, hero.x));

    meteor.y = meteor.y + 240 * dt;
    if (salam.hit(meteor, hero)) {
      lives = lives - 1;
      meteor.y = 0;
    }
    if (meteor.y > 480) meteor.y = 0;

    ctx.fillStyle = "#1b1440";
    ctx.fillRect(0, 0, 360, 480);
    ctx.fillStyle = "gray";
    ctx.fillRect(meteor.x, meteor.y, meteor.w, meteor.h);
    ctx.fillStyle = "tomato";
    ctx.fillRect(hero.x, hero.y, hero.w, hero.h);
    for (let i = 0; i < lives; i++) {
      ctx.drawImage(heart, 10 + i * 30, 10, 24, 24);
    }
  });
</script>

وقتی جان‌ها تمام شد، بازی باید بایستد. ساده‌ترین راه: همه‌ی حرکت‌ها و برخوردها را داخل if (lives > 0) { ... } بگذار. کشیدن را بیرون از آن نگه دار تا صحنه دیده شود، و اگر lives === 0 بود بنویس Game Over. این‌طوری lives هیچ‌وقت منفی نمی‌شود. (جلسه‌ی بعد راه تمیزتری یاد می‌گیری: حالت‌های بازی.)

  salam.loop(function (dt) {
    if (lives > 0) {
      meteor.y = meteor.y + 240 * dt;
      if (salam.hit(meteor, hero)) {
        lives = lives - 1;
        meteor.y = 0;
      }
    }

    // ... draw everything ...
    if (lives === 0) {
      ctx.fillStyle = "white";
      ctx.font = "40px sans-serif";
      ctx.fillText("Game Over", 80, 240);
    }
  });

رکورد! وقتی صفحه بسته شود، همه‌ی متغیرها پاک می‌شوند. ولی salam.save("best", best) عدد را با اسم best نگه می‌دارد، و salam.load("best", 0) دفعه‌ی بعد پس‌اش می‌دهد. عدد دوم (0) جوابِ دفعه‌ی اول است، وقتی هنوز چیزی ذخیره نشده. پس: اول بازی let best = salam.load("best", 0); و هر وقت score از best بیشتر شد، best = score و salam.save("best", best).

<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>

اشتباه‌های رایج: (۱) let score = 0 را داخل حلقه می‌نویسی؛ آن‌وقت هر فریم دوباره صفر می‌شود. متغیرهای امتیاز و جان بیرون از حلقه ساخته می‌شوند. (۲) fillText را بیرون از حلقه می‌گذاری؛ فقط یک بار کشیده می‌شود و فریم بعد زیر آسمان گم می‌شود. (۳) salam.save را بدون اسم صدا می‌زنی. همیشه دو چیز می‌خواهد: اسم و مقدار.

همین در پایتون: در Pygame Zero امتیاز و جان هم متغیرند؛ داخل update باید global score, lives بنویسی تا عوضشان کنی، و با screen.draw.text روی صفحه می‌نویسی. (دوره‌ی «بازی‌سازی با پایتون»، جلسه‌ی ۱۱)

score = 0
lives = 3

def update():
    global score, lives
    if lives == 0:
        return
    if hero.colliderect(coin):
        score += 1
        coin.x = random.randint(20, 340)
    if hero.colliderect(meteor):
        lives -= 1
        meteor.y = 0

def draw():
    screen.fill("midnightblue")
    hero.draw()
    coin.draw()
    meteor.draw()
    screen.draw.text("Score: " + str(score), (10, 10), fontsize=30)
    screen.draw.text("<3 " * lives, (260, 10), fontsize=30, color="red")

مثال‌های این جلسه

چالش‌ها

  1. First Point (آسان)
  2. Tap for Points (آسان)
  3. Three Hearts (آسان)
  4. Catch or Miss (متوسط)
  5. No More Lives (متوسط)
  6. Best Score (متوسط)
  7. Extra Life (سخت)
  8. Record Breaker (سخت)

بیشتر بخوان

← جلسه‌ی 9: برخورد جلسه‌ی 11: شروع، بازی، پایان →