مثال دیکشنری در پایتون: const hero = { x: 160, y: 420, w: 40, h: 40… — بازی من ۲
مثال 3 از درس جلسهی 19 آموزش بازیسازی با HTML و JavaScript برای نوجوانان: بازی من ۲
عددهای مهم بازی را بالای کد در متغیر بگذار: سرعت قهرمان، سرعت افتادن، هر چند ثانیه یک دشمن، تعداد جانها. اینطوری بعداً برای آسانتر یا سختتر کردن بازی، فقط یک عدد را عوض میکنی و لازم نیست وسط کد دنبالش بگردی. اسمهای روشن هم بگذار: fallSpeed خیلی بهتر از s است.
<script>
const ctx = salam.canvas(360, 480);
salam.touchKeys(["ArrowLeft", "ArrowRight"]);
// the numbers that make the game easy or hard
const heroSpeed = 250;
const fallSpeed = 180;
const everySeconds = 1;
const startLives = 3;
const hero = { x: 160, y: 420, w: 40, h: 40 };
let lives = startLives;
salam.loop(function (dt) {
if (salam.keys.ArrowLeft) hero.x = hero.x - heroSpeed * dt;
if (salam.keys.ArrowRight) hero.x = hero.x + heroSpeed * dt;
hero.x = Math.max(0, Math.min(320, hero.x));
ctx.fillStyle = "skyblue";
ctx.fillRect(0, 0, 360, 480);
ctx.drawImage(salam.image("bee"), hero.x, hero.y, hero.w, hero.h);
});
</script>
این کد را همینجا در ویرایشگر پایتون اجرا کن، عددها و متنهایش را عوض کن و دوباره اجرا کن تا ببینی چه تغییری میکند.
بیشتر بخوان: دیکشنری در پایتون: آموزش کامل کلید و مقدار با مثال