Step 2: Meteors Fall چالش 2 از جلسهی 16، متوسط
قدم ۲: شهابسنگها!
- یک آرایهی meteors و یک شمارندهی timer بساز.
- هر ۱ ثانیه یک شهاب تازه: { x: salam.random(0, 330), y: -30, w: 30, h: 30 }.
- همهی شهابها ثانیهای ۲۰۰ پیکسل بیفتند و با عکس salam.image("meteor") کشیده شوند.
- شهابهایی که yشان از ۴۸۰ بیشتر شد با filter پاک شوند (return m.y <= 480;).
<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>
راهنما و جواب این چالش · برگشت به درس جلسهی 16: پروژه: جاخالی · چالش بعدی: Step 3: Crash and Score
بیشتر بخوان: دیکشنری در پایتون: آموزش کامل کلید و مقدار با مثال