מפגש 38: Rect, מקלדת, התנגשויות, טקסט, מצבי משחק ושיא בקובץ.Session 38: Rect, keyboard, collisions, text, game states and a saved high score.
Rect, מקלדת, התנגשויות, טקסט, מצבי משחק ושיא בקובץ. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Rect, keyboard, collisions, text, game states and a saved high score. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
מקלדתKeyboard
RectRect
הופעהSpawning
התנגשותCollision
מצבState
משחקGame
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
יש שתי דרכים לקרוא מקלדת. אירוע `KEYDOWN` מגיע פעם אחת ברגע הלחיצה, ומתאים לפעולה בודדת כמו קפיצה. pygame.key.get_pressed() מחזיר את מצב כל המקשים עכשיו, ומתאים לתנועה רציפה כל עוד מחזיקים. כאן מדמים לחיצות כדי לראות את ההבדל:There are two ways to read the keyboard. A `KEYDOWN` event arrives once at the moment of pressing, good for a single action like a jump. pygame.key.get_pressed() returns every key's state right now, good for continuous movement while held. Here we simulate presses to see the difference:
אנלוגיהAnalogy
KEYDOWN הוא כמו פעמון בדלת: מצלצל פעם אחת כשלוחצים. get_pressed הוא כמו להסתכל אם מישהו עדיין עומד ליד הדלת.KEYDOWN is like a doorbell: it rings once when pressed. get_pressed is like looking whether someone is still standing at the door.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 2
RectRect
Rect ותנועה בגבולותRect and bounded movement
`Rect` יודע left, right, centerxA `Rect` knows left, right, centerx
השחקן הוא Rect: קל להזיז אותו (basket.x += 6), וקל לבדוק את הקצוות. כדי שלא יברח מהמסך, מגבילים לטווח: max(0, min(W - width, x)). ל-Rect יש גם clamp_ip שעושה את זה בשבילכם:The player is a Rect: easy to move (basket.x += 6), easy to check edges. So it doesn't escape the screen, clamp it: max(0, min(W - width, x)). A Rect also has clamp_ip that does it for you:
אנלוגיהAnalogy
להגביל לטווח זה כמו גדר במגרש משחקים: הילדים רצים חופשי, אבל לא יוצאים לכביש.Clamping is like a playground fence: kids run freely, but never out into the road.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
כוכבים מופיעים למעלה במקום אקראי כל כמה פריימים, ונופלים. כדי שהרשימה לא תגדל לנצח, מוחקים כוכב שיצא מתחתית המסך. random.seed קובע את האקראיות כדי שכל הרצה תהיה זהה (נוח לבדיקות):Stars spawn at a random spot at the top every few frames, and fall. So the list doesn't grow forever, remove a star that leaves the bottom of the screen. random.seed fixes the randomness so every run is identical (handy for testing):
אנלוגיהAnalogy
הופעה היא כמו מכונת כדורי טניס: כל כמה שניות יוצא כדור, והכדורים שנפלו מהמגרש נאספים, כדי שהמגרש לא יתמלא.Spawning is like a tennis ball machine: every few seconds a ball comes out, and balls off the court are collected so the court doesn't fill up.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
התנגשות בין שני מלבנים היא שאלה פשוטה: האם הם חופפים? colliderect עונה. collidepoint בודק נקודה (למשל לחיצת עכבר), ו-collidelist מחזיר את האינדקס של הראשון שמתנגש. בפנים זה רק ארבע השוואות:A collision between two rectangles is a simple question: do they overlap? colliderect answers. collidepoint checks a point (like a mouse click), and collidelist returns the index of the first that collides. Inside, it's just four comparisons:
אנלוגיהAnalogy
התנגשות היא כמו לבדוק אם שני שטיחים על הרצפה חופפים: אם אחד נגמר לפני שהשני מתחיל, בכל כיוון, הם לא נוגעים.Collision is like checking whether two rugs on the floor overlap: if one ends before the other starts, in any direction, they don't touch.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 5
מצבState
ניקוד, חיים וסיוםScore, lives and game over
ניקוד עולה בתפיסהScore goes up on a catch
חיים יורדים בפספוסLives go down on a miss
מצב משחק: "playing" / "over"Game state: "playing" / "over"
משחק צריך מטרה: תפיסה מוסיפה נקודה, פספוס מוריד חיים, ובאפס חיים עוברים למצב "סיום". משתנה state אומר באיזה מצב המשחק, והלולאה מתנהגת לפיו. מסך סיום מציג טקסט במרכז בעזרת get_rect(center=...):A game needs a goal: a catch adds a point, a miss costs a life, and at zero lives switch to a "game over" state. A state variable says which mode the game is in, and the loop behaves accordingly. A game-over screen shows centred text using get_rect(center=...):
אנלוגיהAnalogy
מצב משחק הוא כמו רמזור: ירוק = משחקים, אדום = נגמר. הלולאה מסתכלת ברמזור לפני שהיא מחליטה מה לעשות.Game state is like a traffic light: green = playing, red = over. The loop looks at the light before deciding what to do.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 6
משחקGame
Star Catcher: המשחקStar Catcher: the game
סל שזז עם החיציםA basket moving with the arrows
כוכבים נופלים ונתפסיםStars fall and get caught
ניקוד וחיים בפינהScore and lives in the corner
מחברים הכול: סל שזז עם החיצים (כאן "שחקן אוטומטי" זז לעבר הכוכב הקרוב, כי אין מי שילחץ), כוכבים בצורת יהלום שמופיעים ונופלים, התנגשות, ניקוד וחיים. על המחשב שלכם החיצים שולטים:Put it together: a basket moving with the arrows (here an "auto-player" heads for the nearest star, since nobody's pressing keys), diamond-shaped stars that spawn and fall, collision, score and lives. On your computer the arrow keys take over:
אנלוגיהAnalogy
המשחק כולו הוא שלושה רעיונות שכבר הכרתם: תנועה, הופעה והתנגשות. כמו מתכון שמחבר שלושה מצרכים פשוטים למנה.The whole game is three ideas you already know: movement, spawning and collision. Like a recipe combining three simple ingredients into a dish.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.