Format code with black

This commit is contained in:
James Campbell
2026-08-01 12:19:46 -04:00
parent b0ad9610be
commit f29d8c953c
16 changed files with 459 additions and 311 deletions

View File

@@ -6,9 +6,12 @@ from game.traps import make_condition
def ev(spec, px, py, w=20, h=28, dt=0.0):
trap = type("T", (), {"tile": 32,
"sensor_rect": lambda self: pygame.Rect(100, 100, 32, 32)})()
trap = type(
"T", (), {"tile": 32, "sensor_rect": lambda self: pygame.Rect(100, 100, 32, 32)}
)()
return make_condition(spec).evaluate(trap, FakeGame(pygame.Rect(px, py, w, h)), dt)
# trap centre = (116, 116); left100 right132 top100 bottom132
@@ -17,26 +20,26 @@ def test_always():
def test_within_radius():
assert ev({"within": 2}, 106, 104) is True # ~10px away
assert ev({"within": 2}, 400, 116) is False # far
assert ev({"within": 2}, 106, 104) is True # ~10px away
assert ev({"within": 2}, 400, 116) is False # far
def test_dir_left_right():
assert ev({"dir": "left"}, 40, 105) is True # to the left
assert ev({"dir": "left"}, 200, 105) is False # to the right
assert ev({"dir": "left"}, 40, 105) is True # to the left
assert ev({"dir": "left"}, 200, 105) is False # to the right
assert ev({"dir": "right"}, 200, 105) is True
def test_dir_range():
assert ev({"dir": "left", "range": 2}, 60, 105) is True # within 2 tiles left
assert ev({"dir": "left", "range": 2}, 10, 105) is False # too far left
assert ev({"dir": "left", "range": 2}, 60, 105) is True # within 2 tiles left
assert ev({"dir": "left", "range": 2}, 10, 105) is False # too far left
def test_dir_aligned():
# above + aligned requires horizontal overlap with the trap column
assert ev({"dir": "above", "aligned": True}, 108, 60) is True
assert ev({"dir": "above", "aligned": True}, 108, 110) is False # not above
assert ev({"dir": "above", "aligned": True}, 400, 60) is False # not aligned
assert ev({"dir": "above", "aligned": True}, 400, 60) is False # not aligned
# without aligned, any column counts
assert ev({"dir": "above"}, 400, 60) is True
@@ -44,8 +47,8 @@ def test_dir_aligned():
def test_dir_inclusive():
# trap column spans x 100..132 (centre 116). A player standing *inside* the
# column (centre 116) is neither strictly left nor strictly right of it.
assert ev({"dir": "left"}, 106, 105) is False # centre 116, inside -> not left
assert ev({"dir": "right"}, 106, 105) is False # inside -> not right
assert ev({"dir": "left"}, 106, 105) is False # centre 116, inside -> not left
assert ev({"dir": "right"}, 106, 105) is False # inside -> not right
# inclusive counts the trap's own tile as being on that side
assert ev({"dir": "left", "inclusive": True}, 106, 105) is True
assert ev({"dir": "right", "inclusive": True}, 106, 105) is True
@@ -59,24 +62,26 @@ def test_dir_inclusive():
def test_all_and_any():
c = {"all": [{"within": 3}, {"dir": "above", "aligned": True}]}
assert ev(c, 108, 80) is True
assert ev(c, 108, 110) is False # close but not above
assert ev(c, 108, 110) is False # close but not above
c2 = {"any": [{"dir": "left"}, {"dir": "right"}]}
assert ev(c2, 40, 105) is True
assert ev(c2, 110, 40) is False # above-only satisfies neither
assert ev(c2, 110, 40) is False # above-only satisfies neither
def test_timer_cycles():
c = make_condition({"timer": {"interval": 0.3, "up_time": 0.2}})
trap = type("T", (), {"tile": 32,
"sensor_rect": lambda self: pygame.Rect(0, 0, 32, 32)})()
trap = type(
"T", (), {"tile": 32, "sensor_rect": lambda self: pygame.Rect(0, 0, 32, 32)}
)()
g = FakeGame(pygame.Rect(0, 0, 4, 4))
states = [c.evaluate(trap, g, 1 / 60) for _ in range(30)]
assert states[0] is False # starts in the "off" interval
assert any(states) and not all(states) # cycles on and off
assert states[0] is False # starts in the "off" interval
assert any(states) and not all(states) # cycles on and off
def test_bad_condition_raises():
import pytest
with pytest.raises(ValueError):
make_condition({"nope": 1})
@@ -97,10 +102,10 @@ traps:
delay: 0.25
""")
sp = lvl.traps[0]
g = FakeGame(sp.base_rect.copy()) # player right on it -> in range
g = FakeGame(sp.base_rect.copy()) # player right on it -> in range
armed = None
for i in range(30):
if sp.triggered(g, 1 / 60):
armed = i
break
assert armed is not None and 13 <= armed <= 17 # ~0.25s = 15 frames
assert armed is not None and 13 <= armed <= 17 # ~0.25s = 15 frames