Best Score چالش 6 از جلسهی 10، متوسط
بازی «روی ستاره بزن» امتیاز میدهد. حالا بهترین رکورد را نگه دار:
- let best = salam.load("best", 0); بیرون از حلقه.
- هر وقت score از best بیشتر شد: best = score و salam.save("best", best).
- best را هم زیر امتیاز بنویس، مثلاً Best: 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>
راهنما و جواب این چالش · برگشت به درس جلسهی 10: امتیاز و جان · چالش بعدی: Extra Life
بیشتر بخوان: دیکشنری در پایتون: آموزش کامل کلید و مقدار با مثال