נבנה על ידיBuilt by Ariel Yohimovich

PythonPython
41
חלקPart 7: משחקים עם PygameGames with Pygame כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

Brick Breaker: שובר לבניםBrick Breaker

מחלקות במשחק, שלבים מנתוני טקסט, power-ups ומטמון תמונות.

Classes in a game, levels from text data, power-ups and an image cache.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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

1. מה מייצג . במפת השלב?What does . mean in the level map?

2. איך יודעים שהכדור פגע מהצד?How do you know the ball hit from the side?

3. מה זה מנהור?What is tunneling?

תרגיליםExercises

תרגילExercise 1

ספירת לבניםCounting bricks

כתבו parse_level(rows, cell_w, cell_h, top) שמחזירה רשימת (x, y, ch) לכל תו שאינו ., כש-x = עמודה × cell_w ו-y = top + שורה × cell_h, ו-count_by_type(rows) שמחזירה מילון תו → כמות.

Write parse_level(rows, cell_w, cell_h, top) returning (x, y, ch) for every non-. character, where x = column × cell_w and y = top + row × cell_h, and count_by_type(rows) returning a dict character → count.

איך הפלט אמור להיראות?What should the output look like?
[(0, 30, 'R'), (40, 30, 'R'), (120, 30, 'R'), (40, 45, 'O'), (80, 45, 'O')]
{'R': 3, 'O': 2}
main.py
רמזHint 1: for r, row in enumerate(rows): for c, ch in enumerate(row):for r, row in enumerate(rows): for c, ch in enumerate(row):
רמזHint 2: Counter("".join(rows).replace(".", ""))Counter("".join(rows).replace(".", ""))
לפתרוןSolution
תרגילExercise 2

פגיעה לפי צדA hit by side

בלי pygame: כתבו move(ball, vx, vy, bricks) כשהכדור והלבנים הם רשימות [x, y, w, h]. מזיזים ב-x, אם יש חפיפה עם לבנה מוחקים אותה והופכים vx (ומחזירים את x אחורה); ואז אותו דבר ב-y. מחזירה (vx, vy, removed) כש-removed הוא מספר הלבנים שנמחקו.

Without pygame: write move(ball, vx, vy, bricks) where the ball and bricks are [x, y, w, h] lists. Move along x; if it overlaps a brick, remove it and flip vx (and move x back); then the same on y. Return (vx, vy, removed) where removed is the number of bricks removed.

איך הפלט אמור להיראות?What should the output look like?
(0, 4, 1) []
main.py
רמזHint 1: פונקציית עזר first_hit(ball, bricks) שמחזירה אינדקס או -1.A helper first_hit(ball, bricks) returning an index or -1.
רמזHint 2: אחרי היפוך: ball[0] += vx מחזיר את הכדור החוצה.After flipping: ball[0] += vx moves the ball back out.
לפתרוןSolution
תרגילExercise 3

זמני אפקטEffect timers

כתבו מחלקה Effects() עם add(name, frames) שמפעילה אפקט (ואם כבר פעיל, מאריכה ל-max בין הנותר לחדש), tick() שמורידה 1 מכל אפקט ומוחקת את מה שהגיע ל-0, ו-active(name).

Write an Effects() class with add(name, frames) turning an effect on (and if already active, extending it to the max of what's left and the new value), tick() reducing every effect by 1 and removing those at 0, and active(name).

איך הפלט אמור להיראות?What should the output look like?
True False {'wide': 2}
False
main.py
רמזHint 1: מילון name → frames.A dict name → frames.
רמזHint 2: ב-tick: {k: v - 1 for k, v in ... if v > 1}.In tick: {k: v - 1 for k, v in ... if v > 1}.
לפתרוןSolution
תרגילExercise 4

חיזוק אקראיA random drop

כתבו drops(n_bricks, chance, seed) שמחזירה רשימת אינדקסים של לבנים (מתוך n_bricks שנשברות לפי הסדר) שהפילו חיזוק, כשכל אחת מפילה בהסתברות chance לפי random.Random(seed).random() < chance. בנוסף, אסור שני חיזוקים ברצף: אם הלבנה הקודמת הפילה, הנוכחית לא.

Write drops(n_bricks, chance, seed) returning the indexes of bricks (of n_bricks broken in order) that dropped a power-up, each dropping with probability chance via random.Random(seed).random() < chance. Also no two drops in a row: if the previous brick dropped, this one doesn't.

איך הפלט אמור להיראות?What should the output look like?
[0, 3, 8, 13, 16, 19] [0, 2, 4, 6, 8] []
main.py
רמזHint 1: rng = random.Random(seed) פעם אחת.rng = random.Random(seed) once.
רמזHint 2: קוראים ל-rng.random() לכל לבנה גם אם מדלגים, כדי שהאקראיות לא תזוז.Call rng.random() for every brick even when skipping, so the randomness doesn't shift.
לפתרוןSolution
תרגילExercise 5

Brick Breaker: מנועBrick Breaker: the engine

פרויקט מלווהCompanion project

כתבו מחלקה Breaker(level, w=480, h=320) שבונה לבנים מהמפה ([x, y, 44, 14], x = 8 + c × 46, y = 30 + r × 18), מחבט [w // 2 - 40, h - 22, 80, 10], כדור [w // 2 - 5, h - 60, 10, 10] עם vx=3, vy=-4, score, lives=3, state. step(paddle_x): ממקם את המחבט (מוגבל), מזיז את הכדור בהפרדת צירים מול הלבנים (10 נקודות ללבנה), קירות ותקרה, החזרה מהמחבט עם זווית, נפילה = חיים והגשה, אין לבנים = "won".

Write a Breaker(level, w=480, h=320) class building bricks from the map ([x, y, 44, 14], x = 8 + c × 46, y = 30 + r × 18), a paddle [w // 2 - 40, h - 22, 80, 10], a ball [w // 2 - 5, h - 60, 10, 10] with vx=3, vy=-4, score, lives=3, state. step(paddle_x): places the paddle (clamped), moves the ball with axis separation against bricks (10 points a brick), walls and ceiling, a paddle return with an angle, a fall = a life and a re-serve, no bricks = "won".

איך הפלט אמור להיראות?What should the output look like?
playing 0 3 7
brick_breaker.py
רמזHint 1: השתמשו ב-move מתרגיל 2 עבור הלבנים.Use move from exercise 2 for the bricks.
רמזHint 2: זווית: vx = (ball_cx - paddle_cx) / 40 * 5.Angle: vx = (ball_cx - paddle_cx) / 40 * 5.
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

לבנים חזקותStrong bricks

הרחיבו את מפת השלבים: ספרה 1–3 היא לבנה שצריך לפגוע בה כך וכך פעמים, ו-# היא לבנה בלתי שבירה. כתבו build(level) שמחזירה רשימת מילונים {x, y, hp} (hp=None לבלתי שבירה, x = c × 46, y = r × 18), ו-hit(bricks, i) שמורידה hp, מוחקת לבנה שהגיעה ל-0, ומחזירה ניקוד: 10 לשבירה, 2 לפגיעה בלי שבירה, 0 לבלתי שבירה.

Extend the level maps: a digit 1–3 is a brick needing that many hits, and # is an unbreakable brick. Write build(level) returning dicts {x, y, hp} (hp=None for unbreakable, x = c × 46, y = r × 18), and hit(bricks, i) lowering hp, removing a brick at 0, and returning points: 10 for breaking, 2 for a hit without breaking, 0 for unbreakable.

איך הפלט אמור להיראות?What should the output look like?
[2, 2, 10] 3 [None, 1, 2]
main.py
רמזHint 1: hp = None if ch == "#" else int(ch)hp = None if ch == "#" else int(ch)
רמזHint 2: ב-hit: אם hp הוא None מחזירים 0 מיד.In hit: if hp is None return 0 at once.
לפתרוןSolution
אתגרChallenge 2

עורך שלביםA level editor

כתבו validate(level) שבודקת מפה ומחזירה רשימת בעיות: "empty" אם אין לבנים שבירות, "ragged" אם השורות באורך שונה, "bad char X" לכל תו לא חוקי (מותר .#123RGBOY, פעם אחת לכל תו, ממוין), ו-"too wide" אם יש יותר מ-10 עמודות. ואז mirror(level) שמחזירה מפה סימטרית: חצי שמאלי של כל שורה + ההיפוך שלו.

Write validate(level) checking a map and returning a list of problems: "empty" if there are no breakable bricks, "ragged" if rows differ in length, "bad char X" for each invalid character (allowed .#123RGBOY, once per character, sorted), and "too wide" with more than 10 columns. Then mirror(level) returning a symmetric map: each row's left half + its reverse.

איך הפלט אמור להיראות?What should the output look like?
['ragged'] ['empty'] ['bad char x', 'bad char z']
['RGGR', '1##1', 'OOOO']
main.py
רמזHint 1: תווים לא חוקיים: sorted(set("".join(level)) - set(ALLOWED)).Invalid characters: sorted(set("".join(level)) - set(ALLOWED)).
רמזHint 2: half = row[: (len(row) + 1) // 2] ואז half + half[::-1] (לרוחב זוגי).half = row[: (len(row) + 1) // 2] then half + half[::-1] (for even width).
לפתרוןSolution

נבנה על ידיBuilt by Ariel Yohimovich