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

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

Star Catcher: תופסים כוכביםStar Catcher

Rect, מקלדת, התנגשויות, טקסט, מצבי משחק ושיא בקובץ.

Rect, keyboard, collisions, text, game states and a saved high score.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. איזו דרך מתאימה לתנועה רציפה כל עוד מחזיקים חץ?Which fits continuous movement while an arrow is held?

2. מה מחזיר a.colliderect(b)?What does a.colliderect(b) return?

3. איך שומרים את x בין 0 ל-400?How do you keep x between 0 and 400?

תרגיליםExercises

תרגילExercise 1

הגבלה לטווחClamping

כתבו clamp(value, low, high) שמחזירה את הערך אם הוא בטווח, אחרת את הגבול הקרוב, ו-move_player(x, keys, speed, width, player_w) שמקבלת קבוצת מקשים לחוצים ({"left"}, {"right"}...) ומחזירה את x החדש בתוך המסך. שני החיצים יחד מבטלים זה את זה.

Write clamp(value, low, high) returning the value if in range, otherwise the nearest bound, and move_player(x, keys, speed, width, player_w) taking a set of pressed keys ({"left"}, {"right"}...) and returning the new on-screen x. Both arrows together cancel out.

איך הפלט אמור להיראות?What should the output look like?
400 0 100
main.py
רמזHint 1: ("right" in keys) - ("left" in keys) נותן 1, -1 או 0.("right" in keys) - ("left" in keys) gives 1, -1 or 0.
רמזHint 2: clamp(x + d * speed, 0, width - player_w)clamp(x + d * speed, 0, width - player_w)
לפתרוןSolution
תרגילExercise 2

חפיפת מלבניםRectangle overlap

בלי pygame: כתבו overlap(a, b) לשני מלבנים כטאפלים (x, y, w, h) שמחזירה True אם הם חופפים (נגיעה בקצה בלבד אינה חפיפה, בדיוק כמו ב-pygame).

Without pygame: write overlap(a, b) for two rects as tuples (x, y, w, h) returning True if they overlap (merely touching edges isn't overlap, exactly as in pygame).

איך הפלט אמור להיראות?What should the output look like?
True False True
main.py
רמזHint 1: פירוק: ax, ay, aw, ah = a.Unpack: ax, ay, aw, ah = a.
רמזHint 2: ax < bx + bw and bx < ax + aw and ...ax < bx + bw and bx < ax + aw and ...
לפתרוןSolution
תרגילExercise 3

כוכבים נופליםFalling stars

כתבו update_stars(stars, basket, speed, height) שמקבלת רשימת כוכבים (רשימות [x, y] של 16×16) וסל (x, y, w, h). היא מזיזה כל כוכב ב-speed למטה, ומחזירה (stars_left, caught, missed): נתפס = חופף לסל, הוחמץ = y גדול מ-height.

Write update_stars(stars, basket, speed, height) taking a list of stars ([x, y] lists, 16×16) and a basket (x, y, w, h). It moves every star down by speed and returns (stars_left, caught, missed): caught = overlaps the basket, missed = y greater than height.

איך הפלט אמור להיראות?What should the output look like?
([[300, 105]], 1, 1)
main.py
רמזHint 1: בונים רשימה חדשה במקום למחוק תוך כדי לולאה.Build a new list instead of removing while looping.
רמזHint 2: חפיפה כמו בתרגיל הקודם עם (x, y, 16, 16).Overlap as in the previous exercise with (x, y, 16, 16).
לפתרוןSolution
תרגילExercise 4

טקסט במרכזCentred text

בעזרת pygame: צרו חלון 300×120, כתבו GAME OVER בגופן ברירת המחדל בגודל 48 כך שהמרכז של הטקסט יהיה במרכז החלון, והדפיסו את מרכז ה-rect של הטקסט ואם הוא נכנס כולו בחלון.

With pygame: create a 300×120 window, write GAME OVER in the default font at size 48 so the text's centre is the window's centre, and print the text rect's centre and whether it fits entirely in the window.

איך הפלט אמור להיראות?What should the output look like?
(150, 60) True
main.py
רמזHint 1: rect = surf.get_rect(center=(150, 60))rect = surf.get_rect(center=(150, 60))
רמזHint 2: screen.get_rect().contains(rect)screen.get_rect().contains(rect)
לפתרוןSolution
תרגילExercise 5

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

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

כתבו מחלקה Game(width, height) עם סל basket = [x, y, 80, 16] בתחתית במרכז, רשימת stars, score, lives = 3 ו-state = "playing". spawn(x) מוסיפה כוכב ב-[x, -16]. tick(keys) (במצב playing בלבד): מזיזה את הסל 6 פיקסלים לפי המקשים (בתוך המסך), מזיזה כוכבים 4 למטה, תפיסה = נקודה, פספוס = חיים, ובאפס חיים state = "over".

Write a Game(width, height) class with a basket = [x, y, 80, 16] centred at the bottom, a stars list, score, lives = 3 and state = "playing". spawn(x) adds a star at [x, -16]. tick(keys) (only while playing): moves the basket 6 pixels by the keys (within the screen), moves stars 4 down, a catch = a point, a miss = a life, and at zero lives state = "over".

איך הפלט אמור להיראות?What should the output look like?
1 2 playing [200, 290]
star_catcher.py
רמזHint 1: הסל: [(width - 80) // 2, height - 30, 80, 16].The basket: [(width - 80) // 2, height - 30, 80, 16].
רמזHint 2: בתחילת tick: if self.state != "playing": return.At the start of tick: if self.state != "playing": return.
לפתרוןSolution

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

אתגרChallenge 1

קושי עולהRising difficulty

כתבו difficulty(score) שמחזירה (fall_speed, spawn_every): מתחילים ב-(3, 40), כל 5 נקודות המהירות עולה ב-1 וזמן ההופעה יורד ב-4 פריימים, עד מקסימום מהירות 10 ומינימום 12 פריימים. ואז simulate(catches) שמקבלת רשימת True/False (תפיסה/פספוס) לכל כוכב לפי הסדר ומחזירה את הניקוד ואת הקושי בסוף, ועוצרת בשלוש פספוסים.

Write difficulty(score) returning (fall_speed, spawn_every): start at (3, 40), every 5 points speed goes up by 1 and spawn time down by 4 frames, up to a max speed of 10 and a min of 12 frames. Then simulate(catches) taking True/False (catch/miss) per star in order and returning the score and the final difficulty, stopping at three misses.

איך הפלט אמור להיראות?What should the output look like?
(3, 40) (5, 32) (10, 12)
(8, (4, 36))
main.py
רמזHint 1: level = score // 5level = score // 5
רמזHint 2: min(10, 3 + level), max(12, 40 - 4 * level)min(10, 3 + level), max(12, 40 - 4 * level)
לפתרוןSolution
אתגרChallenge 2

טבלת שיאיםA high-score table

כתבו add_score(table, name, score, size=5) שמכניסה שיא לטבלה (רשימת זוגות (name, score) ממוינת מהגבוה), שומרת רק size הטובים, ובשוויון השיא הוותיק נשאר לפני החדש. מחזירה את המקום (1 ומעלה) או None אם לא נכנס. ואז to_text(table) שמחזירה שורות 1. Noa ....... 42 ברוחב 20 (שם משמאל, ניקוד מימין, נקודות באמצע).

Write add_score(table, name, score, size=5) inserting a score into a table (a list of (name, score) pairs sorted high to low), keeping only the best size, and on a tie the older score stays ahead of the new one. Return the place (1 and up) or None if it didn't make it. Then to_text(table) returning lines like 1. Noa ....... 42 at width 20 (name on the left, score on the right, dots between).

איך הפלט אמור להיראות?What should the output look like?
1 4 None None
1. Noa .......... 42
2. Dana ......... 40
3. Avi .......... 30
4. Tal .......... 30
main.py
רמזHint 1: המקום: הראשון שהניקוד שלו קטן מהחדש.The place: the first whose score is lower than the new one.
רמזHint 2: נקודות: "." * (20 - len(left) - len(right) - 2).Dots: "." * (20 - len(left) - len(right) - 2).
לפתרוןSolution

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