מפגש 43: pygame.sprite, Groups, Mask, ירושה, גלים ובוס.Session 43: pygame.sprite, Groups, Mask, inheritance, waves and a boss.
pygame.sprite, Groups, Mask, ירושה, גלים ובוס. בסוף המפגש מוסיפים צעד לפרויקט המלווה.pygame.sprite, Groups, Mask, inheritance, waves and a boss. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
SpritesSprites
יריShooting
פגיעותHits
גליםWaves
חלקיקיםParticles
משחקGame
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
נושאTopic 1
SpritesSprites
Sprite וקבוצותSprites and groups
Sprite = image + rect + updateA Sprite = image + rect + update
sprite.kill() מוציא מכל הקבוצותsprite.kill() removes it from every group
כשיש עשרות עצמים, כדאי להיעזר במערכת של pygame. Sprite הוא מחלקה עם שתי תכונות חובה: image ו-rect. קבוצה מעדכנת ומציירת את כולם בשורה אחת, ו-kill() מוציא Sprite מכל קבוצה שהוא בה. זו ירושה (מפגש 16) בפעולה:With dozens of objects, pygame's system helps. A Sprite is a class with two required attributes: image and rect. A group updates and draws all of them in one line, and kill() removes a Sprite from every group it's in. It's inheritance (session 16) in action:
אנלוגיהAnalogy
קבוצת Sprites היא כמו מנצח על תזמורת: אומר "עכשיו כולם" פעם אחת, וכל נגן מנגן את התפקיד שלו.A sprite group is like a conductor: says "everyone, now" once, and each player plays their own part.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
ירייה = Sprite שזז למעלהA bullet = a Sprite moving up
kill() כשיצאה מהמסךkill() when it leaves the screen
אם יורים בכל פריים שבו מקש הירי לחוץ, יוצאות 60 יריות בשנייה ושום אתגר. זמן המתנה הוא מונה: אחרי ירייה קובעים אותו ל-12, הוא יורד ב-1 בכל פריים, ויורים רק כשהוא הגיע לאפס. ירייה שעברה את ראש המסך נמחקת, אחרת הקבוצה הייתה גדלה לנצח:If you shoot every frame the fire key is held, 60 bullets a second fly out and there's no challenge. A cooldown is a counter: after a shot set it to 12, it drops by 1 every frame, and you fire only when it reaches zero. A bullet past the top of the screen is removed, otherwise the group would grow forever:
אנלוגיהAnalogy
cooldown הוא כמו מכונת קפה שצריכה להתחמם בין כוס לכוס: אפשר ללחוץ כמה שרוצים, היא תמזוג רק כשהיא מוכנה.A cooldown is like a coffee machine that has to reheat between cups: press as much as you like, it pours only when ready.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
מחזיר מילון: מי פגע במיReturns a dict: who hit whom
spritecollide לאחד מול קבוצהspritecollide for one against a group
pygame.sprite.groupcollide בודק כל אויב מול כל ירייה ומחזיר מילון: מפתח = אויב שנפגע, ערך = רשימת היריות שפגעו בו. שני הפרמטרים האחרונים אומרים אם למחוק את הנפגעים. spritecollide בודק Sprite אחד מול קבוצה, למשל החללית מול יריות אויב:pygame.sprite.groupcollide checks every enemy against every bullet and returns a dict: key = an enemy that was hit, value = the list of bullets that hit it. The last two arguments say whether to remove what was hit. spritecollide checks one Sprite against a group, e.g. the ship against enemy bullets:
אנלוגיהAnalogy
groupcollide הוא כמו שופט במשחק כדור-מחניים: עובר על כל הזוגות, מכריז מי נפגע, ושולח אותם החוצה.groupcollide is like a dodgeball referee: checks every pair, announces who was hit, and sends them off.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 4
גליםWaves
גלי אויביםEnemy waves
גל = מערך של אויביםA wave = a formation of enemies
זזים יחד, יורדים בקצהMove together, drop at the edge
הגל הבא מהיר יותרThe next wave is faster
גל אויבים נבנה בלולאה כפולה (כמו לבני Brick Breaker). כל הגל זז יחד הצידה, וכשאחד נוגע בקצה כולם יורדים שורה והופכים כיוון. כשהקבוצה מתרוקנת, בונים גל חדש מהיר יותר. מספר הגל קובע את הקושי:An enemy wave is built with a double loop (like Brick Breaker's bricks). The whole wave moves sideways together, and when one touches the edge they all drop a row and turn around. When the group empties, build a new, faster wave. The wave number sets the difficulty:
אנלוגיהAnalogy
גל אויבים הוא כמו להקת ציפורים: כולן פונות יחד כשהראשונה מגיעה לקצה.An enemy wave is like a flock of birds: they all turn together when the first reaches the edge.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 5
חלקיקיםParticles
חלקיקים ורקע כוכביםParticles and a starfield
חלקיק: מיקום, מהירות, זמן חייםA particle: position, velocity, lifetime
פיצוץ = 12 חלקיקים בכיוונים אקראייםAn explosion = 12 particles in random directions
כוכבים במהירויות שונות = עומקStars at different speeds = depth
פיצוץ הוא כמה חלקיקים: לכל אחד מיקום, מהירות אקראית וזמן חיים שיורד. כשהזמן נגמר, החלקיק נמחק. צבע שתלוי בזמן החיים גורם לניצוץ לדעוך. ורקע של כוכבים שזזים במהירויות שונות נותן תחושת טיסה (זוכרים פרלקסה?):An explosion is a few particles: each with a position, a random velocity and a lifetime that counts down. When time runs out, the particle is removed. A colour depending on lifetime makes the spark fade. And a background of stars moving at different speeds gives a sense of flight (remember parallax?):
אנלוגיהAnalogy
חלקיקים הם כמו זיקוקים: כל ניצוץ טס לבד, דועך, ונעלם, ויחד הם נראים כמו פיצוץ אחד.Particles are like fireworks: each spark flies alone, fades and vanishes, and together they look like one burst.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 6
משחקGame
Space Shooter: המשחקSpace Shooter: the game
חללית, יריות ואויבים כ-SpritesShip, bullets and enemies as Sprites
groupcollide לפגיעותgroupcollide for hits
חלקיקים וכוכביםParticles and stars
הכול יחד: חללית, יריות עם cooldown, גל אויבים שצועד ויורד, פגיעות עם groupcollide, ניצוצות פיצוץ, וכוכבים שזורמים למטה. כאן החללית מכוונת לבד לאויב הקרוב ויורה אוטומטית; במחשב שלכם החיצים והרווח:Everything together: a ship, bullets with a cooldown, an enemy wave that marches and drops, hits with groupcollide, explosion sparks, and stars streaming down. Here the ship aims at the nearest enemy and fires automatically; on your computer, the arrows and space:
אנלוגיהAnalogy
המשחק הזה הוא מה שקורה כשמחלקות, קבוצות ורשימות עובדות יחד: כל עצם יודע מה לעשות, והלולאה רק מתזמרת.This game is what happens when classes, groups and lists work together: each object knows what to do, and the loop just conducts.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
Sprite = image + rect + updateA Sprite = image + rect + update