Coverage for src/retrospeedlab/games/tiltknight/tableview.py: 100%
103 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-03 23:09 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-03 23:09 +0000
1import pyxel
3from retrospeedlab.games.shared.ram import Memory
4from retrospeedlab.games.tiltknight import layout as at
5from retrospeedlab.games.tiltknight.board import ARC, Ramp, Table
6from retrospeedlab.games.tiltknight.codes import GATE, POST, SLING, TARGET
7from retrospeedlab.games.tiltknight.draw import sprite, thick
8from retrospeedlab.games.tiltknight.floorview import draw_floor_look
9from retrospeedlab.games.tiltknight.geometry import Circle, Segment
10from retrospeedlab.games.tiltknight.palette import (
11 FLOOR_TINTS,
12 GOLD,
13 GREEN,
14 INK,
15 MINT,
16 MIST,
17 NIGHT,
18 ORANGE,
19 RED,
20 SLATE,
21 STONE,
22 WHITE,
23)
24from retrospeedlab.games.tiltknight.stage import cleared, current_table, floor_index, in_endless
25from retrospeedlab.games.tiltknight.targets import gate_open, next_shield
26from retrospeedlab.games.tiltknight.tuning import (
27 CAR_FRAMES,
28 FIELD_LEFT,
29 FIELD_RIGHT,
30 FIELD_TOP,
31 SCREEN_HEIGHT,
32 SCREEN_WIDTH,
33)
35PANEL_BRICK = 12
36BLINK = 8
37CHEVRONS = 3
38CHEVRON_GAP = 9
39CHEVRON_HALF = 4
40SLING_FOOT = 12
41SHIELD_HALF = 3
42CAR_HALF = 4
43CAR_DEPTH = 10
44CORNER = (FIELD_RIGHT, FIELD_TOP)
47def floor_look(ram: Memory) -> int:
48 return cleared(ram) % (len(FLOOR_TINTS) - 1) if in_endless(ram) else floor_index(ram)
51def draw_walls_outside() -> None:
52 pyxel.cls(INK)
53 for y in range(0, SCREEN_HEIGHT, PANEL_BRICK):
54 offset = PANEL_BRICK if y // PANEL_BRICK % 2 else 0
55 pyxel.line(0, y, SCREEN_WIDTH, y, NIGHT)
56 for x in range(offset, SCREEN_WIDTH, PANEL_BRICK * 2):
57 pyxel.line(x, y, x, y + PANEL_BRICK - 1, NIGHT)
60def draw_backdrop(ram: Memory) -> None:
61 look = floor_look(ram)
62 draw_floor_look(look, *FLOOR_TINTS[look], ram[at.FRAME])
63 pyxel.tri(FIELD_LEFT, FIELD_TOP, 62, FIELD_TOP, FIELD_LEFT, 42, INK)
64 for start, end in zip(ARC, ARC[1:]):
65 pyxel.tri(*start, *end, *CORNER, INK)
68def draw_ramp(ram: Memory, ramp: Ramp) -> None:
69 (mx1, my1), (mx2, my2) = ramp.gate.start, ramp.gate.end
70 (bx1, by1), (bx2, by2) = ramp.walls[2].start, ramp.walls[2].end
71 pyxel.tri(mx1, my1, mx2, my2, bx1, by1, SLATE)
72 pyxel.tri(mx2, my2, bx1, by1, bx2, by2, SLATE)
73 if not gate_open(ram):
74 thick(ramp.gate.start, ramp.gate.end, STONE)
75 pyxel.line(mx1, my1, mx2, my2, RED)
76 return
77 if ram[at.LIFT_CALLED]:
78 drop = (CAR_FRAMES - ram[at.LIFT_CALLED]) * CAR_DEPTH // CAR_FRAMES
79 pyxel.rect((bx1 + bx2) // 2 - CAR_HALF, min(by1, by2) - CAR_DEPTH + drop, CAR_HALF * 2, CAR_HALF, GOLD)
80 lit = ram[at.FRAME] // BLINK % CHEVRONS
81 for index in range(CHEVRONS):
82 t = index * CHEVRON_GAP + CHEVRON_GAP
83 mid_x = (mx1 + mx2) // 2 + ((bx1 + bx2) // 2 - (mx1 + mx2) // 2) * t // (CHEVRON_GAP * (CHEVRONS + 1))
84 mid_y = (my1 + my2) // 2 + ((by1 + by2) // 2 - (my1 + my2) // 2) * t // (CHEVRON_GAP * (CHEVRONS + 1))
85 color = MINT if index == lit else GREEN
86 dx, dy = (bx1 + bx2 - mx1 - mx2) // 2, (by1 + by2 - my1 - my2) // 2
87 length = max(1, abs(dx) + abs(dy))
88 tip = mid_x + dx * CHEVRON_HALF // length, mid_y + dy * CHEVRON_HALF // length
89 side = -dy * CHEVRON_HALF // length, dx * CHEVRON_HALF // length
90 pyxel.line(mid_x + side[0], mid_y + side[1], tip[0], tip[1], color)
91 pyxel.line(mid_x - side[0], mid_y - side[1], tip[0], tip[1], color)
94def draw_circle(ram: Memory, shape: Circle) -> None:
95 x, y = shape.center
96 if shape.kind == POST:
97 pyxel.circ(x, y, shape.radius, STONE)
98 pyxel.circb(x, y, shape.radius, MIST)
99 return
100 flash = ram[at.BUMPER_FLASH + shape.index % at.BUMPER_SLOTS]
101 pyxel.circ(x, y, shape.radius, GOLD if flash else ORANGE)
102 pyxel.circb(x, y, shape.radius, WHITE if flash else GOLD)
103 pyxel.circ(x, y, shape.radius // 2, WHITE if flash else RED)
106def draw_segment(ram: Memory, shape: Segment) -> None:
107 if shape.kind == TARGET:
108 flash = ram[at.TARGET_FLASH + shape.index % at.TARGET_SLOTS]
109 lit = ram[at.LIT] >> shape.index & 1
110 aimed = shape.index == next_shield(ram[at.LIT]) and ram[at.FRAME] // BLINK % 2
111 color = WHITE if flash or aimed else GOLD if lit else RED if shape.index == next_shield(ram[at.LIT]) else STONE
112 thick(shape.start, shape.end, color)
113 middle = shape.middle()
114 name = "Lit shield" if lit else "Red shield" if shape.index == next_shield(ram[at.LIT]) else "Dark shield"
115 sprite(name, middle[0] - SHIELD_HALF, middle[1] - SHIELD_HALF)
116 return
117 if shape.kind == SLING:
118 thick(shape.start, shape.end, WHITE)
119 return
120 thick(shape.start, shape.end, MIST)
123def draw_slings(table: Table) -> None:
124 for shape in table.every_shape():
125 if isinstance(shape, Segment) and shape.kind == SLING:
126 top, bottom = shape.start, shape.end
127 pyxel.tri(top[0], top[1], bottom[0], bottom[1], top[0], bottom[1] - SLING_FOOT, RED)
130def draw_table(ram: Memory) -> None:
131 table = current_table(ram)
132 draw_walls_outside()
133 draw_backdrop(ram)
134 if table.ramp is not None:
135 draw_ramp(ram, table.ramp)
136 draw_slings(table)
137 for shape in table.every_shape():
138 if isinstance(shape, Circle):
139 draw_circle(ram, shape)
140 elif shape.kind != GATE:
141 draw_segment(ram, shape)