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

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

Neon PongNeon Pong

Vector2, פיזיקת כדור, יריב מחשב, טיימרים וצלילים מסונתזים.

Vector2, ball physics, a computer opponent, timers and synthesized sound.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. למה שומרים מיקום עשרוני נפרד מה-Rect?Why keep a float position separate from the Rect?

2. מה קורה לכדור שפוגע בקצה העליון של המחבט?What happens to a ball hitting the paddle's top edge?

3. למה כופלים מהירות ב-dt?Why multiply speed by dt?

תרגיליםExercises

תרגילExercise 1

זווית החזרהRebound angle

כתבו rebound(ball_y, paddle_center, paddle_h, vx, max_vy=5) שמחזירה (new_vx, new_vy): vx מתהפך ומואץ פי 1.05, ו-vy הוא offset * max_vy כש-offset הוא המיקום היחסי (בין -1 ל-1, מוגבל). עגלו שניהם ל-2.

Write rebound(ball_y, paddle_center, paddle_h, vx, max_vy=5) returning (new_vx, new_vy): vx flips and speeds up ×1.05, and vy is offset * max_vy where offset is the relative position (from -1 to 1, clamped). Round both to 2.

איך הפלט אמור להיראות?What should the output look like?
(-5.25, 0.0) (5.25, 5.0) (-5.25, 5.0)
main.py
רמזHint 1: offset = (ball_y - paddle_center) / (paddle_h / 2)offset = (ball_y - paddle_center) / (paddle_h / 2)
רמזHint 2: max(-1.0, min(1.0, offset))max(-1.0, min(1.0, offset))
לפתרוןSolution
תרגילExercise 2

יריב הוגןA fair opponent

כתבו ai_move(paddle_y, ball_y, speed, dead=4) שמחזירה את מיקום המחבט החדש: מתקרב לכדור לכל היותר speed פיקסלים, לא עובר אותו, ולא זז אם ההפרש קטן או שווה ל-dead.

Write ai_move(paddle_y, ball_y, speed, dead=4) returning the paddle's new position: moves toward the ball by at most speed pixels, never past it, and doesn't move if the gap is at most dead.

איך הפלט אמור להיראות?What should the output look like?
[160, 166, 154, 165]
main.py
רמזHint 1: gap = ball_y - paddle_ygap = ball_y - paddle_y
רמזHint 2: paddle_y + max(-speed, min(speed, gap)) כשהפער גדול מ-dead.paddle_y + max(-speed, min(speed, gap)) when the gap exceeds dead.
לפתרוןSolution
תרגילExercise 3

ניקוד והגשהScoring and serving

כתבו check_point(ball_x, width, score) שמקבלת את x של הכדור ורשימת ניקוד [left, right]: אם הכדור יצא משמאל (x < 0) נקודה לימני, ואם מימין (x > width) נקודה לשמאלי. היא מעדכנת את הרשימה ומחזירה את כיוון ההגשה הבא: 1 (ימינה), -1 (שמאלה) או 0 אם אין נקודה. מגישים לכיוון מי שספג.

Write check_point(ball_x, width, score) taking the ball's x and a score list [left, right]: if the ball went out on the left (x < 0) the right player scores, and on the right (x > width) the left player scores. It updates the list and returns the next serve direction: 1 (right), -1 (left) or 0 if no point. Serve toward whoever conceded.

איך הפלט אמור להיראות?What should the output look like?
[-1, 0, 1, 1] [2, 1]
main.py
רמזHint 1: יצא משמאל = השמאלי ספג → מגישים שמאלה (-1).Out on the left = the left player conceded → serve left (-1).
רמזHint 2: score[1] += 1 לנקודה לימני.score[1] += 1 for a right-player point.
לפתרוןSolution
תרגילExercise 4

קצב קבועA steady pace

כתבו simulate(fps, seconds, speed, width) שמזיזה כדור מ-x=0 ימינה ב-speed פיקסלים לשנייה עם dt = 1/fps, מקפיצה מהקיר הימני ומהשמאלי (רדיוס 0), ומחזירה את x הסופי מעוגל. התוצאה צריכה להיות כמעט זהה ב-30 וב-120 FPS.

Write simulate(fps, seconds, speed, width) moving a ball from x=0 rightward at speed pixels per second with dt = 1/fps, bouncing off the right and left walls (radius 0), returning the final x rounded. The result should be almost the same at 30 and 120 FPS.

איך הפלט אמור להיראות?What should the output look like?
360 360 200
main.py
רמזHint 1: x += v * dt ואז בדיקת קירות.x += v * dt then check walls.
רמזHint 2: בקיר: x = 2 * width - x (שיקוף) והיפוך v.At a wall: x = 2 * width - x (mirror) and flip v.
לפתרוןSolution
תרגילExercise 5

Neon Pong: מנוע המשחקNeon Pong: the game engine

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

כתבו מחלקה Pong(w=480, h=320) עם ball (מילון x, y, vx, vy, size=12), left ו-right (מרכזי y של מחבטים בגובה 70, ב-x=16 ו-x=w-26, רוחב 10), ו-score = [0, 0]. step(left_move, right_move): מזיזה מחבטים (מוגבלים למסך), את הכדור, מקפיצה מתקרה ורצפה, מחזירה מהמחבטים עם זווית (כמו ב-rebound), ובנקודה מעדכנת ניקוד ומגישה מהמרכז לכיוון מי שספג.

Write a Pong(w=480, h=320) class with a ball (dict x, y, vx, vy, size=12), left and right (paddle centre ys, height 70, at x=16 and x=w-26, width 10), and score = [0, 0]. step(left_move, right_move): moves paddles (clamped to the screen), moves the ball, bounces off ceiling and floor, returns off paddles with an angle (as in rebound), and on a point updates the score and serves from the centre toward whoever conceded.

איך הפלט אמור להיראות?What should the output look like?
[6, 0] 270 160.0
neon_pong.py
רמזHint 1: חפיפה עם מחבט: בדיקת x בטווח המחבט ו-|ball_y - center| < 35 + size/2.Paddle overlap: x within the paddle and |ball_y - centre| < 35 + size/2.
רמזHint 2: החזרה רק כש-vx בכיוון המחבט.Return only when vx points toward the paddle.
לפתרוןSolution

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

אתגרChallenge 1

יריב שמנבאA predicting opponent

יריב חכם לא עוקב אחרי הכדור, אלא אחרי המקום שבו הכדור יגיע לקו המחבט. כתבו predict_y(x, y, vx, vy, target_x, height) שמחזירה את ה-y שבו הכדור יחצה את target_x, כולל הקפצות מהתקרה והרצפה (0 ו-height). אם הכדור מתרחק, מחזירים את מרכז המסך.

A smart opponent doesn't follow the ball, but where the ball will arrive at its paddle line. Write predict_y(x, y, vx, vy, target_x, height) returning the y where the ball crosses target_x, including bounces off the ceiling and floor (0 and height). If the ball moves away, return the screen centre.

איך הפלט אמור להיראות?What should the output look like?
160.0 288.4 160.0
main.py
רמזHint 1: זמן הגעה: t = (target_x - x) / vx; אם שלילי, מתרחק.Arrival time: t = (target_x - x) / vx; if negative, it's moving away.
רמזHint 2: שיקופים: p = (y + vy * t) % (2 * height) ואז 2 * height - p אם p > height.Reflections: p = (y + vy * t) % (2 * height) then 2 * height - p if p > height.
לפתרוןSolution
אתגרChallenge 2

שובל חלקיקיםA particle trail

כתבו מחלקה Trail(max_len=12) שזוכרת את המיקומים האחרונים של הכדור: add(x, y) מוסיפה ושומרת רק max_len אחרונים (השתמשו ב-collections.deque), ו-points() מחזירה רשימת (x, y, alpha, radius) מהישן לחדש: alpha עולה ליניארית מ-255 // max_len עד 255, ו-radius מ-1 עד 6 (מעוגל).

Write a Trail(max_len=12) class remembering the ball's last positions: add(x, y) adds and keeps only the last max_len (use collections.deque), and points() returns (x, y, alpha, radius) from oldest to newest: alpha rises linearly from 255 // max_len to 255, and radius from 1 to 6 (rounded).

איך הפלט אמור להיראות?What should the output look like?
[(20, 100, 63, 1), (30, 100, 127, 3), (40, 100, 191, 4), (50, 100, 255, 6)]
main.py
רמזHint 1: deque(maxlen=max_len) זורק לבד את הישן.deque(maxlen=max_len) drops the oldest by itself.
רמזHint 2: לפריט i מתוך n: alpha = 255 * (i + 1) // n... אבל לפי max_len כשהשובל מלא.For item i of n: alpha = 255 * (i + 1) // n, using max_len as the scale.
לפתרוןSolution

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