מפגש 47: משחק הרפתקאות: עולם מאריחים, גיבור מונפש, מצלמה, דיאלוגים ושערים.Session 47: An adventure game: a tile world, an animated hero, a camera, dialogue and gates.
משחק הרפתקאות: עולם מאריחים, גיבור מונפש, מצלמה, דיאלוגים ושערים. בסוף המפגש מוסיפים צעד לפרויקט המלווה.An adventure game: a tile world, an animated hero, a camera, dialogue and gates. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
מבנהLayout
סצנותScenes
תנועהMovement
חדריםRooms
מלאיInventory
משחקGame
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
נושאTopic 1
מבנהLayout
פרויקט בכמה קבציםA multi-file project
settings.py: קבועיםsettings.py: constants
scenes.py: מסכיםscenes.py: screens
main.py: מחבר הכולmain.py: connects it all
Crystal Quest הוא הפרויקט הגדול של החלק הזה, ומשחק בגודל כזה לא נכנס בקובץ אחד בנוחות. מפצלים למודולים כמו בפרויקט מדף: settings.py לקבועים (גודל אריח, מהירות), scenes.py למסכים, ו-main.py שמייבא ומריץ. כל קובץ קצר ואפשר להבין אותו לבד:Crystal Quest is this part's big project, and a game that size doesn't fit comfortably in one file. Split it into modules as in the Shelf project: settings.py for constants (tile size, speed), scenes.py for screens, and main.py that imports and runs. Each file is short and understandable on its own:
אנלוגיהAnalogy
פיצול לקבצים הוא כמו ארגזים במעבר דירה עם תוויות: "מטבח", "ספרים". הכול עדיין שלכם, אבל יודעים איפה לחפש.Splitting into files is like labelled moving boxes: "kitchen", "books". It's all still yours, but you know where to look.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 2
סצנותScenes
סצנותScenes
סצנה = מחלקה עם update ו-drawA scene = a class with update and draw
game.change(new_scene)game.change(new_scene)
הלולאה הראשית לא משתנהThe main loop never changes
עד עכשיו כל משחק השתמש ב-state = "play" ושרשרת if. בפרויקט גדול זה נהיה סבוך, אז כל מסך הופך לסצנה: מחלקה עם update ו-draw. הלולאה הראשית רק קוראת ל-game.scene.update() ו-draw(), וסצנה שרוצה לעבור קוראת ל-game.change(...). כך מוסיפים מסך חדש בלי לגעת בשום if קיים:Until now every game used state = "play" and an if chain. In a big project that gets tangled, so each screen becomes a scene: a class with update and draw. The main loop just calls game.scene.update() and draw(), and a scene that wants to switch calls game.change(...). That way you add a new screen without touching any existing if:
אנלוגיהAnalogy
סצנות הן כמו ערוצים בטלוויזיה: השלט (הלולאה) תמיד אותו שלט, ורק הערוץ שמוצג מתחלף.Scenes are like TV channels: the remote (the loop) is always the same, only the channel on screen changes.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
אלכסון מהיר מדי? מנרמליםDiagonal too fast? Normalise
התנגשות בקירות בהפרדת ציריםWall collision with axis separation
במשחק מבט על אין כבידה: החיצים מזיזים בארבעה כיוונים, ושני חיצים יחד בתנועה אלכסונית. מלכודת קלאסית: באלכסון זזים גם 2 ב-x וגם 2 ב-y, כלומר בערך 2.83 פיקסלים, מהר יותר מבקו ישר. הפתרון הוא לנרמל את וקטור הכיוון. pygame.Vector2 עושה את זה בשבילנו:In a top-down game there's no gravity: arrows move in four directions, and two arrows together give diagonal movement. A classic trap: diagonally you move 2 on x and 2 on y, about 2.83 pixels, faster than in a straight line. The fix is to normalise the direction vector. pygame.Vector2 does it for us:
אנלוגיהAnalogy
זה כמו לחצות מגרש באלכסון: אם כל צעד הוא "צעד ימינה ועוד צעד קדימה", אתם רצים מהר יותר מכולם בלי להתאמץ.It's like crossing a field diagonally: if every step is "a step right plus a step forward", you outrun everyone without trying.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
טעות נפוצהCommon mistake
topdown.py
import pygame
d = pygame.Vector2(0, 0)
d = d.normalize()
מה Python עונהWhat Python says
ValueError: Can't normalize Vector of length zero
ValueError: Can't normalize Vector of length zero: בודקים if d.length(): לפני normalize().ValueError: Can't normalize Vector of length zero: check if d.length(): before normalize().
מנרמלים וקטור אפס כשלא לוחצים על כלום.Normalising a zero vector when nothing is pressed.
נושאTopic 4
חדריםRooms
חדרים ודלתותRooms and doors
כל חדר = מפת אריחים במילוןEach room = a tilemap in a dict
דלת מובילה לחדר אחר ולנקודת כניסהA door leads to another room and an entry point
החלפת חדר = טעינה מחדשChanging rooms = reloading
העולם של Crystal Quest בנוי מחדרים. כל חדר הוא מפת אריחים (כמו בפלטפורמר) שמור במילון לפי שם, ודלת היא רשומה שאומרת לאן היא מובילה ואיפה מופיעים בצד השני. כשנוגעים בדלת, טוענים את החדר החדש ומציבים את השחקן. כאן בודקים שכל דלת מובילה לחדר קיים, וגם שאפשר לחזור:Crystal Quest's world is made of rooms. Each room is a tilemap (as in the platformer) stored in a dict by name, and a door is a record saying where it leads and where you appear on the other side. On touching a door, load the new room and place the player. Here we check that every door leads to an existing room, and that you can come back:
אנלוגיהAnalogy
חדרים ודלתות הם כמו תחנות רכבת: כל תחנה עומדת בפני עצמה, ומסילה (דלת) אומרת לאן ממשיכים ולאיזה רציף מגיעים.Rooms and doors are like train stations: each stands alone, and a track (door) says where you go and which platform you arrive at.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
KeyError: 'caev': שגיאת כתיב בשם החדר. כדאי לבדוק את כל הדלתות פעם אחת בעליית המשחק, כמו בדוגמה למעלה.KeyError: 'caev': a typo in the room name. Check every door once when the game starts, as in the example above.
דלת שמובילה לחדר שלא קיים.A door leading to a room that doesn't exist.
דיאלוג כנתונים, עם תנאיםDialogue as data, with conditions
המלאי הוא מילון: {"crystal": 2, "key": 1}. איסוף מגדיל ספירה, ודלת נעולה בודקת שיש מפתח ומורידה אותו. דיאלוג נשמר כנתונים: לכל דמות רשימת שורות, וכל שורה יכולה לדרוש תנאי ("יש לך 3 קריסטלים?"). הדמות אומרת את השורה הראשונה שהתנאי שלה מתקיים:The inventory is a dict: {"crystal": 2, "key": 1}. Picking up raises a count, and a locked door checks there's a key and uses it up. Dialogue is stored as data: each character has a list of lines, and each line can require a condition ("do you have 3 crystals?"). The character says the first line whose condition holds:
אנלוגיהAnalogy
דיאלוג כנתונים הוא כמו תסריט עם הערות במה: השחקן לא ממציא, הוא קורא את השורה שמתאימה לרגע.Dialogue as data is like a script with stage directions: the actor doesn't improvise, they read the line that fits the moment.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
טעות נפוצהCommon mistake
inventory.py
bag = {"crystal":1}
if bag["key"] >0:print("open")
מה Python עונהWhat Python says
KeyError: 'key'
KeyError: 'key': אין עדיין מפתח במילון. משתמשים ב-bag.get("key", 0).KeyError: 'key': there's no key in the dict yet. Use bag.get("key", 0).
קוראים ספירה של פריט שעוד לא נאסף.Reading the count of an item not yet collected.
נושאTopic 6
משחקGame
Crystal Quest: חלק 1Crystal Quest: part 1
חדר עם קירות, פריטים ודמותA room with walls, items and a character
תנועה, איסוף ושיחהMovement, pickups and talking
שורת מצב עם מלאי והודעהA status line with inventory and a message
החלק הראשון של Crystal Quest: חדר ראשון מתוך מפת אריחים, שחקן שזז במבט על ונעצר בקירות, קריסטלים ומפתח לאיסוף, חכם זקן שמדבר, ודלת שנפתחת עם מפתח. כאן השחקן הולך במסלול קבוע; במחשב שלכם החיצים. בשיעור הבא: אויבים, שמירה, צלילים ופרסום:Crystal Quest part one: a first room from a tilemap, a player moving top-down and stopping at walls, crystals and a key to collect, an old sage who talks, and a door that opens with a key. Here the player walks a fixed route; on your computer, the arrows. Next lesson: enemies, saving, sound and publishing:
אנלוגיהAnalogy
משחק הרפתקה הוא אוסף של חדרים קטנים ופשוטים. הקסם נוצר מהחיבורים: מפתח כאן פותח דלת שם.An adventure game is a collection of small, simple rooms. The magic comes from the connections: a key here opens a door there.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
טעות נפוצהCommon mistake
crystal_quest.py
items = {(1, 1):"c", (2, 2):"k"}
for cell in items:if cell == (1, 1):del items[cell]
מה Python עונהWhat Python says
RuntimeError: dictionary changed size during iteration
RuntimeError: dictionary changed size during iteration: עוברים על עותק (list(items)) או משתמשים ב-items.pop(cell) מחוץ ללולאה, כמו במשחק.RuntimeError: dictionary changed size during iteration: loop over a copy (list(items)) or use items.pop(cell) outside a loop, as in the game.
מוחקים מפריט ממילון בזמן לולאה עליו.Deleting from a dict while looping over it.
settings.py: קבועיםsettings.py: constants
פרויקט בכמה קבציםA multi-file project
scenes.py: מסכיםscenes.py: screens
פרויקט בכמה קבציםA multi-file project
main.py: מחבר הכולmain.py: connects it all
פרויקט בכמה קבציםA multi-file project
סצנה = מחלקה עם update ו-drawA scene = a class with update and draw