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

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

Flappy: ציפור מעופפתFlappy

Sprite sheet, אנימציית פריימים, Parallax ו-dt.

Sprite sheets, frame animation, parallax and dt.

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

2. למה צריך דגל scored לכל צינור?Why does each pipe need a scored flag?

3. בפרלקסה, איזו שכבה זזה הכי לאט?In parallax, which layer moves slowest?

תרגיליםExercises

תרגילExercise 1

קשת קפיצהA jump arc

כתבו simulate(frames, flaps, gravity=0.35, flap=-6.0, start=160.0, max_fall=10.0) שמחזירה רשימת מיקומי y (מעוגלים ל-1) לכל פריים. בפריים שנמצא ב-flaps (set) המהירות נקבעת ל-flap; בכל פריים מוסיפים כבידה (עד max_fall) ואז מזיזים.

Write simulate(frames, flaps, gravity=0.35, flap=-6.0, start=160.0, max_fall=10.0) returning y positions (rounded to 1) for every frame. On a frame in flaps (a set) the velocity is set to flap; every frame add gravity (up to max_fall) and then move.

איך הפלט אמור להיראות?What should the output look like?
[154.3, 149.0, 144.1, 139.5] 111.5 142.8
main.py
רמזHint 1: סדר: דחיפה → כבידה → תנועה.Order: flap → gravity → move.
רמזHint 2: vel = min(max_fall, vel + gravity)vel = min(max_fall, vel + gravity)
לפתרוןSolution
תרגילExercise 2

מלבני צינורPipe rectangles

בלי pygame: כתבו pipe_rects(x, gap_y, gap, height, width=40) שמחזירה שני טאפלים (x, y, w, h): העליון מ-0 עד תחילת הרווח, והתחתון מסוף הרווח עד height. ו-random_gap(rng, gap, height, margin=20) שמחזירה מרכז רווח כך שכל הרווח בתוך המסך עם שוליים.

Without pygame: write pipe_rects(x, gap_y, gap, height, width=40) returning two tuples (x, y, w, h): the top one from 0 to the gap's start, and the bottom one from the gap's end to height. And random_gap(rng, gap, height, margin=20) returning a gap centre so the whole gap is on screen with margins.

איך הפלט אמור להיראות?What should the output look like?
((300, 0, 40, 105), (300, 215, 40, 105))
75 245
main.py
רמזHint 1: top_h = gap_y - gap // 2top_h = gap_y - gap // 2
רמזHint 2: rng.randint(gap // 2 + margin, height - gap // 2 - margin)rng.randint(gap // 2 + margin, height - gap // 2 - margin)
לפתרוןSolution
תרגילExercise 3

ניקוד בלי כפילויותScoring without duplicates

כתבו advance(pipes, speed, bird_x, pipe_w=40) שמזיזה כל צינור (מילון עם x ו-scored) שמאלה ב-speed, מסמנת ומחזירה כמה צינורות חדשים עברו את הציפור (קצה ימני < bird_x), ומוחקת צינורות שיצאו מהמסך (x + pipe_w < 0). הרשימה מתעדכנת במקום.

Write advance(pipes, speed, bird_x, pipe_w=40) moving every pipe (a dict with x and scored) left by speed, marking and returning how many new pipes passed the bird (right edge < bird_x), and removing pipes off screen (x + pipe_w < 0). The list is updated in place.

איך הפלט אמור להיראות?What should the output look like?
1 0 2
[{'x': 20.0, 'scored': True}]
main.py
רמזHint 1: pipes[:] = [p for p in pipes if ...] מעדכן במקום.pipes[:] = [p for p in pipes if ...] updates in place.
רמזHint 2: רק אם not p["scored"].Only if not p["scored"].
לפתרוןSolution
תרגילExercise 4

תיבת פגיעהA hitbox

כתבו hitbox(cx, cy, w, h, shrink=0.2) שמחזירה מלבן (x, y, w, h) שלמים, ממורכז באותה נקודה וקטן בכל כיוון ב-shrink מהגודל (למשל 0.2 = רוחב × 0.8), ו-hits_pipe(box, pipe_rects) שמחזירה True אם יש חפיפה עם אחד המלבנים.

Write hitbox(cx, cy, w, h, shrink=0.2) returning an integer rect (x, y, w, h), centred on the same point and smaller by shrink of the size (e.g. 0.2 = width × 0.8), and hits_pipe(box, pipe_rects) returning True if it overlaps any of the rects.

איך הפלט אמור להיראות?What should the output look like?
(78, 141, 24, 18) False True
main.py
רמזHint 1: nw, nh = round(w * (1 - shrink)), round(h * (1 - shrink))nw, nh = round(w * (1 - shrink)), round(h * (1 - shrink))
רמזHint 2: (round(cx - nw / 2), round(cy - nh / 2), nw, nh)(round(cx - nw / 2), round(cy - nh / 2), nw, nh)
לפתרוןSolution
תרגילExercise 5

Flappy: לוגיקת המשחקFlappy: game logic

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

כתבו מחלקה Flappy(seed=0, w=480, h=320) עם ציפור ב-x=90 (y במרכז, מהירות 0), pipes, score, alive, ו-frame. flap() קובעת מהירות -6. tick(): כל 90 פריימים (מפריים 0) מוסיפה צינור ב-x=w עם רווח אקראי (110, שוליים 20), כבידה 0.35 עד 10, תנועה, קידום צינורות במהירות 2.5 עם ניקוד, ומוות בפגיעה בצינור (תיבת פגיעה 24×18) או ברצפה/תקרה.

Write a Flappy(seed=0, w=480, h=320) class with a bird at x=90 (y in the middle, velocity 0), pipes, score, alive, and frame. flap() sets velocity -6. tick(): every 90 frames (from frame 0) adds a pipe at x=w with a random gap (110, margin 20), gravity 0.35 up to 10, movement, pipe advance at speed 2.5 with scoring, and death on hitting a pipe (a 24×18 hitbox) or the floor/ceiling.

איך הפלט אמור להיראות?What should the output look like?
True 5 600
flappy.py
רמזHint 1: השתמשו בפונקציות מהתרגילים הקודמים.Use the functions from the previous exercises.
רמזHint 2: אחרי מוות tick לא עושה כלום.After death tick does nothing.
לפתרוןSolution

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

אתגרChallenge 1

קושי מתכוונןAdjustable difficulty

כתבו settings(level) שמחזירה מילון gap, speed, spawn_every לפי רמה 1–10 (אינטרפולציה ליניארית בין רמה 1: 140, 2.0, 110 לרמה 10: 90, 4.0, 70, ומעוגל: gap ו-spawn_every למספר שלם, speed ל-2). רמה מחוץ לטווח מוגבלת אליו. ואז reachable(gap, speed, spawn_every, flap=-6, gravity=0.35) שמחזירה True אם המרחק בין צינורות (speed × spawn_every) גדול ממרחק הקשת של קפיצה אחת (המרחק האופקי עד שהציפור חוזרת לגובה המקורי), כך שאפשר להספיק לתקן.

Write settings(level) returning a dict gap, speed, spawn_every for levels 1–10 (linear interpolation from level 1: 140, 2.0, 110 to level 10: 90, 4.0, 70, rounded: gap and spawn_every to integers, speed to 2). Levels outside are clamped. Then reachable(gap, speed, spawn_every, flap=-6, gravity=0.35) returning True if the distance between pipes (speed × spawn_every) exceeds one flap's arc distance (the horizontal distance until the bird returns to its starting height), so there's time to correct.

איך הפלט אמור להיראות?What should the output look like?
0 {'gap': 140, 'speed': 2.0, 'spawn_every': 110} True
1 {'gap': 140, 'speed': 2.0, 'spawn_every': 110} True
5 {'gap': 118, 'speed': 2.89, 'spawn_every': 92} True
10 {'gap': 90, 'speed': 4.0, 'spawn_every': 70} True
99 {'gap': 90, 'speed': 4.0, 'spawn_every': 70} True
main.py
רמזHint 1: t = (level - 1) / 9 ואז a + (b - a) * t.t = (level - 1) / 9 then a + (b - a) * t.
רמזHint 2: משך קשת: סופרים פריימים עד שהגובה חוזר, או בנוסחה 2 * -flap / gravity.Arc duration: count frames until the height returns, or the formula 2 * -flap / gravity.
לפתרוןSolution
אתגרChallenge 2

שיא נשמרA saved best score

כתבו update_best(path, score) ששומרת שיא בקובץ JSON ({"best": N, "games": N}): מגדילה את מספר המשחקים, מעדכנת שיא אם צריך, ומחזירה (best, is_new). קובץ חסר או פגום מתחיל מאפס, והכתיבה דרך קובץ זמני.

Write update_best(path, score) keeping a best score in a JSON file ({"best": N, "games": N}): increments the game count, updates the best if needed, and returns (best, is_new). A missing or broken file starts from zero, and writing goes through a temp file.

איך הפלט אמור להיראות?What should the output look like?
[(3, True), (7, True), (7, False), (7, False)] {'best': 7, 'games': 4}
(2, True)
main.py
רמזHint 1: data = {"best": 0, "games": 0} כברירת מחדל.data = {"best": 0, "games": 0} as the default.
רמזHint 2: Path(path).with_suffix(".tmp") ואז replace.Path(path).with_suffix(".tmp") then replace.
לפתרוןSolution

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