جواب «Step 3: The Hero» چالش 3 از جلسهی 4
سؤال
نوبت قهرمان است:
- یک شیء بساز: const hero = { x: 0, y: 220, w: 80, h: 80 };
- در خط بعد hero.x را طوری حساب کن که قهرمان درست وسط صحنه باشد (با کمک hero.w).
- عکس "hero" را با hero.x، hero.y، hero.w و hero.h بکش.
راهنما
وسط صحنه x = 180 است و x قهرمان لبهی چپش است؛ پس نصفِ پهنا را کم کن: hero.x = 180 - hero.w / 2; که میشود ۱۴۰.
یک جواب درست
<script>
const ctx = salam.canvas(360, 480);
ctx.drawImage(salam.image("night"), 0, 0, 360, 480);
const title = "Star Catcher";
ctx.fillStyle = "white";
ctx.font = "bold 40px sans-serif";
ctx.textAlign = "center";
ctx.fillText(title, 180, 160);
const hero = { x: 0, y: 220, w: 80, h: 80 };
hero.x = 180 - hero.w / 2;
ctx.drawImage(salam.image("hero"), hero.x, hero.y, hero.w, hero.h);
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن