נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
PythonPython

Crystal Quest, חלק 1Crystal Quest, part 1

מפגש 47: משחק הרפתקאות: עולם מאריחים, גיבור מונפש, מצלמה, דיאלוגים ושערים.Session 47: An adventure game: a tile world, an animated hero, a camera, dialogue and gates.

מה נעשה היוםToday's plan

  • מבנהLayout
  • סצנותScenes
  • תנועהMovement
  • חדריםRooms
  • מלאיInventory
  • משחקGame
נושאTopic 1

מבנהLayout

פרויקט בכמה קבציםA multi-file project

  • settings.py: קבועיםsettings.py: constants
  • scenes.py: מסכיםscenes.py: screens
  • main.py: מחבר הכולmain.py: connects it all

אנלוגיהAnalogy

פיצול לקבצים הוא כמו ארגזים במעבר דירה עם תוויות: "מטבח", "ספרים". הכול עדיין שלכם, אבל יודעים איפה לחפש.Splitting into files is like labelled moving boxes: "kitchen", "books". It's all still yours, but you know where to look.
נושא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

אנלוגיהAnalogy

סצנות הן כמו ערוצים בטלוויזיה: השלט (הלולאה) תמיד אותו שלט, ורק הערוץ שמוצג מתחלף.Scenes are like TV channels: the remote (the loop) is always the same, only the channel on screen changes.
נושאTopic 3

תנועהMovement

תנועה במבט עלTop-down movement

  • מבט על: אין כבידהTop-down: no gravity
  • אלכסון מהיר מדי? מנרמליםDiagonal too fast? Normalise
  • התנגשות בקירות בהפרדת ציריםWall collision with axis separation

אנלוגיהAnalogy

זה כמו לחצות מגרש באלכסון: אם כל צעד הוא "צעד ימינה ועוד צעד קדימה", אתם רצים מהר יותר מכולם בלי להתאמץ.It's like crossing a field diagonally: if every step is "a step right plus a step forward", you outrun everyone without trying.

טעות נפוצה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().
נושא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

אנלוגיה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.

טעות נפוצהCommon mistake

rooms.py
ROOMS = {"hall": [], "cave": []}
DOORS = {("hall", "D"): ("caev", (1, 1))}
target, spawn = DOORS[("hall", "D")]
room = ROOMS[target]
מה Python עונהWhat Python says
KeyError: 'caev'
KeyError: 'caev': שגיאת כתיב בשם החדר. כדאי לבדוק את כל הדלתות פעם אחת בעליית המשחק, כמו בדוגמה למעלה.KeyError: 'caev': a typo in the room name. Check every door once when the game starts, as in the example above.
נושאTopic 5

מלאיInventory

מלאי ודיאלוגInventory and dialogue

  • מלאי = מילון ספירותAn inventory = a dict of counts
  • דלת נעולה צורכת מפתחA locked door uses up a key
  • דיאלוג כנתונים, עם תנאיםDialogue as data, with conditions

אנלוגיהAnalogy

דיאלוג כנתונים הוא כמו תסריט עם הערות במה: השחקן לא ממציא, הוא קורא את השורה שמתאימה לרגע.Dialogue as data is like a script with stage directions: the actor doesn't improvise, they read the line that fits the moment.

טעות נפוצה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).
נושא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

אנלוגיהAnalogy

משחק הרפתקה הוא אוסף של חדרים קטנים ופשוטים. הקסם נוצר מהחיבורים: מפתח כאן פותח דלת שם.An adventure game is a collection of small, simple rooms. The magic comes from the connections: a key here opens a door there.

טעות נפוצה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.

settings.py: קבועיםsettings.py: constants

scenes.py: מסכיםscenes.py: screens

main.py: מחבר הכולmain.py: connects it all

סצנה = מחלקה עם update ו-drawA scene = a class with update and draw

game.change(new_scene)game.change(new_scene)

הלולאה הראשית לא משתנהThe main loop never changes

מבט על: אין כבידהTop-down: no gravity

אלכסון מהיר מדי? מנרמליםDiagonal too fast? Normalise

התנגשות בקירות בהפרדת ציריםWall collision with axis separation

כל חדר = מפת אריחים במילוןEach room = a tilemap in a dict

דלת מובילה לחדר אחר ולנקודת כניסהA door leads to another room and an entry point

החלפת חדר = טעינה מחדשChanging rooms = reloading

מלאי = מילון ספירותAn inventory = a dict of counts

דלת נעולה צורכת מפתחA locked door uses up a key

מה למדנוWhat we learned

  • settings.py: קבועיםsettings.py: constants
  • סצנה = מחלקה עם update ו-drawA scene = a class with update and draw
  • מבט על: אין כבידהTop-down: no gravity
  • כל חדר = מפת אריחים במילוןEach room = a tilemap in a dict
  • מלאי = מילון ספירותAn inventory = a dict of counts
  • חדר עם קירות, פריטים ודמותA room with walls, items and a character

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

נבנה על ידיBuilt by Ariel Yohimovich