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

Buttons for the Phone چالش 1 از جلسه‌ی 8، آسان

این قهرمان با کیبورد راه می‌رود، ولی روی گوشی نه. با salam.touchKeys زیر صحنه دو دکمه بساز: یکی برای ArrowLeft و یکی برای ArrowRight.
یادت باشد: یک بار، بیرون از حلقه.

<script>
  const ctx = salam.canvas(360, 480);
  const hero = { x: 160, y: 420, w: 40, h: 40 };

  salam.loop(function (dt) {
    if (salam.keys.ArrowLeft) hero.x = hero.x - 250 * dt;
    if (salam.keys.ArrowRight) hero.x = hero.x + 250 * dt;
    hero.x = Math.max(0, Math.min(360 - hero.w, hero.x));

    ctx.fillStyle = "skyblue";
    ctx.fillRect(0, 0, 360, 480);
    ctx.fillStyle = "tomato";
    ctx.fillRect(hero.x, hero.y, hero.w, hero.h);
  });
</script>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 8: لمس و موس · چالش بعدی: Jump Button

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