Crystal Quest, חלק 2 ופרסוםCrystal Quest, part 2 & publishing
מפגש 48: חפצים, אויבים עם AI, קרבות, בוס, שמירה ב-JSON ופרסום לדפדפן עם pygbag.Session 48: Items, enemies with AI, combat, a boss, saving to JSON and publishing to the browser with pygbag.
חפצים, אויבים עם AI, קרבות, בוס, שמירה ב-JSON ופרסום לדפדפן עם pygbag. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Items, enemies with AI, combat, a boss, saving to JSON and publishing to the browser with pygbag. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
בינהAI
שמירהSave
חלקיקיםParticles
נכסיםAssets
פרסוםPublish
משחקGame
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
מעבר לפי מרחק לשחקןSwitch by distance to the player
טווח יציאה גדול מטווח כניסהExit range larger than entry range
רוח ב-Crystal Quest לא רודפת תמיד: היא מסיירת ליד הבית, עוברת למצב מרדף כשהשחקן מתקרב, ומוותרת וחוזרת הביתה כשהוא מתרחק. זו מכונת מצבים כמו בסצנות, רק בקטן. טריק חשוב: טווח היציאה מהמרדף (90) גדול מטווח הכניסה (60), אחרת רוח בדיוק על הגבול הייתה מתחלפת בכל פריים:A Crystal Quest ghost doesn't always chase: it patrols near home, switches to a chase state when the player gets close, and gives up and returns home when they move away. It's a state machine like the scenes, just small. An important trick: the exit range (90) is bigger than the entry range (60), otherwise a ghost right on the edge would flip every frame:
אנלוגיהAnalogy
זה כמו כלב שמירה: מסתובב בחצר, נובח ורץ כשמישהו מתקרב לגדר, וכשהאורח הלך רחוק הוא חוזר למלונה.It's like a guard dog: wanders the yard, barks and runs when someone nears the fence, and when the visitor is far away it goes back to its kennel.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 2
שמירהSave
שמירה וטעינהSaving and loading
מצב המשחק → מילון → JSONGame state → dict → JSON
כותבים לקובץ זמני ואז מחליפיםWrite to a temp file, then replace
קובץ חסר או פגום = משחק חדשMissing or broken file = a new game
קובץ שמירה הוא JSON רגיל, כמו בשיעור הקבצים. שני כללים הופכים אותו לבטוח: כותבים קודם לקובץ זמני ורק אז מחליפים (Path.replace), כך שקריסה באמצע לא משאירה קובץ חצי כתוב; וטעינה תמיד מוכנה לקובץ חסר או פגום, ואז מתחילה משחק חדש במקום לקרוס:A save file is plain JSON, as in the files lesson. Two rules make it safe: write to a temp file first and only then replace (Path.replace), so a crash midway never leaves a half-written file; and loading is always ready for a missing or broken file, starting a new game instead of crashing:
אנלוגיהAnalogy
שמירה דרך קובץ זמני היא כמו להעתיק מחברת לדף חדש ורק בסוף לזרוק את הישן: אם משהו קורה באמצע, הישן עדיין שלם.Saving via a temp file is like copying a notebook onto a fresh page and only then throwing out the old one: if something happens midway, the old one is still whole.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
טעות נפוצהCommon mistake
save.py
import json
state = {"bag": {"crystal", "key"}}
json.dumps(state)
מה Python עונהWhat Python says
TypeError: Object of type set is not JSON serializable
TypeError: Object of type set is not JSON serializable: JSON מכיר רשימות ומילונים, לא set או Vector2. ממירים: sorted(bag) או [v.x, v.y].TypeError: Object of type set is not JSON serializable: JSON knows lists and dicts, not set or Vector2. Convert: sorted(bag) or [v.x, v.y].
שומרים אובייקט שלא ניתן להמרה ל-JSON.Saving an object JSON can't convert.
נושאTopic 3
חלקיקיםParticles
חלקיקיםParticles
חלקיק = מיקום, מהירות, חייםA particle = position, velocity, life
חיכוך: vel *= 0.95Friction: vel *= 0.95
מוחקים כשהחיים נגמריםDelete when life runs out
הרגע שבו אוספים קריסטל צריך להרגיש טוב, וחלקיקים הם הדרך הזולה ביותר: כמה עשרות נקודות שעפות לכיוונים אקראיים, מאטות בחיכוך ונעלמות. כל חלקיק הוא רשימה קטנה [pos, vel, life]. הנה פיצוץ אחד, מצויר ברגע אחד באמצע חייו:The moment you collect a crystal should feel good, and particles are the cheapest way: a few dozen points flying in random directions, slowing with friction and vanishing. Each particle is a small list [pos, vel, life]. Here's one burst, drawn at a moment halfway through its life:
אנלוגיהAnalogy
חלקיקים הם כמו זיקוקים: כל ניצוץ פשוט לגמרי, נע ודועך, אבל מאות ביחד יוצרים רגע של חגיגה.Particles are like fireworks: each spark is utterly simple, moving and fading, but hundreds together make a moment of celebration.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 4
נכסיםAssets
קבצי משחק ונתיביםGame files and paths
נתיב יחסי לקובץ הקוד: Path(__file__).parentPaths relative to the code file: Path(__file__).parent
תיקיית assets מסודרתA tidy assets folder
קובץ חסר? תחליף זמני ולא קריסהMissing file? A placeholder, not a crash
משחק אמיתי טוען תמונות וצלילים מתיקיית assets. המלכודת: open("assets/hero.png") מחפש יחסית לתיקייה שממנה הורץ הפקודה, לא לתיקייה של הקוד, ולכן עובד אצלכם ונשבר אצל חברים. בונים נתיבים מ-Path(__file__).parent, ומכינים תחליף צבעוני לכל קובץ שחסר, כדי שהמשחק ייפתח גם אם שכחתם להעתיק תמונה:A real game loads images and sounds from an assets folder. The trap: open("assets/hero.png") looks relative to the folder the command was run from, not the code's folder, so it works for you and breaks for friends. Build paths from Path(__file__).parent, and prepare a coloured placeholder for any missing file, so the game opens even if you forgot to copy an image:
אנלוגיהAnalogy
נתיב יחסי לתיקיית ההרצה הוא כמו להגיד "המפתח במגירה השנייה" בלי להגיד באיזה בית. __file__ הוא הכתובת המלאה.A path relative to where you ran from is like saying "the key is in the second drawer" without saying which house. __file__ is the full address.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 5
פרסוםPublish
פרסום המשחקPublishing the game
pygbag: לולאה async עם await asyncio.sleep(0)pygbag: an async loop with await asyncio.sleep(0)
pygbag . → אתר, למשל ל-itch.iopygbag . → a website, for example for itch.io
כדי שחברים ישחקו, יש שתי דרכים. בדפדפן: pygbag הופך את המשחק לאתר. הוא דורש שינוי קטן: הלולאה הראשית בתוך async def main(), עם await asyncio.sleep(0) בכל פריים כדי לתת לדפדפן לנשום. הקוד הזה רץ גם במחשב רגיל, ולכן אין שתי גרסאות:For friends to play, there are two ways. In the browser: pygbag turns the game into a website. It needs one small change: the main loop goes inside async def main(), with await asyncio.sleep(0) every frame to let the browser breathe. This code also runs on a normal computer, so there are no two versions:
אנלוגיהAnalogy
פרסום הוא כמו להעביר מתכון לחבר: אצלכם הכול במקום, אבל צריך לכתוב בדיוק איזה מצרכים לקנות ואיפה הם, אחרת אצלו זה לא יעבוד.Publishing is like handing a recipe to a friend: in your kitchen everything's in place, but you must write exactly which ingredients to buy and where they are, or it won't work for them.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 6
משחקGame
Crystal Quest: הגרסה המלאהCrystal Quest: the full version
רוחות עם מצביםGhosts with states
קריסטלים עם פיצוץ חלקיקיםCrystals with a particle burst
חיים, הבהוב פגיעה ושורת מצבHealth, a hurt flash and a status line
החלק השני של Crystal Quest מחבר הכול: רוחות שמסיירות, רודפות וחוזרות (הצבע מראה את המצב), קריסטלים שמתפוצצים לחלקיקים כשאוספים אותם, ופגיעה שמורידה חיים ונותנת שנייה של חסינות עם הבהוב. כאן השחקן הולך במסלול; במחשב שלכם הוסיפו את המלאי, החדרים והשמירה מהשיעור הקודם ומהיום, ופרסמו:Crystal Quest part two connects it all: ghosts that patrol, chase and return (the colour shows the state), crystals that burst into particles when collected, and a hit that costs health and gives a second of invulnerability with a flash. Here the player walks a route; on your computer, add the inventory, rooms and saving from last lesson and today, and publish:
אנלוגיהAnalogy
משחק גמור הוא לא רעיון גדול אחד אלא מאה רעיונות קטנים שעובדים יחד. כל מה שבניתם בחלק הזה הוא אחד מהם.A finished game isn't one big idea but a hundred small ones working together. Everything you built in this part is one of them.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.