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

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

Pixel PlatformerPixel Platformer

Tilemap, מצלמה, Coyote Time, Jump Buffer ומצבי אנימציה.

Tilemaps, a camera, coyote time, jump buffering and animation states.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. מתי מותר לשחקן לקפוץ?When may the player jump?

2. מה המצלמה משנה?What does the camera change?

3. איך מבחינים בין נחיתה על אויב לבין פגיעה ממנו?How do you tell landing on an enemy from being hit by it?

תרגיליםExercises

תרגילExercise 1

קריאת מפהReading a map

כתבו load(level, tile) שמחזירה מילון עם רשימות: "solid" (מלבנים (x, y, tile, tile) לכל #), "coins" (מרכזי משבצות c), ו-"start" (פינה של המשבצת P, או (0, 0) אם אין).

Write load(level, tile) returning a dict of lists: "solid" (rects (x, y, tile, tile) for each #), "coins" (centres of c cells), and "start" (the corner of the P cell, or (0, 0) if none).

איך הפלט אמור להיראות?What should the output look like?
{'solid': [(0, 16, 16, 16), (16, 16, 16, 16), (32, 16, 16, 16)], 'coins': [(40, 8)], 'start': (0, 0)}
main.py
רמזHint 1: לולאה כפולה עם enumerate.A double loop with enumerate.
רמזHint 2: מרכז: (c * tile + tile // 2, r * tile + tile // 2).Centre: (c * tile + tile // 2, r * tile + tile // 2).
לפתרוןSolution
תרגילExercise 2

תנועה עם התנגשותMoving with collision

בלי pygame: כתבו move(p, dx, vy, solids) כש-p הוא רשימה [x, y, w, h] ו-solids רשימת טאפלים. מזיזים ב-x (נעצרים בקיר), ואז ב-y (נוחתים או נתקלים בתקרה). מחזירה (vy, on_ground).

Without pygame: write move(p, dx, vy, solids) where p is a list [x, y, w, h] and solids a list of tuples. Move on x (stopping at walls), then on y (landing or bumping the ceiling). Return (vy, on_ground).

איך הפלט אמור להיראות?What should the output look like?
(0.0, True) [50, 86, 12, 14]
(0, False) [88, 86, 12, 14]
(0.0, False)
main.py
רמזHint 1: ימינה: p[0] = s[0] - p[2]; שמאלה: p[0] = s[0] + s[2].Right: p[0] = s[0] - p[2]; left: p[0] = s[0] + s[2].
רמזHint 2: נחיתה: p[1] = s[1] - p[3].Landing: p[1] = s[1] - p[3].
לפתרוןSolution
תרגילExercise 3

מצלמהA camera

כתבו camera(target_x, cam, screen_w, world_w, ease=0.15) שמחזירה מיקום מצלמה חדש שמתקרב בהדרגה (כמו קפיץ עם ריסון) אל target_x - screen_w // 3, מוגבל לטווח [0, world_w - screen_w]. ואז steps_to_settle(...) שמחזירה כמה קריאות צריך עד שהמצלמה במרחק פחות מפיקסל אחד מהיעד (מעוגל).

Write camera(target_x, cam, screen_w, world_w, ease=0.15) returning a new camera position that moves gradually (like a damped spring) toward target_x - screen_w // 3, clamped to [0, world_w - screen_w]. Then steps_to_settle(...) returning how many calls until the camera is less than one pixel from the goal (clamped).

איך הפלט אמור להיראות?What should the output look like?
36.0 34 8 0
main.py
רמזHint 1: goal = max(0, min(world_w - screen_w, target_x - screen_w // 3))goal = max(0, min(world_w - screen_w, target_x - screen_w // 3))
רמזHint 2: cam + (goal - cam) * easecam + (goal - cam) * ease
לפתרוןSolution
תרגילExercise 4

סיירA patroller

כתבו patrol(x, d, speed, ground_cells, tile, steps) שמזיזה אויב (x בפיקסלים, רוחב 14) steps צעדים: לפני כל צעד בודקים שהמשבצת שמתחת לנקודה שלפניו (x + 14 + 1 ימינה, או x - 1 שמאלה) נמצאת ב-ground_cells (set של אינדקסי עמודות), אחרת הופכים כיוון. מחזירה (x, d, turns).

Write patrol(x, d, speed, ground_cells, tile, steps) moving an enemy (x in pixels, width 14) for steps steps: before each step check that the cell under the point ahead (x + 14 + 1 to the right, or x - 1 to the left) is in ground_cells (a set of column indexes), otherwise flip direction. Return (x, d, turns).

איך הפלט אמור להיראות?What should the output look like?
(138, 1, 2)
True True
main.py
רמזHint 1: נקודה קדמית: front = x + 15 if d > 0 else x - 1.Front point: front = x + 15 if d > 0 else x - 1.
רמזHint 2: עמודה: front // tile.Column: front // tile.
לפתרוןSolution
תרגילExercise 5

Pixel Platformer: השלבPixel Platformer: the level

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

כתבו מחלקה Level(rows, tile=16) שטוענת מפה (# קרקע, c מטבע, P התחלה, F דגל), עם שחקן 12×14 בנקודת ההתחלה, coins, score, won. step(left, right, jump): תנועה אופקית 2 פיקסלים, קפיצה (-6.2) רק על הקרקע, כבידה 0.35 עד 8, התנגשות בהפרדת צירים, איסוף מטבעות (חפיפה עם ריבוע 8×8 במרכז המשבצת), וניצחון בנגיעה במשבצת הדגל.

Write a Level(rows, tile=16) class loading a map (# ground, c coin, P start, F flag), with a 12×14 player at the start, coins, score, won. step(left, right, jump): 2-pixel horizontal movement, a jump (-6.2) only when grounded, gravity 0.35 up to 8, axis-separated collision, coin pickup (overlap with an 8×8 square at the cell centre), and winning on touching the flag cell.

איך הפלט אמור להיראות?What should the output look like?
True 1 87
platformer.py
רמזHint 1: השתמשו ב-move מתרגיל 2 ובטעינה מתרגיל 1.Use move from exercise 2 and loading from exercise 1.
רמזHint 2: אחרי ניצחון step לא עושה כלום.After winning, step does nothing.
לפתרוןSolution

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

אתגרChallenge 1

זמן קויוטה וזיכרון קפיצהCoyote time and jump buffering

שני טריקים שגורמים לפלטפורמר להרגיש טוב: זמן קויוטה (מותר לקפוץ עד 6 פריימים אחרי שיצאתם מהקרקע) וזיכרון קפיצה (לחיצה עד 6 פריימים לפני הנחיתה נשמרת ומבוצעת בנחיתה). כתבו JumpControl(coyote=6, buffer=6) עם update(on_ground, pressed) שמחזירה True בפריים שבו צריך לקפוץ (ואז מאפסת את שני המונים).

Two tricks that make a platformer feel good: coyote time (jumping allowed up to 6 frames after leaving the ground) and jump buffering (a press up to 6 frames before landing is kept and carried out on landing). Write JumpControl(coyote=6, buffer=6) with update(on_ground, pressed) returning True on the frame a jump should happen (then resetting both counters).

איך הפלט אמור להיראות?What should the output look like?
7 6 False
main.py
רמזHint 1: self.since_ground = 0 if on_ground else self.since_ground + 1self.since_ground = 0 if on_ground else self.since_ground + 1
רמזHint 2: self.since_press = 0 if pressed else self.since_press + 1self.since_press = 0 if pressed else self.since_press + 1
לפתרוןSolution
אתגרChallenge 2

עורך שלבים: בדיקת נגישותLevel editor: reachability

לפני שמפרסמים שלב, בודקים שאפשר לסיים אותו. כתבו reachable(rows, max_jump=3): מיקומים חוקיים הם משבצות ריקות שיש מתחתן #. מכל מיקום אפשר ללכת ימינה/שמאלה למיקום חוקי באותה שורה, לעלות עד max_jump שורות לעמודה סמוכה (או אותה עמודה), ולרדת כל מספר שורות לעמודה סמוכה. החזירו True אם מ-P אפשר להגיע ל-F (BFS).

Before publishing a level, check it can be finished. Write reachable(rows, max_jump=3): valid spots are empty cells with # below them. From a spot you may walk left/right to a valid spot in the same row, climb up to max_jump rows to an adjacent (or the same) column, and drop any number of rows to an adjacent column. Return True if F is reachable from P (BFS).

איך הפלט אמור להיראות?What should the output look like?
True False True
main.py
רמזHint 1: פונקציה standable(r, c): בתוך המפה, לא #, ומתחתיה #.A standable(r, c) function: inside the map, not #, and # below it.
רמזHint 2: שכנים: לכל עמודה ב-c-1..c+1 ולכל שורה ב-r-max_jump..rows-1 שהיא standable.Neighbours: for each column c-1..c+1 and each row r-max_jump..rows-1 that's standable.
לפתרוןSolution

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