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

PythonPython

מבחן 7: לוגיקה של משחקיםExam 7: Game logic

וקטורים, התנגשויות, מצבים ולולאת משחק (בלי pygame)

Vectors, collisions, states and the game loop (without pygame)

75 דקותminutes4 שאלות בחירהmultiple choice3 שאלות קודcoding questions100 נקודותpoints
75:00השעון רק עוזר להתאמן. אפשר גם בלי.The timer is just for practice. You can skip it.

חלק א: שאלות בחירה (5 נקודות לשאלה)Part A: multiple choice (5 points each)

התשובות מתגלות רק כשמסיימים את המבחן.Answers are revealed only when you finish the exam.

1. למה מכפילים מהירות ב-dt?Why multiply speed by dt? 5
2. מה בודקת התנגשות בין שני מלבנים?What does a collision between two rectangles check? 5
3. מה הבעיה בקוד?What's wrong with this code? 5
python
for b in bullets:
    b.y -= 5
    if b.y < 0:
        bullets.remove(b)
4. מהו "מנהור" (tunneling)?What is "tunneling"? 5

חלק ב: שאלות קודPart B: coding questions

שאלהQuestion 5

התנגשותCollision

כתבו overlap(a, b) למלבנים (x, y, w, h) (נגיעה בקצה אינה חפיפה), ו-circle_hit(c1, c2) למעגלים (x, y, r) (נגיעה כן נחשבת).

Write overlap(a, b) for rectangles (x, y, w, h) (touching edges isn't overlap), and circle_hit(c1, c2) for circles (x, y, r) (touching counts).

איך הפלט אמור להיראות?What should the output look like?
True False True
main.py
רמזHint 1: ארבעה תנאים עם <.Four conditions with <.
רמזHint 2: math.dist <= r1 + r2.math.dist <= r1 + r2.
הפתרון שלנוOur solution
main.py
import math

def overlap(a, b):
    return a[0] < b[0] + b[2] and b[0] < a[0] + a[2] and a[1] < b[1] + b[3] and b[1] < a[1] + a[3]

def circle_hit(c1, c2):
    return math.dist(c1[:2], c2[:2]) <= c1[2] + c2[2]

print(overlap((0, 0, 10, 10), (5, 5, 10, 10)), overlap((0, 0, 10, 10), (10, 0, 5, 5)), circle_hit((0, 0, 3), (6, 0, 3)))
פלטOutput
True False True

יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.

מחווןRubric (20 נקודותpoints)

כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.

שאלהQuestion 6

קפיצה עם כבידהJumping with gravity

כתבו simulate_jump(jump, gravity, frames) שמתחילה ב-y = 0, vy = jump (שלילי = למעלה), ובכל פריים: vy += gravity, y += vy, ואם y >= 0 נוחתים (y = 0, עוצרים). החזירו (max_height, landed_frame) כש-max_height חיובי (מעוגל לספרה אחת) ו-landed_frame הפריים שבו נחתו (מ-1), או None אם לא נחתו.

Write simulate_jump(jump, gravity, frames) starting at y = 0, vy = jump (negative = up), and each frame: vy += gravity, y += vy, and if y >= 0 land (y = 0, stop). Return (max_height, landed_frame) with max_height positive (rounded to one decimal) and landed_frame the frame of landing (from 1), or None if it didn't land.

איך הפלט אמור להיראות?What should the output look like?
(60.0, 31) (52.5, None)
main.py
רמזHint 1: top = min(top, y).top = min(top, y).
הפתרון שלנוOur solution
main.py
def simulate_jump(jump, gravity, frames):
    y, vy, top = 0.0, jump, 0.0
    for frame in range(1, frames + 1):
        vy += gravity
        y += vy
        top = min(top, y)
        if y >= 0:
            return round(-top, 1), frame
    return round(-top, 1), None

print(simulate_jump(-8, 0.5, 100), simulate_jump(-8, 0.5, 10))
פלטOutput
(60.0, 31) (52.5, None)

יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.

מחווןRubric (30 נקודותpoints)

כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.

שאלהQuestion 7

מכונת מצבים למשחקA game state machine

כתבו run(events) שמתחילה במצב "menu" ומעבדת רשימת אירועים: menu+start → play; play+pause → paused; paused+pause → play; play+die → over; over+start → play (וניקוד מתאפס); play+coin → ניקוד +1. אירועים אחרים מתעלמים. החזירו (state, score, best) כש-best הניקוד הגבוה ביותר שהושג.

Write run(events) starting in state "menu" and processing a list of events: menu+start → play; play+pause → paused; paused+pause → play; play+die → over; over+start → play (and score resets); play+coin → score +1. Other events are ignored. Return (state, score, best) with best the highest score reached.

איך הפלט אמור להיראות?What should the output look like?
('play', 1, 3)
main.py
רמזHint 1: מילון מעברים.A transitions dict.
רמזHint 2: עדכנו best אחרי כל מטבע.Update best after every coin.
הפתרון שלנוOur solution
main.py
MOVES = {("menu", "start"): "play", ("play", "pause"): "paused", ("paused", "pause"): "play", ("play", "die"): "over", ("over", "start"): "play"}

def run(events):
    state, score, best = "menu", 0, 0
    for e in events:
        if state == "play" and e == "coin":
            score += 1
            best = max(best, score)
        elif (state, e) in MOVES:
            if state == "over":
                score = 0
            state = MOVES[(state, e)]
    return state, score, best

print(run(["coin", "start", "coin", "coin", "pause", "coin", "pause", "coin", "die", "start", "coin"]))
פלטOutput
('play', 1, 3)

יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.

מחווןRubric (30 נקודותpoints)

כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.

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