מפגש 46: מסלול עם BFS, גלים, בחירת מטרה, כלכלה וממשק כפתורים.Session 46: Pathing with BFS, waves, targeting, an economy and a button UI.
מסלול עם BFS, גלים, בחירת מטרה, כלכלה וממשק כפתורים. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Pathing with BFS, waves, targeting, an economy and a button UI. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
מסלולPath
טווחRange
קצבRate
קליעיםBullets
גליםWaves
משחקGame
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
צעד לעבר הנקודה הבאה בכיוון מנורמלStep toward the next point in a normalised direction
מגיעים? מתקדמים לנקודה הבאהArrived? Move to the next point
ב-Tower Defense האויבים לא חופשיים: הם הולכים במסלול קבוע שמוגדר כרשימת נקודות ציון. בכל פריים מחשבים את הכיוון לנקודה הבאה, מנרמלים אותו (מחלקים באורך, כמו בפרק הווקטורים), וזזים צעד באורך המהירות. אם הנקודה קרובה יותר מצעד אחד, קופצים אליה ועוברים לבאה, כך שאין "ריצוד" סביב הנקודה:In Tower Defense enemies aren't free: they walk a fixed path defined as a list of waypoints. Each frame, compute the direction to the next point, normalise it (divide by the length, as in the vectors lesson), and move one speed-length step. If the point is closer than one step, snap to it and move on, so there's no "jitter" around the point:
אנלוגיהAnalogy
נקודות ציון הן כמו שלטים בשביל טיול: לא צריך לדעת את כל הדרך, רק לאן השלט הבא מצביע.Waypoints are like trail markers: you don't need the whole route, just where the next marker points.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 2
טווחRange
טווח ובחירת מטרהRange and targeting
בטווח = math.dist(...) <= rangeIn range = math.dist(...) <= range
מגדל רואה רק אויבים בתוך עיגול הטווח שלו: math.dist נותן מרחק בין שתי נקודות. מבין אלה שבטווח צריך לבחור מטרה. האסטרטגיה הנפוצה היא "הראשון": מי שהתקדם הכי רחוק במסלול, כי הוא הכי קרוב לגרום נזק. max עם key עושה את זה בשורה, ואפשר להחליף לקרוב ביותר או לחזק ביותר:A tower sees only enemies inside its range circle: math.dist gives the distance between two points. Among those in range you need to pick a target. The common strategy is "first": whoever has travelled furthest along the path, since they're closest to doing damage. max with key does it in one line, and you can swap to closest or strongest:
אנלוגיהAnalogy
זה כמו שוער שבוחר באיזה כדור לטפל: לא בכל מה שבמגרש, רק במה שקרוב לשער, ובדרך כלל במה שהכי קרוב להבקיע.It's like a goalkeeper choosing which ball to handle: not everything on the pitch, just what's near the goal, usually whatever's closest to scoring.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
קצב = 60 / cooldown יריות בשנייהRate = 60 / cooldown shots a second
בלי הגבלה מגדל היה יורה בכל פריים, 60 פעם בשנייה. זמן טעינה הוא מונה: כשיורים מציבים בו מספר, בכל פריים מורידים אחד, ויורים שוב רק כשהגיע לאפס. ההבדל בין מגדל מהיר לאיטי הוא רק המספר הזה. הנה שלושה מגדלים לאורך שתי שניות:Without a limit a tower would fire every frame, 60 times a second. A cooldown is a counter: on firing you set it, each frame you subtract one, and you fire again only when it reaches zero. The only difference between a fast and a slow tower is that number. Here are three towers over two seconds:
אנלוגיהAnalogy
זמן טעינה הוא כמו מכונת קפה: לוחצים, מקבלים כוס, ועד שהיא מוכנה שוב שום לחיצה לא תעזור.A cooldown is like a coffee machine: press, get a cup, and until it's ready again no press will help.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 4
קליעיםBullets
קליעים מונחיםHoming projectiles
קליע מונחה מכוון מחדש בכל פרייםA homing bullet re-aims every frame
פגיעה = מרחק קטן מסףA hit = distance below a threshold
המטרה מתה בדרך? הקליע נעלםTarget died on the way? The bullet vanishes
קליע ישר עלול לפספס אויב זז. קליע מונחה מחשב בכל פריים מחדש את הכיוון אל המטרה ומתקדם אליה, בדיוק כמו אויב שהולך לנקודת ציון, רק שהנקודה זזה. כשהמרחק קטן מסף, זו פגיעה. ואם מישהו אחר כבר הרג את המטרה? מוחקים את הקליע, אחרת הוא ירדוף אחרי רוח:A straight bullet may miss a moving enemy. A homing bullet recomputes the direction to its target every frame and moves toward it, exactly like an enemy walking to a waypoint, except the point moves. When the distance drops below a threshold, it's a hit. And if someone else already killed the target? Delete the bullet, or it chases a ghost:
אנלוגיהAnalogy
קליע מונחה הוא כמו כלב שרודף אחרי כדור מתגלגל: הוא לא מחשב מראש, רק מסתכל שוב ושוב איפה הכדור עכשיו.A homing bullet is like a dog chasing a rolling ball: no planning ahead, just looking again and again at where the ball is now.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
טעות נפוצהCommon mistake
homing.py
enemies = ["slime"]
hits = ["slime", "slime"]
for target in hits:
enemies.remove(target)
מה Python עונהWhat Python says
ValueError: list.remove(x): x not in list
ValueError: list.remove(x): x not in list: שני קליעים פגעו באותו אויב באותו פריים. בודקים if target in enemies: לפני remove.ValueError: list.remove(x): x not in list: two bullets hit the same enemy in one frame. Check if target in enemies: before remove.
מוחקים מהרשימה אויב שכבר נמחק.Removing an enemy that's already been removed.
נושאTopic 5
גליםWaves
גלים וכלכלהWaves and economy
גל = כמה אויבים, מרווח, עוצמהA wave = how many, the gap, the strength
כל גל קשה יותרEach wave is harder
זהב על הריגה, מגדל עולה זהבGold per kill, a tower costs gold
משחק Tower Defense הוא איזון: גלים שמתחזקים, מול זהב שמרוויחים על כל הריגה וקונים בו מגדלים. נוח לתאר גלים כנתונים (רשימת מילונים) ולא כקוד, כך שקל לכוונן. הפונקציה הזאת מייצרת לוח זמנים של הופעות ומחשבת כמה זהב אפשר להרוויח בכל גל:Tower Defense is a balance: waves that grow stronger, against gold you earn per kill and spend on towers. It's convenient to describe waves as data (a list of dicts) rather than code, so they're easy to tune. This function builds a spawn schedule and works out how much gold each wave can earn:
אנלוגיהAnalogy
גלים הם כמו שיעורי כושר: מתחילים קל, מוסיפים משקל בהדרגה, ונותנים מנוחה קצרה בין סטים.Waves are like a workout: start light, add weight gradually, and rest briefly between sets.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 6
משחקGame
Tower Defense: המשחקTower Defense: the game
אויבים במסלול, מגדלים בצדדיםEnemies on the path, towers beside it
ירי מונחה, פסי חייםHoming shots, health bars
חיים, זהב והריגותLives, gold and kills
המשחק כולו: גלים של אויבים שהולכים במסלול, ארבעה מגדלים שבוחרים את הראשון בטווח ויורים קליעים מונחים עם זמן טעינה, פס חיים מעל כל אויב, וחיים שיורדים כשמישהו מגיע לסוף. במחשב שלכם הוסיפו בניית מגדל בלחיצת עכבר אם יש זהב:The whole game: waves of enemies walking the path, four towers picking the first in range and firing homing bullets with a cooldown, a health bar above each enemy, and lives dropping when someone reaches the end. On your computer, add building a tower with a mouse click if there's enough gold:
אנלוגיהAnalogy
Tower Defense הוא כמו לתכנן מחסומים בכביש: אתם לא שולטים במכוניות, רק בוחרים איפה להציב את השוטרים.Tower Defense is like planning checkpoints on a road: you don't control the cars, you only choose where to station the officers.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.