جواب «Extra Life» چالش 7 از جلسهی 10
سؤال
در بازی «روی ستاره بزن» حالا lives = 3 هم هست و قلبها بالای صحنهاند.
هدیه بده: هر بار که score به یک مضرب ۵ رسید (۵، ۱۰، ۱۵ …)، یک جان اضافه بده. ولی جانها هیچوقت از ۵ بیشتر نشوند.
راهنما
score % 5 باقیماندهی تقسیم بر ۵ است؛ اگر 0 بود، score مضرب ۵ است. بعد از زیاد کردن امتیاز: if (score % 5 === 0) { lives = Math.min(lives + 1, 5); }
یک جواب درست
<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 lives = 3;
const heart = salam.image("heart");
salam.tap(function (x, y) {
if (salam.hit({ x: x, y: y, w: 1, h: 1 }, star)) {
score = score + 1;
if (score % 5 === 0) {
lives = Math.min(lives + 1, 5);
}
}
});
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);
for (let i = 0; i < lives; i++) {
ctx.drawImage(heart, 330 - i * 30, 10, 24, 24);
}
});
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن