جلسهی 13: چند دشمن Many Things
به جای یک ستاره، یک عالمه ستاره! همه را در یک آرایه نگه میداری، با یک for همه را حرکت میدهی و میکشی، با push ستارهی تازه اضافه میکنی و با filter آنهایی را که از صحنه بیرون رفتند پاک میکنی. آخرش یک باران ستاره میسازی.
در این جلسه یاد میگیری
- در پایان این جلسه میتوانی چند شخصیت را در یک آرایه از شیءها نگه داری.
- در پایان این جلسه میتوانی با
for (const s of stars)همهی آنها را حرکت بدهی و بکشی. - در پایان این جلسه میتوانی با
pushوسط بازی چیز تازه اضافه کنی. - در پایان این جلسه میتوانی با
filterچیزهایی را که از صحنه بیرون رفتند یا گرفته شدند حذف کنی.
درس
تا حالا هر شخصیت یک متغیر داشت: hero، ball. ولی اگر ده ستاره بخواهی چه؟ star1، star2، … star10؟ خستهکننده است! به جایش همه را در یک آرایه میگذاری. آرایه با [ و ] ساخته میشود و هر عضوش اینجا یک شیء {x, y, w, h} است. ستارهی اول stars[0] است، دومی stars[1]، و stars.length میگوید چند ستاره داری.
<script>
const ctx = salam.canvas(360, 480);
const stars = [
{ x: 60, y: 80, w: 20, h: 20 },
{ x: 170, y: 150, w: 20, h: 20 },
{ x: 280, y: 60, w: 20, h: 20 },
];
ctx.fillStyle = "midnightblue";
ctx.fillRect(0, 0, 360, 480);
ctx.fillStyle = "gold";
ctx.fillRect(stars[0].x, stars[0].y, stars[0].w, stars[0].h);
ctx.fillStyle = "white";
ctx.font = "20px sans-serif";
ctx.fillText("Stars: " + stars.length, 10, 460);
</script>در کد بالا فقط ستارهی اول کشیده شد. برای کشیدن همه، for (const s of stars) بنویس. معنیاش این است: «برای هر ستاره در stars، اسمش را بگذار s و این کار را بکن.» سه ستاره یا سیصد ستاره، کد همین چند خط است.
<script>
const ctx = salam.canvas(360, 480);
const stars = [
{ x: 60, y: 80, w: 20, h: 20 },
{ x: 170, y: 150, w: 20, h: 20 },
{ x: 280, y: 60, w: 20, h: 20 },
];
ctx.fillStyle = "midnightblue";
ctx.fillRect(0, 0, 360, 480);
ctx.fillStyle = "gold";
for (const s of stars) {
ctx.fillRect(s.x, s.y, s.w, s.h);
}
</script>حرکت دادن هم همینطور است. در salam.loop، داخل همان for، y هر ستاره را زیاد کن. یادت هست؟ سرعت «پیکسل در ثانیه» است و با dt ضرب میشود. اینجا هر ستاره ثانیهای ۱۰۰ پیکسل پایین میآید.
<script>
const ctx = salam.canvas(360, 480);
const stars = [
{ x: 60, y: 0, w: 20, h: 20 },
{ x: 170, y: 80, w: 20, h: 20 },
{ x: 280, y: 160, w: 20, h: 20 },
];
salam.loop(function (dt) {
ctx.fillStyle = "midnightblue";
ctx.fillRect(0, 0, 360, 480);
ctx.fillStyle = "gold";
for (const s of stars) {
s.y = s.y + 100 * dt;
ctx.fillRect(s.x, s.y, s.w, s.h);
}
});
</script>push یک عضو تازه به ته آرایه اضافه میکند. اینجا هر بار که صحنه را لمس کنی (یا کلیک کنی)، یک ستارهی تازه همانجا ساخته میشود و شروع به افتادن میکند. چند بار پشت هم لمس کن!
<script>
const ctx = salam.canvas(360, 480);
const stars = [];
salam.tap(function (x, y) {
stars.push({ x: x, y: y, w: 20, h: 20 });
});
salam.loop(function (dt) {
ctx.fillStyle = "midnightblue";
ctx.fillRect(0, 0, 360, 480);
ctx.fillStyle = "gold";
for (const s of stars) {
s.y = s.y + 100 * dt;
ctx.fillRect(s.x, s.y, s.w, s.h);
}
ctx.fillStyle = "white";
ctx.font = "20px sans-serif";
ctx.fillText("Stars: " + stars.length, 10, 30);
});
</script>در کد بالا ستارهها از پایین صحنه بیرون میروند، ولی هنوز در آرایهاند و Stars: مدام بزرگتر میشود. بازی بیخودی آنها را حرکت میدهد! filter یک آرایهی تازه میسازد فقط با عضوهایی که تابعش برایشان true برمیگرداند. «فقط ستارههایی را نگه دار که yشان هنوز کمتر از ۴۸۰ است.» چون آرایه عوض میشود، این بار stars را با let بساز، نه const.
<script>
const ctx = salam.canvas(360, 480);
let stars = [];
salam.tap(function (x, y) {
stars.push({ x: x, y: y, w: 20, h: 20 });
});
salam.loop(function (dt) {
for (const s of stars) {
s.y = s.y + 100 * dt;
}
stars = stars.filter(function (s) {
return s.y < 480;
});
ctx.fillStyle = "midnightblue";
ctx.fillRect(0, 0, 360, 480);
ctx.fillStyle = "gold";
for (const s of stars) {
ctx.fillRect(s.x, s.y, s.w, s.h);
}
ctx.fillStyle = "white";
ctx.font = "20px sans-serif";
ctx.fillText("Stars: " + stars.length, 10, 30);
});
</script>با filter ستارههای گرفتهشده را هم برمیداری: «فقط ستارههایی را نگه دار که به سبد نخوردهاند.» علامت ! یعنی «نه». هر ستارهای که به سبد خورد، قبل از حذف شدن یک امتیاز میدهد. با دکمههای زیر صحنه سبد را تکان بده.
<script>
const ctx = salam.canvas(360, 480);
const basket = { x: 150, y: 420, w: 60, h: 20 };
let stars = [
{ x: 40, y: 0, w: 20, h: 20 },
{ x: 170, y: -150, w: 20, h: 20 },
{ x: 300, y: -300, w: 20, h: 20 },
];
let score = 0;
salam.touchKeys(["ArrowLeft", "ArrowRight"]);
salam.loop(function (dt) {
if (salam.keys.ArrowLeft) basket.x = basket.x - 300 * dt;
if (salam.keys.ArrowRight) basket.x = basket.x + 300 * dt;
basket.x = Math.max(0, Math.min(360 - basket.w, basket.x));
for (const s of stars) {
s.y = s.y + 120 * dt;
if (salam.hit(s, basket)) score = score + 1;
}
stars = stars.filter(function (s) {
return !salam.hit(s, basket);
});
ctx.fillStyle = "midnightblue";
ctx.fillRect(0, 0, 360, 480);
ctx.fillStyle = "gold";
for (const s of stars) ctx.fillRect(s.x, s.y, s.w, s.h);
ctx.fillStyle = "sienna";
ctx.fillRect(basket.x, basket.y, basket.w, basket.h);
ctx.fillStyle = "white";
ctx.font = "20px sans-serif";
ctx.fillText("Score: " + score, 10, 30);
});
</script>اشتباههای رایج: stars را با const بسازی و بعد بنویسی stars = stars.filter(...) (خطا میدهد؛ let لازم است)؛ در تابعِ filter کلمهی return یادت برود (آنوقت همهی ستارهها پاک میشوند!)؛ یا ستارهی اول را stars[1] بدانی (شمارش از 0 شروع میشود).
همین در پایتون: در Pygame Zero هم بازیگرها را در لیست میگذاری، با for star in stars: همه را حرکت میدهی و با append اضافه میکنی. برای حذفِ آنهایی که بیرون رفتند، روی کپی لیست (stars[:]) میچرخی و remove میکنی. (دورهی «بازیسازی با پایتون»، جلسهی ۱۰)
stars = []
def on_mouse_down(pos):
stars.append(Actor("star", pos))
def update():
for star in stars[:]:
star.y += 3
if star.top > HEIGHT:
stars.remove(star)
def draw():
screen.fill("midnightblue")
for star in stars:
star.draw()مثالهای این جلسه
- مثال لیست در پایتون: const stars = [
- مثال حلقه for در پایتون: for (const s of stars) {
- مثال حلقه for در پایتون: for (const s of stars) { — چند دشمن
- مثال حلقه for در پایتون: for (const s of stars) { — چند دشمن 2
- مثال حلقه for در پایتون: for (const s of stars) { — چند دشمن 2 3
- مثال دیکشنری در پایتون: const basket = { x: 150, y: 420, w: 60, h: …
- مثال بازیسازی با Pygame Zero در پایتون: stars.append(Actor("star", pos))
چالشها
- One More Star (آسان)
- Everyone Falls (آسان)
- Tap a Star (آسان)
- Endless Snow (متوسط)
- Off the Stage (متوسط)
- Rain of Stars (متوسط)
- Catch the Stars (سخت)
- Broken Rain (سخت)