جواب «Each Its Own Speed» چالش 5 از جلسهی 14
سؤال
هر لمس یک ستاره در بالای صحنه میسازد. به هر ستاره یک سرعت شانسی بده: speed: salam.random(100, 300)، و هر ستاره را با سرعت خودش حرکت بده: s.y = s.y + s.speed * dt.
راهنما
در push یک ویژگی تازه اضافه کن: { x: x, y: 0, w: 20, h: 20, speed: salam.random(100, 300) }. در for، 100 را به s.speed عوض کن.
یک جواب درست
<script>
const ctx = salam.canvas(360, 480);
let stars = [];
salam.tap(function (x, y) {
stars.push({ x: x, y: 0, w: 20, h: 20, speed: salam.random(100, 300) });
});
salam.loop(function (dt) {
for (const s of stars) {
s.y = s.y + s.speed * dt;
}
stars = stars.filter(function (s) { return s.y < 480; });
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>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن