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

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

Tower Defense: הגנת מגדליםTower Defense

מסלול עם BFS, גלים, בחירת מטרה, כלכלה וממשק כפתורים.

Pathing with BFS, waves, targeting, an economy and a button UI.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. למה מנרמלים את הכיוון לנקודת הציון?Why normalise the direction to the waypoint?

2. מה עושה max(in_range, key=lambda e: e["travelled"])?What does max(in_range, key=lambda e: e["travelled"]) do?

3. מגדל עם cooldown של 20 ב-60 FPS יורה...A tower with a cooldown of 20 at 60 FPS fires...

תרגיליםExercises

תרגילExercise 1

אורך מסלולPath length

כתבו path_length(points) שמחזירה את אורך המסלול (סכום המרחקים בין נקודות עוקבות), ו-frames_to_finish(points, speed) שמחזירה כמה פריימים צריך כדי לעבור אותו (מעוגל למעלה, math.ceil).

Write path_length(points) returning the path's length (the sum of distances between consecutive points), and frames_to_finish(points, speed) returning how many frames it takes to cover it (rounded up, math.ceil).

איך הפלט אמור להיראות?What should the output look like?
720.0 480 3
main.py
רמזHint 1: zip(points, points[1:]) נותן זוגות עוקבים.zip(points, points[1:]) gives consecutive pairs.
רמזHint 2: math.dist(a, b).math.dist(a, b).
לפתרוןSolution
תרגילExercise 2

הליכה במסלולWalking the path

כתבו מחלקה Walker(path, speed) עם x, y (מתחיל בנקודה הראשונה), target (אינדקס הנקודה הבאה, מתחיל ב-1) ו-done. update() מתקדמת צעד: אם הנקודה הבאה במרחק <= speed, קופצים אליה ומקדמים את target; אחרת זזים צעד מנורמל. כשעוברים את הנקודה האחרונה, done הופך ל-True ו-update לא עושה כלום.

Write a Walker(path, speed) class with x, y (starting at the first point), target (index of the next point, starting at 1) and done. update() takes one step: if the next point is <= speed away, snap to it and advance target; otherwise move a normalised step. Once past the last point, done becomes True and update does nothing.

איך הפלט אמור להיראות?What should the output look like?
8 (10, 10)
main.py
רמזHint 1: done = target == len(path).done = target == len(path).
רמזHint 2: השתמשו ב-math.hypot.Use math.hypot.
לפתרוןSolution
תרגילExercise 3

בחירת מטרהChoosing a target

כתבו pick(tower, reach, enemies, mode) כש-enemies רשימת מילונים עם pos, travelled, hp. מחזירה את האויב שנבחר מבין אלה במרחק <= reach, או None אם אין. mode הוא "first" (הכי רחוק במסלול), "close" (הכי קרוב למגדל) או "strong" (הכי הרבה hp).

Write pick(tower, reach, enemies, mode) where enemies is a list of dicts with pos, travelled, hp. Return the chosen enemy among those <= reach away, or None if none. mode is "first" (furthest along), "close" (nearest the tower) or "strong" (most hp).

איך הפלט אמור להיראות?What should the output look like?
[3, 2, 2] None
main.py
רמזHint 1: מילון של פונקציות key לפי mode.A dict of key functions by mode.
רמזHint 2: ל-close צריך min, או key שלילי.close needs min, or a negated key.
לפתרוןSolution
תרגילExercise 4

לוח גליםA wave schedule

כתבו schedule(waves, pause=120) כש-waves רשימת מילונים עם count, gap, hp. מחזירה רשימת טאפלים (frame, wave_number, hp) לכל אויב, כשגל מתחיל pause פריימים אחרי שהאויב האחרון של הקודם הוצב (כלומר אחרי gap שלו). ו-total_hp(waves) שמחזירה מילון {wave_number: hp_total}.

Write schedule(waves, pause=120) where waves is a list of dicts with count, gap, hp. Return a list of (frame, wave_number, hp) tuples for every enemy, with a wave starting pause frames after the previous wave's last spawn (that is, after its gap). And total_hp(waves) returning a dict {wave_number: hp_total}.

איך הפלט אמור להיראות?What should the output look like?
[(0, 1, 3), (30, 1, 3), (180, 2, 5), (190, 2, 5), (200, 2, 5)]
{1: 6, 2: 15}
main.py
רמזHint 1: שמרו מונה frame שגדל ב-gap אחרי כל אויב וב-pause אחרי כל גל.Keep a frame counter that grows by gap after each enemy and by pause after each wave.
רמזHint 2: enumerate(waves, start=1).enumerate(waves, start=1).
לפתרוןSolution
תרגילExercise 5

Tower Defense: הסימולציהTower Defense: the simulation

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

כתבו simulate(path, towers, waves, frames) בלי pygame. אויבים (Walker מתרגיל 2 עם hp) מופיעים לפי schedule מתרגיל 4 במהירות 1.5. כל מגדל (x, y) עם טווח 70 ו-cooldown 18 פוגע מיד (בלי קליע) באויב "הראשון" בטווח, ומוריד 1 hp. אויב עם 0 hp מת (+5 זהב); אויב שמסיים את המסלול מוריד חיים. מחזירה מילון lives (מתחיל ב-10), gold (מתחיל ב-0), killed, alive.

Write simulate(path, towers, waves, frames) without pygame. Enemies (exercise 2's Walker with hp) spawn per exercise 4's schedule at speed 1.5. Each tower (x, y) with range 70 and cooldown 18 instantly hits (no bullet) the "first" enemy in range, removing 1 hp. An enemy at 0 hp dies (+5 gold); an enemy finishing the path costs a life. Return a dict of lives (starting at 10), gold (starting at 0), killed, alive.

איך הפלט אמור להיראות?What should the output look like?
{'lives': 6, 'gold': 35, 'killed': 7, 'alive': 0}
{'lives': 10, 'gold': 55, 'killed': 11, 'alive': 0}
{'lives': -1, 'gold': 0, 'killed': 0, 'alive': 0}
tower_defense.py
רמזHint 1: בכל פריים: הצבה, תנועה, ירי, ניקוי.Each frame: spawn, move, shoot, clean up.
רמזHint 2: ל-"first" צריך כמה האויב עבר: ספרו travelled ב-update."first" needs how far an enemy went: count travelled in update.
לפתרוןSolution

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

אתגרChallenge 1

ירי עם הקדמהLeading the shot

קליע ישר (לא מונחה) צריך לכוון לאן שהאויב יהיה. כתבו lead(shooter, target, vel, speed) שמחזירה את נקודת המפגש: הזמן t החיובי הקטן ביותר שבו |target + vel*t - shooter| = speed*t. זו משוואה ריבועית a t² + b t + c = 0 עם a = vx²+vy²-speed², b = 2(dx·vx + dy·vy), c = dx²+dy². החזירו (x, y) מעוגלים לספרה אחת, או None אם אין פתרון חיובי.

A straight (non-homing) bullet must aim where the enemy will be. Write lead(shooter, target, vel, speed) returning the meeting point: the smallest positive t with |target + vel*t - shooter| = speed*t. It's a quadratic a t² + b t + c = 0 with a = vx²+vy²-speed², b = 2(dx·vx + dy·vy), c = dx²+dy². Return (x, y) rounded to one decimal, or None if there's no positive solution.

איך הפלט אמור להיראות?What should the output look like?
(100.0, 75.0)
(100.0, 0.0)
None
main.py
רמזHint 1: dx, dy = target - shooter.dx, dy = target - shooter.
רמזHint 2: אם a == 0 המשוואה לינארית: t = -c / b.If a == 0 it's linear: t = -c / b.
לפתרוןSolution
אתגרChallenge 2

איפה לבנות?Where to build?

כתבו best_spot(path, spots, reach, step=2): לכל מקום אפשרי ב-spots, מדדו כמה מהמסלול נמצא בטווח שלו. דוגמים נקודות לאורך המסלול כל step פיקסלים (כולל נקודת ההתחלה של כל קטע), וסופרים כמה במרחק <= reach. מחזירה (spot, count) של המקום הטוב ביותר (בתיקו, הראשון).

Write best_spot(path, spots, reach, step=2): for each candidate in spots, measure how much of the path is within range. Sample points along the path every step pixels (including each segment's start), and count how many are <= reach away. Return (spot, count) for the best spot (on a tie, the first).

איך הפלט אמור להיראות?What should the output look like?
((90, 110), 98)
main.py
רמזHint 1: לכל קטע (a, b): n = int(math.dist(a, b) // step) נקודות ב-a + (b - a) * i / n... או פשוט יותר: i * step / length.For each segment (a, b): points at a + (b - a) * (i * step / length) for i in range(int(length // step)).
רמזHint 2: max(..., key=...) מחזיר את הראשון בתיקו.max(..., key=...) returns the first on a tie.
לפתרוןSolution

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