comparison tools/preview.py @ 1:5492efd60e2c

first prototype + updated art sheet
author ark
date 2026-10-18 14:50 +0200
parents
children 3dd5b745fa08
comparison
equal deleted inserted replaced
0:7af9dd4e2595 1:5492efd60e2c
1 """
2 preview images of the build, for checking routes, placement and tower levels
3 without starting the game.
4 """
5
6 from PIL import Image, ImageDraw
7
8 ROUTE_COLOURS = [(220, 0, 160), (0, 90, 255), (255, 120, 0), (0, 160, 60),
9 (140, 0, 255), (0, 180, 200), (200, 40, 40), (90, 90, 90)]
10
11
12 def _base(map_path, mp):
13 """map image faded towards white, with the placement grid drawn on it."""
14 im = Image.open(map_path).convert("RGBA")
15 im = Image.blend(im, Image.new("RGBA", im.size, (255, 255, 255, 255)), 0.55)
16 draw = ImageDraw.Draw(im)
17 cell, cols = mp["cell"], mp["cols"]
18 for i, c in enumerate(mp["grid"]):
19 if c == "1":
20 x, y = (i % cols) * cell, (i // cols) * cell
21 draw.rectangle((x, y, x + cell - 2, y + cell - 2), fill=(60, 170, 60, 255))
22 return im, draw
23
24
25 def _route(draw, pts, colour, width):
26 pts = [tuple(p) for p in pts]
27 draw.line(pts, fill=colour, width=width, joint="curve")
28 for x, y in pts:
29 draw.ellipse((x - 2, y - 2, x + 2, y + 2), fill=(0, 0, 0))
30 # arrow heads at a few points, showing the walking direction
31 for k in range(1, 6):
32 i = k * (len(pts) - 1) // 6
33 (x0, y0), (x1, y1) = pts[i], pts[i + 1]
34 dx, dy = x1 - x0, y1 - y0
35 n = (dx * dx + dy * dy) ** 0.5 or 1
36 dx, dy = dx / n, dy / n
37 tip = (x0 + dx * 6, y0 + dy * 6)
38 draw.polygon([tip, (x0 - dx * 6 - dy * 6, y0 - dy * 6 + dx * 6),
39 (x0 - dx * 6 + dy * 6, y0 - dy * 6 - dx * 6)], fill=colour)
40
41
42 def _ends(draw, mp):
43 for x, y, r in mp["entrances"]:
44 draw.ellipse((x - r, y - r, x + r, y + r), outline=(0, 0, 255), width=3)
45 for x, y, r in mp["exits"]:
46 draw.ellipse((x - r, y - r, x + r, y + r), outline=(0, 160, 0), width=3)
47
48
49 def map_preview(map_path, mp):
50 """the map with grid, centre line graph, routes, entrances and exits."""
51 im, draw = _base(map_path, mp)
52 for pts in mp["graph"]["edges"]:
53 draw.line([tuple(p) for p in pts], fill=(120, 120, 120), width=1)
54 for x, y in mp["graph"]["nodes"]:
55 draw.rectangle((x - 3, y - 3, x + 3, y + 3), outline=(0, 0, 0))
56 for k, r in enumerate(mp["routes"]):
57 _route(draw, r["points"], ROUTE_COLOURS[k % len(ROUTE_COLOURS)], 3)
58 _ends(draw, mp)
59 for k, r in enumerate(mp["routes"]):
60 x, y = r["points"][len(r["points"]) // 3]
61 draw.text((x + 6, y + 6 + 10 * k), f"route {r['number']}", fill=(0, 0, 0))
62 return im
63
64
65 def route_sheet(map_path, mp):
66 """one panel per route, half size, for maps with several routes."""
67 panels = []
68 for k, r in enumerate(mp["routes"]):
69 im, draw = _base(map_path, mp)
70 _route(draw, r["points"], ROUTE_COLOURS[k % len(ROUTE_COLOURS)], 5)
71 _ends(draw, mp)
72 draw.text((10, 10), f"route {r['number']}: length {r['length']:.0f},"
73 f" turning {r['turning']:.0f}, value {r['value']:.2f}", fill=(0, 0, 0))
74 panels.append(im.resize((im.width // 2, im.height // 2)))
75 cols = min(3, len(panels))
76 rows = (len(panels) + cols - 1) // cols
77 pw, ph = panels[0].size
78 sheet = Image.new("RGBA", (cols * pw, rows * ph), (255, 255, 255, 255))
79 for k, p in enumerate(panels):
80 sheet.paste(p, ((k % cols) * pw, (k // cols) * ph))
81 return sheet
82
83
84 def tower_sheet(assets):
85 """
86 tower levels 1-5 per row: at full size on grey, so the white fill shows,
87 and at game scale next to each other.
88 """
89 rows = []
90 for t in range(4):
91 names = [f"t{t}"] + [f"t{t}_{lv}" for lv in range(2, 6)]
92 rows.append([Image.open(assets / f"{n}.png") for n in names])
93 pad = 16
94 cell_w = max(im.width for r in rows for im in r) + pad
95 cell_h = max(im.height for r in rows for im in r) + pad
96 small_w = cell_w // 4 + 4
97 sheet = Image.new("RGBA", (5 * cell_w + 5 * small_w + pad, 4 * cell_h + 20),
98 (170, 170, 170, 255))
99 draw = ImageDraw.Draw(sheet)
100 for lv in range(5):
101 draw.text((lv * cell_w + pad, 4), f"level {lv + 1}", fill=(0, 0, 0))
102 draw.text((5 * cell_w + pad, 4), "game scale 0.25", fill=(0, 0, 0))
103 for t, r in enumerate(rows):
104 y = 20 + t * cell_h
105 for lv, im in enumerate(r):
106 x = lv * cell_w + pad // 2
107 # cross at the image centre, the default rotation point
108 sheet.alpha_composite(im, (x, y))
109 cx, cy = x + im.width // 2, y + im.height // 2
110 draw.line((cx - 5, cy, cx + 5, cy), fill=(255, 0, 0))
111 draw.line((cx, cy - 5, cx, cy + 5), fill=(255, 0, 0))
112 small = im.resize((max(1, im.width // 4), max(1, im.height // 4)), Image.LANCZOS)
113 sheet.alpha_composite(small, (5 * cell_w + pad + lv * small_w, y + cell_h // 3))
114 return sheet