Record Breaker چالش 8 از جلسهی 10، سخت
بازی گرفتن ستاره امتیاز و جان دارد و وقتی جانها تمام شد میایستد.
رکورد را اضافه کن، ولی فقط آخر بازی:
- let best = salam.load("best", 0);
- درست همان لحظه که lives صفر میشود، اگر score از best بیشتر بود: best = score و salam.save("best", best).
- وسط بازی best عوض نشود. Best: ... را هم روی صحنه بنویس.
<script>
const ctx = salam.canvas(360, 480);
const hero = { x: 140, y: 420, w: 70, h: 30 };
const star = { x: 160, y: 0, w: 30, h: 30 };
const heroPic = salam.image("hero");
const starPic = salam.image("star");
const heart = salam.image("heart");
let score = 0;
let lives = 3;
salam.touchKeys(["ArrowLeft", "ArrowRight"]);
salam.loop(function (dt) {
if (lives > 0) {
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(360 - hero.w, hero.x));
star.y = star.y + 240 * dt;
if (salam.hit(star, hero)) {
score = score + 1;
star.y = 0;
}
if (star.y > 480) {
lives = lives - 1;
star.y = 0;
}
}
ctx.fillStyle = "#1b1440";
ctx.fillRect(0, 0, 360, 480);
ctx.drawImage(starPic, star.x, star.y, star.w, star.h);
ctx.drawImage(heroPic, hero.x, hero.y, hero.w, hero.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);
}
if (lives === 0) {
ctx.font = "40px sans-serif";
ctx.fillText("Game Over", 80, 240);
}
});
</script>
راهنما و جواب این چالش · برگشت به درس جلسهی 10: امتیاز و جان
بیشتر بخوان: دیکشنری در پایتون: آموزش کامل کلید و مقدار با مثال