بتل کد Battle Codeدارد باز می‌شود…

Each Its Own Speed چالش 5 از جلسه‌ی 14، متوسط

هر لمس یک ستاره در بالای صحنه می‌سازد. به هر ستاره یک سرعت شانسی بده: speed: salam.random(100, 300)، و هر ستاره را با سرعت خودش حرکت بده: s.y = s.y + s.speed * dt.

<script>
  const ctx = salam.canvas(360, 480);
  let stars = [];

  salam.tap(function (x, y) {
    stars.push({ x: x, y: 0, w: 20, h: 20 });
  });

  salam.loop(function (dt) {
    for (const s of stars) {
      s.y = s.y + 100 * 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>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 14: تصادفی · چالش بعدی: Party Colours

بیشتر بخوان: حلقه for در پایتون: آموزش کامل با range و مثال