ברוכים הבאים ל-Pygame: בועות קופצותWelcome to Pygame: Bubble Bounce
מפגש 37: התקנת pygame-ce, לולאת המשחק, ציור, תנועה, כבידה ואירועי עכבר.Session 37: Installing pygame-ce, the game loop, drawing, movement, gravity and mouse events.
התקנת pygame-ce, לולאת המשחק, ציור, תנועה, כבידה ואירועי עכבר. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Installing pygame-ce, the game loop, drawing, movement, gravity and mouse events. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
חלוןWindow
לולאהLoop
צורותShapes
תנועהMotion
הקפצהBounce
משחקGame
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
צבע = (אדום, ירוק, כחול), 0 עד 255Colour = (red, green, blue), 0 to 255
ברוכים הבאים לחלק המשחקים! pygame פותח חלון, ומאפשר לצייר בו ולהגיב למקלדת ולעכבר. מתקינים את pygame-ce, הגרסה המתוחזקת (מייבאים אותה בשם pygame). צבעים כותבים כשלשה של אדום, ירוק וכחול, כל אחד מ-0 עד 255. באתר הלולאה רצה מספר קבוע של פריימים ואז נעצרת, והתמונה מתחת לקוד היא הפריים האחרון. על המחשב שלכם נפתח חלון אמיתי שרץ עד שסוגרים אותו.Welcome to the games part! pygame opens a window and lets you draw in it and react to the keyboard and mouse. Install pygame-ce, the maintained version (you import it as pygame). Colours are a triple of red, green and blue, each 0 to 255. On this site the loop runs a fixed number of frames and then stops, and the picture under the code is the last frame. On your computer a real window opens and runs until you close it.
אנלוגיהAnalogy
החלון הוא כמו לוח ציור, ו-flip() הוא הרגע שבו מסובבים את הלוח לקהל: כל מה שציירתם מופיע בבת אחת.The window is like an easel, and flip() is the moment you turn the canvas to the audience: everything you drew appears at once.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 2
לולאהLoop
לולאת המשחקThe game loop
אירועים → עדכון → ציורEvents → update → draw
QUIT סוגר את החלוןQUIT closes the window
clock.tick(60) = 60 פריימים בשנייהclock.tick(60) = 60 frames per second
כל משחק בנוי על לולאת משחק שרצה שוב ושוב: (1) קוראים אירועים (האם המשתמש סגר את החלון? לחץ?), (2) מעדכנים מיקומים וניקוד, (3) מוחקים את המסך ומציירים מחדש. כל סיבוב הוא פריים, ו-clock.tick(60) דואג שלא יהיו יותר מ-60 בשנייה. במחשב שלכם זה while running:; כאן נריץ 90 פריימים:Every game is built on a game loop running again and again: (1) read events (did the user close the window? click?), (2) update positions and score, (3) clear the screen and redraw. Each pass is a frame, and clock.tick(60) keeps it to at most 60 a second. On your computer it's while running:; here we run 90 frames:
אנלוגיהAnalogy
לולאת משחק היא כמו ספר דפדוף: כל דף הוא פריים, וכשמדפדפים מהר העין רואה תנועה.A game loop is like a flip book: each page is a frame, and flipping fast makes the eye see motion.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 3
צורותShapes
קואורדינטות וצורותCoordinates and shapes
(0, 0) בפינה השמאלית העליונה(0, 0) is the top-left corner
בקואורדינטות של מסך הראשית בפינה השמאלית העליונה, ו-y גדל כלפי מטה, הפוך מהמתמטיקה בבית הספר. pygame.Rect(x, y, w, h) הוא מלבן שיודע את הקצוות שלו (left, right, top, bottom, center), ונשתמש בו הרבה.In screen coordinates the origin is the top-left corner, and y grows down, the opposite of school maths. pygame.Rect(x, y, w, h) is a rectangle that knows its edges (left, right, top, bottom, center), and we'll use it a lot.
אנלוגיהAnalogy
מסך מחשב הוא כמו דף במחברת: מתחילים לכתוב בפינה העליונה, ושורה חדשה היא למטה, לא למעלה.A computer screen is like a notebook page: you start writing at the top corner, and a new line is lower down, not higher up.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
מהירות שלילית = שמאלה/למעלהNegative velocity = left/up
תנועה היא חיבור של מהירות למיקום בכל פריים: x += vx, y += vy. מהירות של 3 ב-60 פריימים בשנייה היא 180 פיקסלים בשנייה. בדוגמה מציירים את המסלול של בועה כדי לראות את התנועה בתמונה אחת:Motion is adding velocity to position every frame: x += vx, y += vy. A velocity of 3 at 60 frames a second is 180 pixels a second. In the example we draw a bubble's path so you can see the motion in one picture:
אנלוגיהAnalogy
מהירות היא כמו הוראה לצב: "כל צעד, 5 משבצות ימינה ו-3 למעלה". אחרי 10 צעדים יודעים בדיוק איפה הוא.Velocity is like an instruction to a robot turtle: "each step, 5 squares right and 3 up". After 10 steps you know exactly where it is.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 5
הקפצהBounce
להקפיץ מהקירותBouncing off the walls
פגיעה בקיר = הופכים את סימן המהירותHitting a wall = flip the velocity's sign
בודקים את הקצה, לא את המרכז: x + rCheck the edge, not the centre: x + r
מחזירים פנימה כדי לא להיתקעPush back inside so it doesn't get stuck
כשבועה נוגעת בקיר, הופכים את המהירות באותו ציר: vx = -vx. בודקים את הקצה של העיגול (x + r מול רוחב החלון), לא את המרכז. טריק חשוב: מחזירים את הבועה פנימה לגבול, אחרת היא עלולה להיתקע בתוך הקיר ולרטוט. הנה הפונקציה, שאפשר לבדוק בלי חלון בכלל:When a bubble touches a wall, flip the velocity on that axis: vx = -vx. Check the circle's edge (x + r against the window width), not its centre. An important trick: push the bubble back to the boundary, otherwise it can get stuck in the wall and jitter. Here's the function, testable without any window:
אנלוגיהAnalogy
להקפיץ זה כמו כדור שולחן: הוא פוגע בדופן, והכיוון מתהפך, אבל המהירות נשארת אותה מהירות.Bouncing is like a pool ball: it hits the cushion and the direction flips, but the speed stays the same.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.
נושאTopic 6
משחקGame
Bubble Bounce: המשחקBubble Bounce: the game
מחלקת Bubble עם update ו-drawA Bubble class with update and draw
רשימה של בועותA list of bubbles
MOUSEBUTTONDOWN → פוצציםMOUSEBUTTONDOWN → pop
מחברים הכול למשחק: מחלקה Bubble (מפגש 15) שיודעת לזוז, לצייר את עצמה ולבדוק אם נקודה בתוכה (מרחק מהמרכז קטן מהרדיוס). רשימה של 12 בועות, ובכל לחיצת עכבר מוחקים את הבועה שנלחצה ומוסיפים נקודה. כאן מדמים שלוש לחיצות:Put it all together into a game: a Bubble class (session 15) that can move, draw itself and check whether a point is inside it (distance from the centre less than the radius). A list of 12 bubbles, and every mouse click removes the clicked bubble and adds a point. Here we simulate three clicks:
אנלוגיהAnalogy
כל בועה היא כמו שחקן שמכיר את התפקיד שלו: המשחק רק אומר לכולם "זוזו" ו"ציירו את עצמכם", וכל אחד יודע איך.Each bubble is like an actor who knows its part: the game just tells everyone "move" and "draw yourselves", and each knows how.
שואלים אם מישהו מכיר דוגמה נוספת מהחיים.Ask for another real-life example.