One Every Second چالش 4 از جلسهی 14، متوسط
هر ۱ ثانیه یک ستارهی تازه به stars اضافه کن: { x: salam.random(0, 340), y: -20, w: 20, h: 20 }.
ستارهها خودشان ثانیهای ۱۰۰ پیکسل میافتند. از timer برای شمردن ثانیه استفاده کن.
<script>
const ctx = salam.canvas(360, 480);
let stars = [];
let timer = 0;
salam.loop(function (dt) {
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>
راهنما و جواب این چالش · برگشت به درس جلسهی 14: تصادفی · چالش بعدی: Each Its Own Speed
بیشتر بخوان: حلقه for در پایتون: آموزش کامل با range و مثال