جواب «Harder With Time» چالش 6 از جلسهی 15
سؤال
این بار بازی با زمان سخت میشود، نه با امتیاز: هر ۵ ثانیه، speed ۲۰ تا زیاد شود (۱۰۰ ← ۱۲۰ ← ۱۴۰ …).
از یک شمارندهی timer استفاده کن: هر فریم timer = timer + dt؛ وقتی timer >= 5 شد، speed را ۲۰ تا زیاد کن و timer را صفر کن.
راهنما
let timer = 0; بالا. اول حلقه:timer = timer + dt;if (timer >= 5) { timer = 0; speed = speed + 20; }
یک جواب درست
<script>
const ctx = salam.canvas(360, 480);
const star = { x: 160, y: 0, w: 40, h: 40 };
let score = 0;
let speed = 100;
let timer = 0;
salam.tap(function (x, y) {
if (salam.hit({ x: x, y: y, w: 1, h: 1 }, star)) {
score = score + 1;
star.y = 0;
}
});
salam.loop(function (dt) {
timer = timer + dt;
if (timer >= 5) {
timer = 0;
speed = speed + 20;
}
star.y = star.y + speed * dt;
if (star.y > 480) star.y = 0;
ctx.fillStyle = "midnightblue";
ctx.fillRect(0, 0, 360, 480);
ctx.fillStyle = "gold";
ctx.fillRect(star.x, star.y, star.w, star.h);
ctx.fillStyle = "white";
ctx.font = "20px sans-serif";
ctx.fillText("Score: " + score, 10, 30);
});
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن