جواب «Step 1: The Hero Dodges» چالش 1 از جلسهی 16
سؤال
قدم ۱: موشک (hero) را بساز: { x: 160, y: 420, w: 40, h: 40 } با عکس salam.image("rocket").
- با ArrowLeft و ArrowRight ثانیهای ۲۵۰ پیکسل حرکت کند.
- از صحنه بیرون نرود (x از ۰ تا ۳۲۰).
- روی گوشی هم کار کند: salam.touchKeys(["ArrowLeft", "ArrowRight"]).
راهنما
مثل جلسهی ۷ و ۸: if (salam.keys.ArrowLeft) hero.x = hero.x - 250 * dt; و برای راست هم. بعد hero.x = Math.max(0, Math.min(360 - hero.w, hero.x));. عکس را بعد از پسزمینه با ctx.drawImage(heroImg, hero.x, hero.y, hero.w, hero.h) بکش.
یک جواب درست
<script>
const ctx = salam.canvas(360, 480);
const hero = { x: 160, y: 420, w: 40, h: 40 };
const heroImg = salam.image("rocket");
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));
ctx.fillStyle = "#14123a";
ctx.fillRect(0, 0, 360, 480);
ctx.drawImage(heroImg, hero.x, hero.y, hero.w, hero.h);
});
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن