HarvestForTwo / sprites.py
princessamygdala's picture
Scaffold task_pe-cu-nicewebrl: fork SL-task infra for prosocial effort task
a05d415
Raw History Blame Contribute Delete
29.8 kB
"""
Procedural sprite drawing for Duo Treasure Hunt (PIL, no asset files).
Draws characters, houses, chests, coins, keys, themed rooms and a welcome splash
given screen coordinates. `experiment_structure.py` composes these.
"""
import os
from PIL import Image, ImageDraw, ImageFont
def _darker(c, f=0.7):
return tuple(int(x * f) for x in c)
def _lighter(c, f=1.3):
return tuple(min(255, int(x * f)) for x in c)
# ---------------------------------------------------------------------------
# Fonts (DejaVu ships with matplotlib, which is already a dependency)
# ---------------------------------------------------------------------------
_FONT_CACHE = {}
def font(size, bold=False):
key = (int(size), bool(bold))
if key in _FONT_CACHE:
return _FONT_CACHE[key]
f = None
try:
import matplotlib
name = "DejaVuSans-Bold.ttf" if bold else "DejaVuSans.ttf"
f = ImageFont.truetype(os.path.join(matplotlib.get_data_path(), "fonts", "ttf", name), size)
except Exception:
f = ImageFont.load_default()
_FONT_CACHE[key] = f
return f
def text(d, x, y, s, size=18, bold=False, fill=(255, 255, 255), center=False, outline=False):
"""Draw text with the DejaVu font; center horizontally and/or add a dark outline."""
fnt = font(size, bold)
if center:
w = d.textlength(s, font=fnt)
x = x - w / 2
kw = {}
if outline:
kw = dict(stroke_width=max(2, size // 10), stroke_fill=(20, 20, 20))
d.text((x, y), s, fill=fill, font=fnt, **kw)
# ---------------------------------------------------------------------------
# Ground tiles + decorations
# ---------------------------------------------------------------------------
def _hash(wr, wc):
return ((wr * 73856093) ^ (wc * 19349663)) & 0xFFFFFFFF
def draw_grass_tile(d, x, y, t, wr, wc):
base = (74, 132, 68) if (wr + wc) % 2 == 0 else (68, 124, 62)
d.rectangle([x, y, x + t - 1, y + t - 1], fill=base)
h = _hash(wr, wc)
if h % 3 == 0:
gx, gy = x + 6 + (h % 7), y + 8 + (h % 11)
d.line([gx, gy, gx, gy - 5], fill=(60, 110, 55), width=1)
d.line([gx + 3, gy, gx + 3, gy - 4], fill=(60, 110, 55), width=1)
if h % 17 == 0:
draw_tree(d, x, y, t)
elif h % 11 == 0:
draw_bush(d, x, y, t)
elif h % 7 == 0:
draw_flower(d, x, y, t, h)
def draw_tree(d, x, y, t):
cx = x + t // 2
d.rectangle([cx - 3, y + t - 16, cx + 3, y + t - 4], fill=(96, 64, 36))
d.ellipse([x + 4, y + 2, x + t - 4, y + t - 10], fill=(46, 110, 50))
d.ellipse([x + 8, y + 6, x + t - 12, y + t - 16], fill=(60, 130, 64))
def draw_bush(d, x, y, t):
d.ellipse([x + 6, y + t - 18, x + t - 6, y + t - 4], fill=(54, 116, 56))
d.ellipse([x + 10, y + t - 20, x + t - 12, y + t - 10], fill=(64, 130, 66))
def draw_flower(d, x, y, t, h):
fx, fy = x + 8 + (h % 9), y + 10 + ((h >> 3) % 12)
colors = [(231, 76, 60), (241, 196, 15), (155, 89, 182), (236, 240, 241)]
col = colors[h % len(colors)]
d.line([fx, fy, fx, fy + 6], fill=(46, 110, 50), width=1)
for dx, dy in ((-2, 0), (2, 0), (0, -2), (0, 2)):
d.ellipse([fx + dx - 2, fy + dy - 2, fx + dx + 2, fy + dy + 2], fill=col)
d.ellipse([fx - 1, fy - 1, fx + 1, fy + 1], fill=(241, 196, 15))
# ---------------------------------------------------------------------------
# Characters: blocky humanoid, faces movement, 2-frame walk cycle, 3 styles
# style 0 = neutral (tuft), 1 = femme-coded (long hair + bow), 2 = masc-coded (cap + beard)
# ---------------------------------------------------------------------------
def draw_character(d, cx, cy, facing=0, frame=0,
shirt=(52, 120, 200), skin=(245, 205, 160),
hair=(70, 45, 25), pants=(58, 58, 92), mask=False, style=0,
accent=(236, 90, 150)):
"""cx, cy = feet (bottom-center). mask=True => anonymous masked partner."""
w = 22
x = cx - w // 2
top = cy - 36
head = 12
pants_d = _darker(pants, 0.8)
legtop = top + 26
if facing in (2, 3):
off = 3 if frame else -3
d.rectangle([x + 3 + off, legtop, x + 3 + off + 7, legtop + 10], fill=pants)
d.rectangle([x + w - 10 - off, legtop, x + w - 3 - off, legtop + 10], fill=pants_d)
else:
ly1 = legtop + (2 if frame else 0)
ly2 = legtop + (0 if frame else 2)
d.rectangle([x + 2, ly1, x + 9, ly1 + 10], fill=pants)
d.rectangle([x + w - 9, ly2, x + w - 2, ly2 + 10], fill=pants_d)
# femme style: a skirt over the top of the legs
if style == 1:
d.polygon([(x + 1, top + 24), (x + w - 1, top + 24), (x + w + 2, legtop + 4), (x - 2, legtop + 4)],
fill=_lighter(shirt, 1.05))
# arms + body
d.rectangle([x - 1, top + 13, x + 3, top + 26], fill=_darker(shirt, 0.85))
d.rectangle([x + w - 3, top + 13, x + w + 1, top + 26], fill=_darker(shirt, 0.85))
d.rectangle([x + 2, top + 13, x + w - 2, top + 27], fill=shirt)
# head
hx = x + (w - head) // 2
# femme: long hair behind/around head
if style == 1 and not mask:
d.rectangle([hx - 3, top + 1, hx, top + 24], fill=hair)
d.rectangle([hx + head, top + 1, hx + head + 3, top + 24], fill=hair)
d.rectangle([hx, top, hx + head, top + head], fill=skin)
d.rectangle([hx, top, hx + head, top + 4], fill=hair) # hair fringe
eye = (25, 25, 25)
if mask and facing != 1:
d.rectangle([hx - 1, top + 4, hx + head + 1, top + 9], fill=(40, 42, 55))
d.rectangle([hx + 2, top + 6, hx + 4, top + 8], fill=(225, 225, 235))
d.rectangle([hx + head - 4, top + 6, hx + head - 2, top + 8], fill=(225, 225, 235))
elif facing == 1:
d.rectangle([hx, top, hx + head, top + head - 2], fill=hair)
else:
if facing == 0:
d.rectangle([hx + 2, top + 6, hx + 4, top + 8], fill=eye)
d.rectangle([hx + head - 4, top + 6, hx + head - 2, top + 8], fill=eye)
elif facing == 2:
d.rectangle([hx + 1, top + 6, hx + 3, top + 8], fill=eye)
elif facing == 3:
d.rectangle([hx + head - 3, top + 6, hx + head - 1, top + 8], fill=eye)
# style extras (front/side only)
if not mask:
if style == 1: # bow on top
d.polygon([(hx + 1, top - 2), (hx + 5, top + 1), (hx + 1, top + 3)], fill=accent)
d.polygon([(hx + 9, top - 2), (hx + 5, top + 1), (hx + 9, top + 3)], fill=accent)
d.ellipse([hx + 4, top - 1, hx + 7, top + 2], fill=_darker(accent, 0.8))
elif style == 2: # cap brim + beard
d.rectangle([hx - 2, top - 1, hx + head + 2, top + 3], fill=_darker(shirt, 0.7))
d.rectangle([hx - 4, top + 2, hx + 2, top + 4], fill=_darker(shirt, 0.7))
d.rectangle([hx + 1, top + 9, hx + head - 1, top + head], fill=_darker(hair, 0.9))
else: # neutral tuft
d.rectangle([hx + head // 2 - 1, top - 3, hx + head // 2 + 1, top], fill=hair)
# 3 distinct explorers (the player picks one at the start)
AVATARS = [
dict(shirt=(46, 175, 150), skin=(238, 205, 170), hair=(95, 75, 55), pants=(60, 95, 95),
style=0), # 0 neutral, teal, tuft
dict(shirt=(214, 92, 150), skin=(245, 205, 178), hair=(80, 45, 95), pants=(160, 75, 130),
style=1, accent=(255, 220, 90)), # 1 femme-coded, pink, long hair + bow
dict(shirt=(70, 110, 205), skin=(224, 178, 138), hair=(55, 38, 26), pants=(58, 58, 88),
style=2), # 2 masc-coded, blue, cap + beard
]
PLAYER_PAL = AVATARS[0]
# the partner: unmasked, plain gender-neutral outfit (neutral, not flashy)
PARTNER_PAL = dict(shirt=(122, 150, 128), skin=(234, 200, 170), hair=(92, 76, 60),
pants=(96, 100, 116), style=0)
# ---------------------------------------------------------------------------
# Houses. htype 0..5 = main types; 6 = practice-only ("forest hut")
# ---------------------------------------------------------------------------
# 12 distinct main houses (0..11) + a practice-only one (12 = forest hut)
HOUSE_NAMES = ["castle", "cottage", "mansion", "treehouse", "tower", "barn",
"windmill", "igloo", "tent", "pagoda", "lighthouse", "log cabin", "forest hut"]
def draw_house(d, bx, by, t, htype, completed=False):
left, right = bx + 3, bx + t - 3
bottom = by + t - 3
top = by - 14
midx = (left + right) // 2
if completed:
d.rectangle([left - 5, top - 5, right + 5, bottom + 5], outline=(241, 196, 15), width=3)
if htype == 0: # castle
d.rectangle([left, top + 8, right, bottom], fill=(150, 150, 160))
for cx in range(left, right - 3, 9):
d.rectangle([cx, top + 2, cx + 5, top + 10], fill=(130, 130, 140))
d.rectangle([midx - 5, bottom - 14, midx + 5, bottom], fill=(70, 50, 40))
d.line([right - 4, top + 2, right - 4, top - 10], fill=(120, 120, 130), width=2)
d.polygon([(right - 4, top - 10), (right + 6, top - 7), (right - 4, top - 4)], fill=(220, 60, 60))
elif htype == 1: # cottage
d.rectangle([left + 2, top + 14, right - 2, bottom], fill=(245, 226, 190))
d.polygon([(left, top + 16), (midx, top), (right, top + 16)], fill=(200, 80, 60))
d.rectangle([midx - 5, bottom - 13, midx + 5, bottom], fill=(120, 80, 50))
d.rectangle([left + 6, top + 20, left + 14, top + 28], fill=(150, 210, 235))
d.ellipse([right - 14, bottom - 8, right - 8, bottom - 2], fill=(231, 76, 60))
elif htype == 2: # mansion
d.rectangle([left, top + 10, right, bottom], fill=(214, 198, 170))
d.rectangle([left, top + 4, right, top + 12], fill=(150, 60, 55))
for wx in (left + 4, midx - 4, right - 12):
d.rectangle([wx, top + 16, wx + 8, top + 24], fill=(150, 210, 235))
d.rectangle([wx, top + 28, wx + 8, top + 36], fill=(150, 210, 235))
d.rectangle([midx - 6, bottom - 16, midx + 6, bottom], fill=(110, 70, 45))
d.line([left + 3, top + 12, left + 3, bottom], fill=(240, 240, 235), width=2)
d.line([right - 3, top + 12, right - 3, bottom], fill=(240, 240, 235), width=2)
elif htype == 3: # treehouse
d.rectangle([midx - 4, top + 18, midx + 4, bottom], fill=(96, 64, 36))
d.ellipse([left - 2, top - 6, right + 2, top + 30], fill=(46, 120, 52))
d.rectangle([left + 6, top + 12, right - 6, top + 30], fill=(140, 95, 55))
d.polygon([(left + 4, top + 14), (midx, top + 2), (right - 4, top + 14)], fill=(120, 80, 45))
d.rectangle([midx - 3, top + 20, midx + 3, top + 30], fill=(70, 45, 25))
d.line([midx + 6, top + 30, midx + 6, bottom], fill=(110, 80, 50), width=2)
elif htype == 4: # tower
tl, tr = midx - 11, midx + 11
d.rectangle([tl, top + 6, tr, bottom], fill=(120, 110, 170))
d.polygon([(tl - 3, top + 8), (midx, top - 14), (tr + 3, top + 8)], fill=(95, 70, 160))
text(d, midx - 4, top - 12, "*", size=14, bold=True, fill=(241, 196, 15))
d.rectangle([midx - 4, top + 18, midx + 4, top + 28], fill=(150, 210, 235))
d.rectangle([midx - 5, bottom - 12, midx + 5, bottom], fill=(70, 55, 95))
elif htype == 5: # barn
d.rectangle([left, top + 14, right, bottom], fill=(178, 70, 58))
d.polygon([(left, top + 16), (left + 6, top + 4), (right - 6, top + 4), (right, top + 16)],
fill=(146, 52, 44))
d.rectangle([midx - 8, bottom - 18, midx + 8, bottom], fill=(120, 78, 50))
d.rectangle([midx - 8, bottom - 18, midx + 8, bottom], outline=(245, 240, 230), width=2)
d.line([midx, bottom - 18, midx, bottom], fill=(245, 240, 230), width=1)
d.rectangle([midx - 4, top + 6, midx + 4, top + 12], fill=(240, 220, 150))
elif htype == 6: # windmill
d.rectangle([midx - 12, top + 12, midx + 12, bottom], fill=(214, 206, 190))
d.polygon([(midx - 14, top + 14), (midx, top + 2), (midx + 14, top + 14)], fill=(150, 90, 60))
for ang in ((-10, -10, 10, 4), (10, -10, -4, 10)):
d.line([midx + ang[0], top + 4 + ang[1], midx + ang[2], top + 4 + ang[3]],
fill=(120, 80, 50), width=3)
d.line([midx - 12, top - 6, midx + 12, top + 14], fill=(120, 80, 50), width=3)
d.line([midx + 12, top - 6, midx - 12, top + 14], fill=(120, 80, 50), width=3)
d.rectangle([midx - 4, bottom - 12, midx + 4, bottom], fill=(90, 64, 44))
elif htype == 7: # igloo
d.ellipse([left, top + 6, right, bottom + 8], fill=(225, 232, 244))
d.rectangle([left, bottom - 4, right, bottom + 8], fill=(225, 232, 244))
for ix in range(left + 4, right - 2, 10):
d.line([ix, top + 8, ix, bottom], fill=(200, 210, 226), width=1)
d.ellipse([midx - 7, bottom - 12, midx + 7, bottom + 4], fill=(120, 150, 180))
elif htype == 8: # tent
d.polygon([(midx, top), (left, bottom), (right, bottom)], fill=(214, 120, 70))
d.polygon([(midx, top), (midx - 6, bottom), (midx + 6, bottom)], fill=(70, 50, 40))
d.line([midx, top, midx, top - 8], fill=(110, 80, 50), width=2)
d.polygon([(midx, top - 8), (midx + 9, top - 5), (midx, top - 2)], fill=(220, 60, 60))
elif htype == 9: # pagoda
for k, yy in enumerate((top + 6, top + 18, top + 30)):
inset = 2 + k * 3
d.polygon([(left + inset - 4, yy + 8), (midx, yy), (right - inset + 4, yy + 8)],
fill=(176, 70, 60))
d.rectangle([left + inset, yy + 8, right - inset, yy + 14], fill=(214, 200, 180))
d.rectangle([left + 8, top + 38, right - 8, bottom], fill=(190, 110, 80))
d.rectangle([midx - 4, bottom - 12, midx + 4, bottom], fill=(90, 50, 40))
elif htype == 10: # lighthouse
tl, tr = midx - 11, midx + 11
for k, yy in enumerate(range(top + 6, bottom, 12)):
c = (224, 80, 70) if k % 2 == 0 else (242, 242, 242)
d.rectangle([tl, yy, tr, yy + 12], fill=c)
d.rectangle([tl - 2, top + 2, tr + 2, top + 8], fill=(80, 80, 90))
d.ellipse([midx - 6, top - 8, midx + 6, top + 4], fill=(255, 230, 130))
elif htype == 11: # log cabin
d.rectangle([left, top + 12, right, bottom], fill=(150, 104, 62))
for yy in range(top + 16, bottom, 8):
d.line([left, yy, right, yy], fill=(120, 82, 48), width=2)
d.polygon([(left - 2, top + 14), (midx, top), (right + 2, top + 14)], fill=(110, 74, 44))
d.rectangle([right - 14, top - 2, right - 8, top + 12], fill=(90, 60, 40)) # chimney
d.rectangle([midx - 5, bottom - 14, midx + 5, bottom], fill=(70, 46, 28))
else: # 12 forest hut (practice only): mossy dome + toadstool
d.ellipse([left, top + 6, right, bottom + 6], fill=(96, 120, 70))
d.rectangle([left + 4, top + 22, right - 4, bottom], fill=(120, 90, 60))
d.rectangle([midx - 4, bottom - 12, midx + 4, bottom], fill=(70, 50, 35))
d.rectangle([right - 12, bottom - 9, right - 9, bottom], fill=(150, 110, 70))
d.ellipse([right - 16, bottom - 14, right - 5, bottom - 7], fill=(210, 70, 60))
d.ellipse([right - 14, bottom - 13, right - 12, bottom - 11], fill=(245, 245, 245))
if completed:
bx2, by2 = right - 2, top - 2
d.ellipse([bx2 - 8, by2 - 8, bx2 + 8, by2 + 8], fill=(39, 174, 96), outline=(255, 255, 255), width=2)
d.line([bx2 - 4, by2, bx2 - 1, by2 + 4], fill=(255, 255, 255), width=2)
d.line([bx2 - 1, by2 + 4, bx2 + 5, by2 - 4], fill=(255, 255, 255), width=2)
# ---------------------------------------------------------------------------
# Chests, coins, keys
# ---------------------------------------------------------------------------
def draw_chest(d, x, y, w, h, color, is_open=False, highlight=False):
body_top = y + h // 3
dark = _darker(color, 0.7)
if highlight:
d.rectangle([x - 6, y - 6, x + w + 6, y + h + 6], outline=(241, 196, 15), width=5)
d.rectangle([x, body_top, x + w, y + h], fill=color, outline=(40, 30, 20), width=3)
d.rectangle([x + w // 2 - 6, body_top + (h // 4), x + w // 2 + 6, y + h], fill=(218, 165, 32))
d.line([x, body_top + (h // 6), x + w, body_top + (h // 6)], fill=dark, width=3)
if not is_open:
d.rectangle([x, y + 4, x + w, body_top + 4], fill=_lighter(color, 1.1), outline=(40, 30, 20), width=3)
d.ellipse([x + w // 2 - 5, body_top - 4, x + w // 2 + 5, body_top + 6], fill=(218, 165, 32))
else:
d.rectangle([x, y - 12, x + w, y + 2], fill=dark, outline=(40, 30, 20), width=3)
d.rectangle([x + 4, body_top - 4, x + w - 4, body_top + 6], fill=(30, 24, 16))
d.rectangle([x + 6, body_top + 2, x + w - 6, body_top + 6], fill=(241, 196, 15))
# 26 genuinely distinct box stimuli (0..23 used in the main task, 24..25 for practice).
# Each = a unique combination of colour x material x lid-shape x pattern. The look is an
# arbitrary, randomised label.
STIM_PALETTE = [
(214, 70, 64), (52, 130, 214), (54, 168, 96), (232, 150, 40), (150, 90, 196),
(40, 176, 168), (224, 110, 150), (130, 150, 64), (184, 120, 72), (90, 110, 206),
(206, 184, 64), (152, 152, 160), (108, 184, 214),
] # 13 distinct hues
MATERIALS = ["plain", "wood", "stone", "metal", "woven", "dots", "stripes", "marble"]
LIDS = ["flat", "arched", "peaked"]
N_STIM = 26
# human-readable hue names, aligned 1:1 with STIM_PALETTE above
COLOR_NAMES = ["red", "blue", "green", "orange", "purple", "teal", "pink",
"olive", "brown", "indigo", "yellow", "grey", "sky-blue"]
def stim_color(sid):
return STIM_PALETTE[int(sid) % len(STIM_PALETTE)]
def stim_describe(sid):
"""A verbal label for a chest stimulus, e.g. 'green arched woven' — matches stim_design."""
sid = int(sid)
color = COLOR_NAMES[sid % len(STIM_PALETTE)]
mat = MATERIALS[sid % len(MATERIALS)]
lid = LIDS[(sid // 2) % len(LIDS)]
return f"{color} {lid}-lid {mat}".replace(" plain", "").strip()
def stim_design(sid):
sid = int(sid)
return (STIM_PALETTE[sid % len(STIM_PALETTE)],
MATERIALS[sid % len(MATERIALS)],
LIDS[(sid // 2) % len(LIDS)])
def _fill_material(d, x0, y0, x1, y1, color, mat, accent):
d.rectangle([x0, y0, x1, y1], fill=color)
if mat == "wood":
for yy in range(y0 + 5, y1, 9):
d.line([x0, yy, x1, yy], fill=_darker(color, 0.82), width=2)
elif mat == "stone":
dk = _darker(color, 0.78)
for k, yy in enumerate(range(y0, y1, 12)):
d.line([x0, yy, x1, yy], fill=dk, width=1)
off = 0 if k % 2 == 0 else (x1 - x0) // 4
for xx in range(x0 + off, x1, max(8, (x1 - x0) // 2)):
d.line([xx, yy, xx, min(yy + 12, y1)], fill=dk, width=1)
elif mat == "metal":
d.rectangle([(x0 + x1) // 2 - 4, y0, (x0 + x1) // 2 + 4, y1], fill=_lighter(color, 1.3))
for ry in (y0 + 4, y1 - 7):
for rx in range(x0 + 7, x1 - 4, 16):
d.ellipse([rx, ry, rx + 4, ry + 4], fill=_darker(color, 0.65))
elif mat == "woven":
for yy in range(y0 + 4, y1, 8):
d.line([x0, yy, x1, yy], fill=_lighter(color, 1.16), width=2)
for xx in range(x0 + 4, x1, 8):
d.line([xx, y0, xx, y1], fill=_darker(color, 0.85), width=1)
elif mat == "dots":
for yy in range(y0 + 9, y1, 15):
for xx in range(x0 + 9, x1, 17):
d.ellipse([xx - 3, yy - 3, xx + 3, yy + 3], fill=accent)
elif mat == "stripes":
for xx in range(x0 + 5, x1, 17):
d.rectangle([xx, y0, xx + 6, y1], fill=accent)
elif mat == "marble":
for k in range(3):
yy = y0 + 7 + k * max(6, (y1 - y0) // 3)
d.line([(x0 + 4, yy), ((x0 + x1) // 2, yy - 6), (x1 - 4, yy + 4)],
fill=_lighter(color, 1.28), width=2)
def draw_stim_chest(d, x, y, w, h, stim_id, is_open=False, highlight=False):
"""One of 26 distinct chests (colour x material x lid-shape x pattern)."""
color, mat, lid = stim_design(stim_id)
accent = _lighter(color, 1.45) if sum(color) < 410 else _darker(color, 0.6)
body_top = y + h // 3
dark = _darker(color, 0.7)
if highlight:
d.rectangle([x - 6, y - 6, x + w + 6, y + h + 6], outline=(241, 196, 15), width=5)
_fill_material(d, x, body_top, x + w, y + h, color, mat, accent)
d.rectangle([x, body_top, x + w, y + h], outline=(40, 30, 20), width=3)
d.rectangle([x + w // 2 - 6, body_top + h // 5, x + w // 2 + 6, y + h], fill=(218, 165, 32))
if not is_open:
if lid == "arched":
d.pieslice([x, y - 6, x + w, body_top + 12], 180, 360,
fill=_lighter(color, 1.08), outline=(40, 30, 20), width=3)
elif lid == "peaked":
d.polygon([(x, body_top + 4), (x + w // 2, y - 6), (x + w, body_top + 4)],
fill=_lighter(color, 1.08), outline=(40, 30, 20))
d.line([x, body_top + 4, x + w, body_top + 4], fill=(40, 30, 20), width=3)
else: # flat
_fill_material(d, x, y + 4, x + w, body_top + 4, _lighter(color, 1.08), mat, accent)
d.rectangle([x, y + 4, x + w, body_top + 4], outline=(40, 30, 20), width=3)
d.ellipse([x + w // 2 - 5, body_top - 4, x + w // 2 + 5, body_top + 6], fill=(218, 165, 32))
else:
d.rectangle([x, y - 12, x + w, y + 2], fill=dark, outline=(40, 30, 20), width=3)
d.rectangle([x + 4, body_top - 4, x + w - 4, body_top + 6], fill=(30, 24, 16))
d.rectangle([x + 6, body_top + 2, x + w - 6, body_top + 6], fill=(241, 196, 15))
def draw_coin(d, cx, cy, value, r=22):
"""value is the coin amount (e.g. +100 / -100 / 0). Drawn 'floating'."""
label = f"+{value}" if value > 0 else (str(value) if value < 0 else "0")
if value > 0:
d.ellipse([cx - r, cy - r, cx + r, cy + r], fill=(241, 196, 15), outline=(191, 150, 10), width=3)
tcol = (120, 90, 10)
elif value < 0:
d.ellipse([cx - r, cy - r, cx + r, cy + r], fill=(231, 76, 60), outline=(150, 40, 30), width=3)
tcol = (255, 235, 235)
else: # 0: pale coin with a dark outline (readable on any bg)
d.ellipse([cx - r, cy - r, cx + r, cy + r], fill=(228, 228, 232),
outline=(20, 20, 24), width=3)
tcol = (20, 20, 24)
text(d, cx, cy - 9, label, size=17, bold=True, fill=tcol, center=True, outline=True)
def draw_key(d, x, y, color):
d.ellipse([x, y, x + 10, y + 10], outline=color, width=3)
d.line([x + 10, y + 5, x + 24, y + 5], fill=color, width=3)
d.line([x + 20, y + 5, x + 20, y + 10], fill=color, width=3)
d.line([x + 24, y + 5, x + 24, y + 11], fill=color, width=3)
# ---------------------------------------------------------------------------
# Themed indoor rooms (interior matches the house type)
# ---------------------------------------------------------------------------
# per house type: wall, seam, floor, floor_line, accent(torch/decor), window=True/False
ROOM_THEMES = [
dict(wall=(120, 122, 132), seam=(104, 106, 116), floor=(96, 96, 104), line=(80, 80, 88), accent=(240, 160, 50)), # 0 castle
dict(wall=(214, 196, 162), seam=(198, 180, 148), floor=(160, 116, 74), line=(138, 98, 60), accent=(231, 120, 90)), # 1 cottage
dict(wall=(196, 178, 150), seam=(176, 158, 132), floor=(150, 120, 92), line=(120, 94, 70), accent=(214, 180, 90)), # 2 mansion
dict(wall=(96, 120, 78), seam=(82, 104, 66), floor=(140, 100, 60), line=(116, 82, 48), accent=(90, 170, 90)), # 3 treehouse
dict(wall=(96, 80, 140), seam=(82, 68, 124), floor=(74, 60, 110), line=(60, 48, 92), accent=(255, 220, 90)), # 4 tower
dict(wall=(168, 84, 72), seam=(150, 70, 60), floor=(176, 150, 96), line=(150, 124, 78), accent=(240, 210, 120)), # 5 barn
dict(wall=(206, 198, 178), seam=(188, 180, 160), floor=(150, 128, 92), line=(124, 104, 72), accent=(200, 150, 90)), # 6 windmill
dict(wall=(188, 208, 224), seam=(168, 190, 210), floor=(176, 196, 214), line=(150, 172, 194), accent=(120, 170, 210)), # 7 igloo
dict(wall=(196, 130, 96), seam=(176, 112, 82), floor=(150, 110, 76), line=(126, 90, 60), accent=(232, 150, 90)), # 8 tent
dict(wall=(150, 96, 92), seam=(132, 82, 78), floor=(140, 104, 80), line=(116, 84, 62), accent=(214, 120, 100)), # 9 pagoda
dict(wall=(120, 140, 156), seam=(102, 122, 138), floor=(132, 138, 146), line=(108, 114, 122), accent=(255, 220, 130)), # 10 lighthouse
dict(wall=(140, 104, 68), seam=(120, 86, 54), floor=(150, 110, 70), line=(124, 88, 54), accent=(220, 160, 90)), # 11 log cabin
dict(wall=(110, 132, 92), seam=(94, 116, 78), floor=(132, 100, 64), line=(108, 80, 50), accent=(210, 90, 80)), # 12 forest hut (practice)
]
def draw_room(d, W, H, floor_y, table_x0, table_x1, table_top, theme=None):
th = theme or ROOM_THEMES[1]
d.rectangle([0, 0, W, floor_y], fill=th["wall"])
for x in range(0, W, 64):
d.line([x, 0, x, floor_y], fill=th["seam"], width=1)
d.line([0, floor_y, W, floor_y], fill=_darker(th["wall"], 0.7), width=3)
# window with sky
wx0, wy0, wx1, wy1 = 60, 78, 196, 184
d.rectangle([wx0, wy0, wx1, wy1], fill=(120, 186, 222))
d.rectangle([wx0 + 6, wy0 + 6, (wx0 + wx1) // 2 - 2, wy1 - 6], fill=(150, 205, 235))
d.rectangle([(wx0 + wx1) // 2 + 2, wy0 + 6, wx1 - 6, wy1 - 6], fill=(150, 205, 235))
d.ellipse([wx1 - 42, wy0 + 12, wx1 - 22, wy0 + 32], fill=(245, 230, 150))
d.rectangle([wx0 - 4, wy0 - 4, wx1 + 4, wy1 + 4], outline=_darker(th["wall"], 0.6), width=4)
# accent torch/sconce on the right wall
tx = W - 130
d.rectangle([tx, 104, tx + 8, 136], fill=_darker(th["wall"], 0.6))
d.ellipse([tx - 7, 84, tx + 15, 108], fill=th["accent"])
d.ellipse([tx - 3, 86, tx + 11, 102], fill=_lighter(th["accent"], 1.2))
# floor
d.rectangle([0, floor_y, W, H], fill=th["floor"])
for y in range(floor_y + 18, H, 26):
d.line([0, y, W, y], fill=th["line"], width=2)
# table
d.rectangle([table_x0, table_top, table_x1, table_top + 26], fill=(120, 82, 48),
outline=(80, 54, 32), width=3)
d.rectangle([table_x0 + 14, table_top + 26, table_x0 + 34, floor_y + 40], fill=(104, 70, 40))
d.rectangle([table_x1 - 34, table_top + 26, table_x1 - 14, floor_y + 40], fill=(104, 70, 40))
# ---------------------------------------------------------------------------
# Avatar preview + welcome splash
# ---------------------------------------------------------------------------
def draw_avatar_preview(idx, size=130):
img = Image.new("RGB", (size, size), (54, 60, 78))
d = ImageDraw.Draw(img)
tmp = Image.new("RGB", (size // 2, size // 2), (54, 60, 78))
dt = ImageDraw.Draw(tmp)
draw_character(dt, size // 4, size // 2 - 4, facing=0, frame=0, **AVATARS[idx])
img.paste(tmp.resize((size, size), Image.NEAREST), (0, 0))
return img
def draw_welcome_splash(W, H):
img = Image.new("RGB", (W, H), (120, 180, 224))
d = ImageDraw.Draw(img)
# sky gradient
for i in range(H):
f = i / H
d.line([0, i, W, i], fill=(int(120 + 60 * f), int(180 + 30 * f), int(224 - 20 * f)))
# fluffy clouds (overlapping circles)
for cx, cy in ((110, 84), (545, 120), (325, 58)):
for dx, dy, r in ((0, 6, 15), (20, 0, 20), (42, 6, 15), (22, 13, 17)):
d.ellipse([cx + dx - r, cy + dy - r, cx + dx + r, cy + dy + r], fill=(248, 250, 253))
# ground
gy = H - 150
d.rectangle([0, gy, W, H], fill=(86, 154, 74))
d.rectangle([0, gy, W, gy + 10], fill=(104, 176, 88))
for x in range(0, W, 40):
d.rectangle([x, gy + 30, x + 38, H], outline=(76, 138, 66), width=1)
# a couple of little houses + trees on the horizon
draw_house(d, 70, gy - 40, 48, 1)
draw_house(d, W - 150, gy - 40, 48, 4)
draw_tree(d, 200, gy - 40, 48)
draw_tree(d, W - 240, gy - 40, 48)
# two explorers walking
draw_character(d, W // 2 - 22, gy + 64, facing=0, frame=0, **AVATARS[0])
draw_character(d, W // 2 + 22, gy + 64, facing=0, frame=1, **PARTNER_PAL)
# a big treasure chest
draw_chest(d, W // 2 - 34, gy - 8, 68, 48, (230, 170, 40))
# title
text(d, W // 2, 160, "Duo Treasure Hunt", size=46, bold=True, fill=(255, 238, 160),
center=True, outline=True)
return img
def draw_end_splash(W, H):
"""A celebratory outro scene (sunset)."""
img = Image.new("RGB", (W, H), (250, 180, 120))
d = ImageDraw.Draw(img)
for i in range(H):
f = i / H
d.line([0, i, W, i], fill=(int(250 - 20 * f), int(150 + 40 * f), int(110 + 80 * f)))
# sun low on the horizon
gy = H - 150
d.ellipse([W // 2 - 50, gy - 70, W // 2 + 50, gy + 30], fill=(255, 226, 150))
# ground
d.rectangle([0, gy, W, H], fill=(86, 140, 74))
d.rectangle([0, gy, W, gy + 10], fill=(104, 164, 88))
draw_tree(d, 120, gy - 40, 48)
draw_tree(d, W - 168, gy - 40, 48)
# two explorers + a big open treasure chest of coins between them
draw_character(d, W // 2 - 70, gy + 60, facing=3, frame=0, **AVATARS[0])
draw_character(d, W // 2 + 70, gy + 60, facing=2, frame=0, **PARTNER_PAL)
draw_chest(d, W // 2 - 36, gy - 16, 72, 52, (230, 170, 40), is_open=True)
for k, (cx, cy) in enumerate([(W // 2 - 18, gy - 30), (W // 2, gy - 40), (W // 2 + 18, gy - 30)]):
d.ellipse([cx - 9, cy - 9, cx + 9, cy + 9], fill=(241, 196, 15), outline=(191, 150, 10), width=2)
text(d, W // 2, 60, "Adventure Complete!", size=42, bold=True, fill=(255, 244, 180),
center=True, outline=True)
return img