Format code with black
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
import hashlib
|
||||
import pygame
|
||||
from conftest import (FakeGame, step, run, place, hold, DT)
|
||||
from conftest import FakeGame, step, run, place, hold, DT
|
||||
from game.assets import AssetStore
|
||||
from game.traps import Spike, ArrowShooter, Warp, PhaseBlock, Block
|
||||
|
||||
@@ -51,7 +51,7 @@ traps:
|
||||
def test_spike_sprite_rotates_per_direction(tmp_path):
|
||||
# A deliberately asymmetric sprite so each rotation is distinct.
|
||||
surf = pygame.Surface((8, 8), pygame.SRCALPHA)
|
||||
surf.fill((255, 0, 0, 255), (0, 0, 8, 2)) # red bar along the top only
|
||||
surf.fill((255, 0, 0, 255), (0, 0, 8, 2)) # red bar along the top only
|
||||
adir = tmp_path / "assets"
|
||||
adir.mkdir()
|
||||
pygame.image.save(surf, str(adir / "spike.png"))
|
||||
@@ -62,7 +62,8 @@ def test_spike_sprite_rotates_per_direction(tmp_path):
|
||||
|
||||
def h(s):
|
||||
return hashlib.md5(pygame.image.tostring(s, "RGBA")).hexdigest()
|
||||
assert h(up) != h(down) # rotation actually happened
|
||||
|
||||
assert h(up) != h(down) # rotation actually happened
|
||||
assert left.get_size() == (16, 32) and up.get_size() == (32, 16)
|
||||
|
||||
|
||||
@@ -85,10 +86,10 @@ traps:
|
||||
""")
|
||||
sh = lvl.traps[0]
|
||||
g = FakeGame(pygame.Rect(0, 0, 4, 4))
|
||||
for _ in range(40): # ~0.66s -> at least one shot
|
||||
for _ in range(40): # ~0.66s -> at least one shot
|
||||
sh.update(DT, g)
|
||||
assert sh.arrows # spawned arrows
|
||||
assert sh.hazard_rects() # arrows are hazards
|
||||
assert sh.arrows # spawned arrows
|
||||
assert sh.hazard_rects() # arrows are hazards
|
||||
|
||||
|
||||
def test_arrow_shooter_trigger_gates_firing(make_level):
|
||||
@@ -108,10 +109,10 @@ traps:
|
||||
trigger: { within: 1 }
|
||||
""")
|
||||
sh = lvl.traps[0]
|
||||
far = FakeGame(pygame.Rect(0, 0, 4, 4)) # nowhere near
|
||||
far = FakeGame(pygame.Rect(0, 0, 4, 4)) # nowhere near
|
||||
for _ in range(60):
|
||||
sh.update(DT, far)
|
||||
assert not sh.arrows # never fired while player out of range
|
||||
assert not sh.arrows # never fired while player out of range
|
||||
|
||||
|
||||
# --- invisible wall, now an array of invisible blocks ------------------------
|
||||
@@ -132,10 +133,10 @@ traps:
|
||||
count: [1, 2]
|
||||
""")
|
||||
blocks = [t for t in lvl.traps if isinstance(t, Block)]
|
||||
assert len(blocks) == 2 # one per cell
|
||||
assert len(blocks) == 2 # one per cell
|
||||
assert all(b.invisible and b.solid_rects() for b in blocks)
|
||||
ys = sorted(b.current_rect().top for b in blocks)
|
||||
assert ys == [1 * 32, 2 * 32] # stacked vertically
|
||||
assert ys == [1 * 32, 2 * 32] # stacked vertically
|
||||
|
||||
|
||||
# --- warp --------------------------------------------------------------------
|
||||
@@ -178,9 +179,9 @@ traps:
|
||||
to: [7, 1]
|
||||
""")
|
||||
warp = g.level.traps[0]
|
||||
assert warp._pulse == 0.0 # dormant: no aura
|
||||
place(g, 4, 2) # stand on the warp tile
|
||||
step(g) # -> teleports, aura flashes on
|
||||
assert warp._pulse == 0.0 # dormant: no aura
|
||||
place(g, 4, 2) # stand on the warp tile
|
||||
step(g) # -> teleports, aura flashes on
|
||||
assert warp._pulse > 0.9
|
||||
|
||||
# The aura paints at BOTH the source and the destination — even though the
|
||||
@@ -218,9 +219,9 @@ traps:
|
||||
pb = g.level.traps[0]
|
||||
place(g, 1, 3)
|
||||
step(g)
|
||||
assert not pb.solid and pb.alpha == 0 # dormant far away
|
||||
assert not pb.solid and pb.alpha == 0 # dormant far away
|
||||
run(g, 40, lambda i: hold(right=True))
|
||||
assert pb.solid and pb.alpha > 0 # phased in solid on approach
|
||||
assert pb.solid and pb.alpha > 0 # phased in solid on approach
|
||||
|
||||
|
||||
def test_phase_block_kills_if_inside_when_it_forms(make_game):
|
||||
@@ -239,7 +240,7 @@ traps:
|
||||
trigger: { within: 3 }
|
||||
""")
|
||||
pb = g.level.traps[0]
|
||||
place(g, 3, 2) # standing right where it will form
|
||||
place(g, 3, 2) # standing right where it will form
|
||||
killed, d0 = False, g.deaths
|
||||
for _ in range(10):
|
||||
step(g)
|
||||
@@ -268,7 +269,7 @@ traps:
|
||||
pb = g.level.traps[0]
|
||||
b = pb.base_rect
|
||||
# Straddle the block's left edge: mostly outside, just clipping into it.
|
||||
g.player.fx = float(b.left - g.player.w + 5) # 5px of overlap
|
||||
g.player.fx = float(b.left - g.player.w + 5) # 5px of overlap
|
||||
g.player.fy = float(b.top + 2)
|
||||
g.player.vx = g.player.vy = 0.0
|
||||
g.player._sync_rect()
|
||||
@@ -331,21 +332,23 @@ traps:
|
||||
trigger: { within: 3 }
|
||||
""")
|
||||
pb = g.level.traps[0]
|
||||
place(g, 5, 2) # near enough to trigger, not on the cell
|
||||
for _ in range(6): # let it partially fade in
|
||||
place(g, 5, 2) # near enough to trigger, not on the cell
|
||||
for _ in range(6): # let it partially fade in
|
||||
step(g)
|
||||
assert 0 < pb.alpha < 1
|
||||
g._start_death() # die from something
|
||||
assert pb.alpha == 1.0 and pb.solid # snapped fully visible for the freeze
|
||||
g._start_death() # die from something
|
||||
assert pb.alpha == 1.0 and pb.solid # snapped fully visible for the freeze
|
||||
|
||||
|
||||
def test_debug_overscan_reveals_offmap_trap(make_level):
|
||||
# In debug the play surface is enlarged and shifted (render_offset) so a trap
|
||||
# placed just off the map is drawn into the overscan instead of being clipped.
|
||||
from game import settings as S
|
||||
|
||||
lvl = make_level(
|
||||
"name: t\ntile_size: 32\ndebug: true\nmap: |\n #####\n #...#\n #####\n"
|
||||
"traps:\n - type: block\n at: [5, 1]\n deadly: true\n") # col 5 = off-map
|
||||
"traps:\n - type: block\n at: [5, 1]\n deadly: true\n"
|
||||
) # col 5 = off-map
|
||||
a = AssetStore("noassets")
|
||||
m = S.DEBUG_VIEW_MARGIN * lvl.tile
|
||||
lvl.render_offset = (m, m)
|
||||
@@ -354,7 +357,7 @@ def test_debug_overscan_reveals_offmap_trap(make_level):
|
||||
lvl.draw(surf, a)
|
||||
cx = 5 * lvl.tile + m + lvl.tile // 2
|
||||
cy = 1 * lvl.tile + m + lvl.tile // 2
|
||||
assert surf.get_at((cx, cy))[:3] != (0, 0, 0) # off-map block painted in overscan
|
||||
assert surf.get_at((cx, cy))[:3] != (0, 0, 0) # off-map block painted in overscan
|
||||
|
||||
|
||||
# --- generic invisible flag --------------------------------------------------
|
||||
@@ -384,9 +387,12 @@ traps:
|
||||
w = pygame.Surface((lvl.width, lvl.height))
|
||||
w.fill((0, 0, 0))
|
||||
sp.render(w, a)
|
||||
return any(w.get_at((x, y))[:3] != (0, 0, 0)
|
||||
for y in range(32, 96) for x in range(32, 96))
|
||||
return any(
|
||||
w.get_at((x, y))[:3] != (0, 0, 0)
|
||||
for y in range(32, 96)
|
||||
for x in range(32, 96)
|
||||
)
|
||||
|
||||
assert painted(False) is False # invisible in play
|
||||
assert painted(True) is True # revealed in debug
|
||||
assert sp.hazard_rects() # still deadly either way
|
||||
assert painted(False) is False # invisible in play
|
||||
assert painted(True) is True # revealed in debug
|
||||
assert sp.hazard_rects() # still deadly either way
|
||||
|
||||
Reference in New Issue
Block a user