Jump Button چالش 2 از جلسهی 8، آسان
قهرمان روی گوشی راه میرود، ولی نمیپرد. یک دکمهی سوم برای کلید فاصله (" ") به salam.touchKeys اضافه کن، بعد از دو دکمهی دیگر.
<script>
const ctx = salam.canvas(360, 480);
const hero = { x: 160, y: 420, w: 40, h: 40 };
let vy = 0;
salam.touchKeys(["ArrowLeft", "ArrowRight"]);
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));
if (salam.keys[" "] && hero.y === 420) vy = -500;
vy = vy + 1200 * dt;
hero.y = hero.y + vy * dt;
if (hero.y > 420) {
hero.y = 420;
vy = 0;
}
ctx.fillStyle = "skyblue";
ctx.fillRect(0, 0, 360, 480);
ctx.fillStyle = "tomato";
ctx.fillRect(hero.x, hero.y, hero.w, hero.h);
});
</script>
راهنما و جواب این چالش · برگشت به درس جلسهی 8: لمس و موس · چالش بعدی: Catch the Star
بیشتر بخوان: دیکشنری در پایتون: آموزش کامل کلید و مقدار با مثال