מסלול עם 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
תרגילים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.
כתבו מחלקה 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?
כתבו 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.
כתבו 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?
רמזHint 1:שמרו מונה frame שגדל ב-gap אחרי כל אויב וב-pause אחרי כל גל.Keep a frame counter that grows by gap after each enemy and by pause after each wave.
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?
קליע ישר (לא מונחה) צריך לכוון לאן שהאויב יהיה. כתבו 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.
כתבו 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.