pygame.sprite, Groups, Mask, inheritance, waves and a boss.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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
נשק עם המתנהA gun with a cooldown
כתבו מחלקה Gun(delay) עם tick() שמורידה את המונה (לא מתחת ל-0) ו-fire() שמחזירה True ומאפסת את המונה ל-delay אם מוכן, אחרת False. ואז shots_in(frames, delay) שמחזירה כמה יריות יוצאות כשמחזיקים ירי frames פריימים (קודם tick, אחר כך fire).
Write a Gun(delay) class with tick() lowering the counter (not below 0) and fire() returning True and resetting the counter to delay if ready, otherwise False. Then shots_in(frames, delay) returning how many shots fire when holding fire for frames frames (tick first, then fire).
איך הפלט אמור להיראות?What should the output look like?
5 60 1
main.py
רמזHint 1:מוכן כש-self.wait == 0.Ready when self.wait == 0.
רמזHint 2:sum(1 for _ in range(frames) if (g.tick() or g.fire()))sum(1 for _ in range(frames) if (g.tick() or g.fire()))
בלי pygame: כתבו group_hits(enemies, bullets) כששני המילונים ממפים שם → מלבן (x, y, w, h). מחזירה מילון אויב → רשימה ממוינת של היריות שפגעו בו, ומוחקת מהמילונים את האויבים שנפגעו ואת היריות שפגעו (כמו groupcollide(..., True, True)).
Without pygame: write group_hits(enemies, bullets) where both dicts map name → rect (x, y, w, h). Return a dict enemy → sorted list of the bullets that hit it, and remove the hit enemies and the hitting bullets from the dicts (like groupcollide(..., True, True)).
איך הפלט אמור להיראות?What should the output look like?
{'e1': ['b1', 'b2']} ['e2'] ['b3']
main.py
רמזHint 1:לולאה כפולה, ואז מחיקה אחרי הלולאה.A double loop, then delete after the loop.
רמזHint 2:ירייה שפגעה בשניים נמחקת פעם אחת: bullets.pop(b, None).A bullet hitting two is removed once: bullets.pop(b, None).
כתבו march(enemies, dx, width, margin=10, drop=10) שמקבלת רשימת מיקומי x,y. אם צעד של dx יוציא אויב כלשהו מהטווח [margin, width - margin], כולם יורדים drop והכיוון מתהפך; אחרת כולם זזים dx. מחזירה (new_enemies, new_dx).
Write march(enemies, dx, width, margin=10, drop=10) taking a list of x,y positions. If a step of dx would take any enemy outside [margin, width - margin], all drop by drop and the direction flips; otherwise all move by dx. Return (new_enemies, new_dx).
איך הפלט אמור להיראות?What should the output look like?
[[458, 50], [408, 50]] -8
main.py
רמזHint 1:any(not margin <= x + dx <= width - margin for x, _ in enemies)any(not margin <= x + dx <= width - margin for x, _ in enemies)
כתבו explode(x, y, n, rng, speed=3, life=20) שמחזירה n חלקיקים [x, y, vx, vy, life] עם מהירות אקראית בתוך [-speed, speed] לכל ציר (rng.uniform), ו-update(particles) שמזיזה, מורידה life ב-1, ומחזירה רשימה רק של החיים (life > 0).
Write explode(x, y, n, rng, speed=3, life=20) returning n particles [x, y, vx, vy, life] with a random velocity within [-speed, speed] per axis (rng.uniform), and update(particles) moving them, lowering life by 1, and returning only the living ones (life > 0).
איך הפלט אמור להיראות?What should the output look like?
[12, 12, 0, 0] True
main.py
רמזHint 1:[x, y, rng.uniform(-speed, speed), rng.uniform(-speed, speed), life][x, y, rng.uniform(-speed, speed), rng.uniform(-speed, speed), life]
רמזHint 2:רשימה חדשה: [p for p in ... if p[4] > 0].A new list: [p for p in ... if p[4] > 0].
כתבו מחלקה Shooter(seed=0, w=480, h=320): חללית ב-x=w/2, נשק עם delay 12, יריות (מלבנים 4×12 שעולים 8), גל אויבים (7×3 מלבנים 26×18 ב-x=60+c×50, y=40+r×30) שצועד ב-1.5, score, wave = 1. step(move, fire): מזיזה חללית (4 פיקסלים, בתוך המסך), יורה, מזיזה יריות (מוחקת שיצאו), צועדת, פגיעות (10 נקודות לאויב), וגל ריק = גל חדש עם wave += 1 ומהירות גדולה ב-0.4.
Write a Shooter(seed=0, w=480, h=320) class: a ship at x=w/2, a gun with delay 12, bullets (4×12 rects moving up 8), an enemy wave (7×3 rects 26×18 at x=60+c×50, y=40+r×30) marching at 1.5, score, wave = 1. step(move, fire): moves the ship (4 pixels, on screen), fires, moves bullets (removing those that left), marches, hits (10 points an enemy), and an empty wave = a new wave with wave += 1 and speed 0.4 faster.
איך הפלט אמור להיראות?What should the output look like?
2 210 True 1.9
space_shooter.py
רמזHint 1:השתמשו ב-Gun, march ו-group_hits מהתרגילים (גרסה עם רשימות).Use Gun, march and group_hits from the exercises (a list version).
רמזHint 2:ירייה יוצאת מ-(ship_x - 2, h - 40).A bullet starts at (ship_x - 2, h - 40).
יצירה ומחיקה של אלפי עצמים עולה זמן. כתבו BulletPool(size) שמחזיקה מראש size יריות (מילונים עם x, y, active=False). fire(x, y) מפעילה את הירייה הלא-פעילה הראשונה ומחזירה אותה, או None אם כולן פעילות. update(speed, top) מזיזה את הפעילות ומכבה את מי שעברה את top. active_count().
Creating and deleting thousands of objects costs time. Write BulletPool(size) holding size bullets up front (dicts with x, y, active=False). fire(x, y) activates the first inactive bullet and returns it, or None if all are active. update(speed, top) moves active ones and deactivates those past top. active_count().
איך הפלט אמור להיראות?What should the output look like?
[True, True, False] 3
0 True
main.py
רמזHint 1:next((b for b in self.items if not b["active"]), None)next((b for b in self.items if not b["active"]), None)
רמזHint 2:לא יוצרים מילונים חדשים אחרי הבנאי.Create no new dicts after the constructor.
כתבו enemy_fire(enemies, rng, chance): רק האויב התחתון בכל עמודה (לפי x מעוגל) יכול לירות, וכל אחד כזה יורה בהסתברות chance (rng.random() < chance). מחזירה רשימת נקודות (x + 13, y + 18) ממוינת. אויב הוא [x, y].
Write enemy_fire(enemies, rng, chance): only the lowest enemy in each column (by rounded x) may shoot, and each such enemy shoots with probability chance (rng.random() < chance). Return a sorted list of points (x + 13, y + 18). An enemy is [x, y].
איך הפלט אמור להיראות?What should the output look like?