מפגש 41: מחלקות במשחק, שלבים מנתוני טקסט, power-ups ומטמון תמונות.Session 41: Classes in a game, levels from text data, power-ups and an image cache.
מחלקות במשחק, שלבים מנתוני טקסט, power-ups ומטמון תמונות. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Classes in a game, levels from text data, power-ups and an image cache. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
שלבLevel
צדדיםSides
מנהורTunneling
חיזוקיםPower-ups
שלביםLevels
משחקGame
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
במקום למקם כל לבנה ביד, מציירים את השלב כמפה של מחרוזות: כל תו הוא לבנה, ונקודה היא ריק. לולאה כפולה עם enumerate הופכת כל תו למלבן במקום הנכון. ככה מוסיפים שלב חדש בלי לגעת בקוד:Instead of placing every brick by hand, draw the level as a map of strings: each character is a brick, and a dot is empty. A double loop with enumerate turns each character into a rectangle in the right place. That way you add a new level without touching the code:
אנלוגיהAnalogy
מפת שלב היא כמו תווים של שיר: סדרה של סימנים פשוטים שמישהו אחר (הקוד) יודע לנגן.A level map is like sheet music: a series of simple symbols someone else (the code) knows how to play.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
כשהכדור פוגע בלבנה, צריך לדעת אם להפוך את vx (פגיעה מהצד) או את vy (מלמעלה או מלמטה). הטריק הפשוט: הפרדת צירים. מזיזים רק ב-x ובודקים התנגשות; אם יש, זו פגיעה מהצד. אחר כך מזיזים ב-y ובודקים שוב; אם יש, זו פגיעה מלמעלה/מלמטה:When the ball hits a brick, you need to know whether to flip vx (a side hit) or vy (from above or below). The simple trick: axis separation. Move only along x and check collisions; if there's one, it was a side hit. Then move along y and check again; if there's one, it was a top/bottom hit:
אנלוגיהAnalogy
הפרדת צירים היא כמו לבדוק דלת בשני שלבים: קודם דוחפים ימינה ורואים אם נתקעים, אחר כך דוחפים קדימה. כך יודעים בדיוק איפה המכשול.Axis separation is like testing a door in two steps: push right and see if you're blocked, then push forward. That's how you know exactly where the obstacle is.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 3
מנהורTunneling
מהירות ומנהורSpeed and tunneling
כדור מהיר יכול "לדלג" מעל לבנהA fast ball can "skip" a brick
צעדים קטנים: מחלקים את התנועהSmall steps: split the motion
מהירות מקסימלית < עובי הלבנהMax speed < brick thickness
אם הכדור זז 20 פיקסלים בפריים ולבנה עבה 14 פיקסלים, הוא יכול להיות לפני הלבנה בפריים אחד ואחריה בפריים הבא, בלי לגעת בה אף פעם: מנהור. פתרון פשוט: מחלקים את התנועה לכמה צעדים קטנים בכל פריים, ובודקים התנגשות אחרי כל אחד:If the ball moves 20 pixels a frame and a brick is 14 pixels thick, it can be in front of the brick on one frame and past it on the next, never touching it: tunneling. A simple fix: split the motion into several small steps each frame, checking for collisions after each:
אנלוגיהAnalogy
מנהור הוא כמו לצלם רץ מהיר פעם בשנייה: בתמונה אחת הוא לפני המכשול ובבאה אחריו, ונראה שקפץ דרכו.Tunneling is like photographing a sprinter once a second: in one shot they're before the hurdle and in the next past it, as if they went through it.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 4
חיזוקיםPower-ups
חיזוקיםPower-ups
לבנה שנשברת מפילה חיזוק לפעמיםA broken brick sometimes drops a power-up
תופסים במחבט → אפקטCatch it with the paddle → an effect
אפקט זמני עם מונה פריימיםA temporary effect with a frame counter
חיזוקים מוסיפים עניין: כשלבנה נשברת, בהסתברות קטנה נופל פריט. אם המחבט תופס אותו, מקבלים אפקט, למשל מחבט רחב, ל-300 פריימים. את הזמן סופרים במונה שיורד בכל פריים, וכשמגיע לאפס מחזירים את הרוחב:Power-ups add interest: when a brick breaks, there's a small chance an item drops. If the paddle catches it, you get an effect, say a wide paddle, for 300 frames. Count the time with a counter that goes down each frame, and when it reaches zero, restore the width:
אנלוגיהAnalogy
חיזוק זמני הוא כמו כרטיס "פי שניים נקודות לשעה" בפארק שעשועים: יש שעון, וכשהוא נגמר הכול חוזר לרגיל.A temporary power-up is like a "double points for an hour" pass at a fun fair: there's a clock, and when it runs out everything goes back to normal.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 5
שלביםLevels
חיים ושלביםLives and levels
כדור מתחת למחבט = חיים פחותBall below the paddle = a life lost
אין לבנים = שלב הבאNo bricks = next level
רשימת שלבים = רשימת מפותA list of levels = a list of maps
המשחק צריך התקדמות: כשהכדור נופל מתחת למחבט מאבדים חיים ומגישים מחדש; כשכל הלבנים נשברו, טוענים את המפה הבאה מרשימת השלבים. אם נגמרו השלבים, ניצחון. כל זה הוא מכונת מצבים קטנה:The game needs progress: when the ball falls below the paddle you lose a life and re-serve; when every brick is broken, load the next map from the list of levels. If the levels run out, victory. It's all a small state machine:
אנלוגיהAnalogy
שלבים הם כמו פרקים בספר: כשמסיימים אחד, מדפדפים לבא, ובסוף הספר מגיע הסוף הטוב.Levels are like chapters in a book: finish one, turn to the next, and at the end comes the happy ending.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 6
משחקGame
Brick Breaker: המשחקBrick Breaker: the game
שלב מטקסטA level from text
הפרדת צירים בפגיעותAxis separation on hits
זווית מהמחבט, ניקודAngle off the paddle, score
הכול יחד: שלב שנבנה מהמפה, כדור שמשתמש בהפרדת צירים מול הלבנים, זווית לפי נקודת הפגיעה במחבט (כמו ב-Pong), וניקוד. כאן המחבט עוקב אחרי הכדור לבד; במחשב שלכם העכבר או החיצים מזיזים אותו:All together: a level built from the map, a ball using axis separation against bricks, an angle by where it hits the paddle (as in Pong), and score. Here the paddle follows the ball by itself; on your computer the mouse or arrows move it:
אנלוגיהAnalogy
Brick Breaker משלב את כל מה שבנינו: Rect, התנגשויות, זווית, מצבים. כמו מתכון שמשתמש בכל הכלים במטבח.Brick Breaker combines everything we've built: Rects, collisions, angles, states. Like a recipe that uses every tool in the kitchen.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.