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

Off the Stage چالش 5 از جلسه‌ی 13، متوسط

سه ستاره ثانیه‌ای ۲۰۰ پیکسل می‌افتند، ولی وقتی از صحنه بیرون می‌روند هنوز در آرایه‌اند. بعد از حرکت دادن، با filter فقط ستاره‌هایی را نگه دار که yشان کمتر از ۴۸۰ است:
stars = stars.filter(function (s) { return s.y < 480; });
stars.length باید کم‌کم از ۳ به ۰ برسد.

<script>
  const ctx = salam.canvas(360, 480);
  let stars = [
    { x: 60, y: 0, w: 20, h: 20 },
    { x: 170, y: 200, w: 20, h: 20 },
    { x: 280, y: 400, w: 20, h: 20 },
  ];

  salam.loop(function (dt) {
    for (const s of stars) {
      s.y = s.y + 200 * 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);
    }
    ctx.fillStyle = "white";
    ctx.font = "20px sans-serif";
    ctx.fillText("Stars: " + stars.length, 10, 30);
  });
</script>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 13: چند دشمن · چالش بعدی: Rain of Stars

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