Installing pygame-ce, the game loop, drawing, movement, gravity and mouse events.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-VS Code. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in VS Code. Progress is saved.
חימום: 3 שאלותWarm-up: 3 questions
תרגיליםExercises
תרגילExercise 1
צבעיםColours
כתבו mix(c1, c2) שמחזירה את הצבע שבאמצע בין שני צבעי RGB (ממוצע כל רכיב, מעוגל למטה), ו-darker(c, amount) שמורידה amount מכל רכיב בלי לרדת מתחת ל-0.
Write mix(c1, c2) returning the RGB colour halfway between two colours (each component averaged, rounded down), and darker(c, amount) subtracting amount from each component without going below 0.
איך הפלט אמור להיראות?What should the output look like?
(127, 0, 127) (0, 80, 155)
main.py
רמזHint 1:tuple((a + b) // 2 for a, b in zip(c1, c2))tuple((a + b) // 2 for a, b in zip(c1, c2))
כתבו bounce(pos, vel, r, size) שמזיזה בציר אחד ומחזירה (pos, vel) חדשים: אם הקצה עבר את 0 או את size, הבועה מוחזרת לגבול והמהירות מתהפכת.
Write bounce(pos, vel, r, size) that moves along one axis and returns the new (pos, vel): if the edge passed 0 or size, the bubble is put back on the boundary and the velocity flips.
איך הפלט אמור להיראות?What should the output look like?
(460, -15) (20, 10) (204, 4)
main.py
רמזHint 1:קודם pos += vel.First pos += vel.
רמזHint 2:if pos - r < 0: pos, vel = r, -velif pos - r < 0: pos, vel = r, -vel
כתבו hit(bubble, point) שמחזירה True אם נקודת הלחיצה בתוך העיגול (מרחק מהמרכז ≤ רדיוס), ו-pop_at(bubbles, point) שמוחקת את הבועה העליונה (האחרונה ברשימה, כי היא צוירה אחרונה) שנלחצה ומחזירה True, או False אם לא נלחצה אף אחת. בועה היא מילון עם x, y, r.
Write hit(bubble, point) returning True if the click is inside the circle (distance from the centre ≤ radius), and pop_at(bubbles, point) removing the topmost clicked bubble (the last in the list, since it was drawn last) and returning True, or False if none was clicked. A bubble is a dict with x, y, r.
איך הפלט אמור להיראות?What should the output look like?
True [{'x': 100, 'y': 100, 'r': 30}] False
main.py
רמזHint 1:בלי שורש: dx * dx + dy * dy <= r * r.No square root: dx * dx + dy * dy <= r * r.
רמזHint 2:עוברים מהסוף: for i in range(len(bubbles) - 1, -1, -1).Walk from the end: for i in range(len(bubbles) - 1, -1, -1).
בעזרת pygame: צרו חלון 200×120, מלאו בצבע (18, 24, 48), ציירו עיגול (255, 200, 60) ברדיוס 20 במרכז, והדפיסו את צבע הפיקסל במרכז ובפינה (screen.get_at, רק שלושת הרכיבים הראשונים).
With pygame: create a 200×120 window, fill it with (18, 24, 48), draw a (255, 200, 60) circle of radius 20 in the centre, and print the pixel colour at the centre and at the corner (screen.get_at, first three components only).
איך הפלט אמור להיראות?What should the output look like?
Bubble Bounce: מחלקת בועהBubble Bounce: a bubble class
פרויקט מלווהCompanion project
כתבו מחלקה Bubble(x, y, r, vx, vy) עם update(w, h) (תנועה והקפצה משני הצירים, כולל החזרה לגבול) ו-hit(px, py). ואז step(bubbles, w, h, clicks) שמעדכנת את כל הבועות, מפוצצת לכל לחיצה את הבועה העליונה שנפגעה, ומחזירה כמה פוצצו.
Write a Bubble(x, y, r, vx, vy) class with update(w, h) (move and bounce on both axes, back to the boundary) and hit(px, py). Then step(bubbles, w, h, clicks) updating every bubble, popping the topmost hit bubble for each click, and returning how many popped.
איך הפלט אמור להיראות?What should the output look like?
1 1 (465, 305, -10, -10)
bubble_bounce.py
רמזHint 1:פונקציית עזר לציר אחד חוסכת כפילות.A helper for one axis saves duplication.
רמזHint 2:לכל לחיצה: לולאה מהסוף עם break.For each click: a loop from the end with break.
כתבו physics_step(b, w, h, gravity=0.5, bounce=0.8) לבועה (מילון x, y, r, vx, vy): מוסיפה gravity ל-vy, מזיזה, ובפגיעה ברצפה מחזירה לגבול והופכת את vy כפול bounce (איבוד אנרגיה). בקירות הצד: הקפצה רגילה. ואז frames_until_rest(b, w, h) שמחזירה אחרי כמה פריימים הבועה על הרצפה עם |vy| < 1 (עד 2000).
Write physics_step(b, w, h, gravity=0.5, bounce=0.8) for a bubble (a dict x, y, r, vx, vy): adds gravity to vy, moves, and on hitting the floor puts it back on the boundary and flips vy times bounce (losing energy). Side walls: a normal bounce. Then frames_until_rest(b, w, h) returning after how many frames the bubble sits on the floor with |vy| < 1 (up to 2000).
איך הפלט אמור להיראות?What should the output look like?
230 42 310
main.py
רמזHint 1:b["vy"] += gravity לפני התנועה.b["vy"] += gravity before moving.
רמזHint 2:ברצפה: b["y"] = h - b["r"]; b["vy"] = -b["vy"] * bounce.On the floor: b["y"] = h - b["r"]; b["vy"] = -b["vy"] * bounce.
כתבו collide(a, b) לשתי בועות (מילונים x, y, r, vx, vy, באותה מסה): אם הן חופפות (מרחק < r1 + r2), מחליפים ביניהן את רכיבי המהירות לאורך הקו שמחבר את המרכזים, ומרחיקים אותן כך שרק ייגעו. מחזירה True אם הייתה התנגשות. התנגשות חזיתית של בועות זהות מחליפה את המהירויות.
Write collide(a, b) for two bubbles (dicts x, y, r, vx, vy, equal mass): if they overlap (distance < r1 + r2), swap their velocity components along the line joining the centres, and push them apart so they just touch. Returns True if they collided. A head-on collision of identical bubbles swaps their velocities.
איך הפלט אמור להיראות?What should the output look like?
True (-2.0, 3.0) (95.0, 135.0) False
main.py
רמזHint 1:כיוון: nx, ny = dx / d, dy / d; רכיב המהירות לאורכו: vx * nx + vy * ny.Direction: nx, ny = dx / d, dy / d; the velocity component along it: vx * nx + vy * ny.
רמזHint 2:החלפה: מוסיפים לכל אחד (p_other - p_self) * n.Swap: add (p_other - p_self) * n to each.