جواب «One Every Second» چالش 4 از جلسهی 14
سؤال
هر ۱ ثانیه یک ستارهی تازه به stars اضافه کن: { x: salam.random(0, 340), y: -20, w: 20, h: 20 }.
ستارهها خودشان ثانیهای ۱۰۰ پیکسل میافتند. از timer برای شمردن ثانیه استفاده کن.
راهنما
اول حلقه:timer = timer + dt;if (timer >= 1) { timer = 0; stars.push({ x: salam.random(0, 340), y: -20, w: 20, h: 20 }); }
یک جواب درست
<script>
const ctx = salam.canvas(360, 480);
let stars = [];
let timer = 0;
salam.loop(function (dt) {
timer = timer + dt;
if (timer >= 1) {
timer = 0;
stars.push({ x: salam.random(0, 340), y: -20, w: 20, h: 20 });
}
for (const s of stars) {
s.y = s.y + 100 * dt;
}
ctx.fillStyle = "midnightblue";
ctx.fillRect(0, 0, 360, 480);
ctx.fillStyle = "gold";
for (const s of stars) {
ctx.fillRect(s.x, s.y, s.w, s.h);
}
});
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن