import pyxel
import random
import math

# =============================================================================
# GAME STATES
# =============================================================================
STATE_INTRO      = 6
STATE_HAUS       = 0
STATE_GARTEN     = 1
STATE_STRASSE    = 2
STATE_BAUM       = 3
STATE_GAMEOVER   = 4
STATE_WIN        = 5
STATE_HUND_INTRO = 7

# =============================================================================
# PLAYER
# =============================================================================
player_x  = 32
player_y  = 32
player_w  = 8
player_h  = 16
speed     = 2
offset_x  = 4
offset_y  = 0
player_facing = 1
player_moving = False

# =============================================================================
# GLOBAL FLAGS
# =============================================================================
state            = STATE_INTRO
hinweis_gefunden = False
hat_katzenfutter = False

# =============================================================================
# INTRO STATE
# =============================================================================
intro_page          = 0
intro_timer         = 0
INTRO_PAGES         = 2
intro_cat_x         = -10.0
intro_jingle_played = False

visited_cat_no_futter = False
current_music         = -1

# =============================================================================
# HUND INTRO
# =============================================================================
hund_intro_timer = 0

# =============================================================================
# LEVEL 1 - HAUS
# =============================================================================
korb_x   = 200
korb_y   = 100
futter_x = 75
futter_y = 50

extra_items = [
    (140, 15, "Fenster", 0),
    (275, 19, "Sofa",    1),
    (280,90, "Napf",    2),
]
extra_found = [False, False, False]
extra_texts = [
    "Draussen: nichts.",
    "Blutspuren am Sofa.",
    "Der Napf ist noch voll!",
]

walls_haus = [
    (0,   0,   552,  8),
    (0,   120, 552,  8),
    (0,   4,   4,    128),
    (116, 0,   16,   80),
    (116, 104, 16,   16),
    (552, 0,   16,   80),
    (0,   0,   110,  20),
    (8,   4,   16,   64),
    (8,   48,  74,   20),
    (376,  0,  10,  56),
    (376,  88,  10,  48),
    (232,  94,  32,  48)
]

# FIX 5: Extra wall that blocks the right exit until hinweis is found.
WALL_EXIT_BLOCK = (490, 0, 8, 128)

# =============================================================================
# LEVEL 2 - GARTEN
# =============================================================================
hund_x     = 120.0
hund_y     = 80.0
hund_speed = 2.2

walls_garten = [
    (0,   0,   4,   256),
    (0,   0,   256, 4),
    (0,   252, 256, 4),
    (252, 0,   4,   256),
    (96,  128,  32,  16),
    (176,  200,  48,  16),
    (96,  72,  48,  16),
]

# =============================================================================
# LEVEL 3 - STRASSE
# =============================================================================
CAR_SPRITES  = [(0, 48), (0, 64), (0, 80)]
CAR_SPRITE_W = 16
CAR_SPRITE_H = 16
CAR_HIT_W    = 12
CAR_HIT_H    = 10

WORLD_H      = 512
WORLD_W      = 128
SCREEN_H     = 128
GOAL_WORLD_Y = 110

LANE_DEFS = [
    (476, +1, 0.6), (460, -1, 0.7),
    (428, +1, 1.0), (412, -1, 1.1),
    (380, -1, 1.4), (364, +1, 1.5),
    (332, +1, 1.9), (316, -1, 2.0),
    (284, -1, 2.3), (268, +1, 2.5),
]

cars             = []
player_world_y   = 490
CARS_PER_LANE    = 2
strasse_from_top = False
garten_from_top  = False

walls_strasse = [
    (0, 248, 40, 64), 
    (80, 248, 40, 64),
]
# =============================================================================
# LEVEL 4 - BAUM
# =============================================================================
platforms = [
    (10, 108, 44, 4),
    (76,  90, 42, 4),
    (14,  72, 40, 4),
    (76,  54, 42, 4),
    (12,  36, 40, 4),
    (74,  18, 36, 4),
]
CAT_X      = 86
CAT_Y      = 10
player_vy  = 0.0
on_ground  = False
GRAVITY    = 0.4
JUMP_FORCE = -4.5

leaves      = []
leaves_init = False
bird_x      = 0.0
bird_y      = 40.0
bird_dir    = 1
bird_flap   = 0
baum_nofutter_timer = 0

# =============================================================================
# SOUNDS
# =============================================================================
SOUNDS_OK = False

def setup_sounds():
    global SOUNDS_OK
    try:
        pyxel.sound(0).set("c3 e3 g3 c4",               "SSSS",     "4321",     "NNNN",     10)
        pyxel.sound(1).set("g2 e2 c2 a1",               "SSSS",     "4444",     "NNNN",      7)
        pyxel.sound(2).set("c3 e3 g3 a3 c4 a3 g3 c4",  "SSSSSSSS", "44444444", "NNNNNNNN",  8)
        pyxel.sound(3).set("g1 f1 g1",                  "NNN",      "432",      "NNN",        4)
        pyxel.sound(4).set("a2 g2 f2 e2 d2",            "NNNNN",    "54321",    "NNNNN",      5)
        pyxel.sound(5).set("c4 g3",                     "SS",       "43",       "NS",         8)
        pyxel.sound(6).set("c2",                        "N",        "4",        "N",          4)
        pyxel.sound(7).set("c3 e3 g3 c4 g3 e3 c3",     "SSSSSSS",  "3456543",  "NNNNNNN",   14)
        pyxel.sound(8).set(
            "c3 e3 g3 a3 g3 e3 c3 e3 d3 f3 a3 c4 a3 f3 d3 r",
            "TTTTTTTTTTTTTTTT", "3344554433445500", "NNNNNNNNNNNNNNNN", 24)
        pyxel.sound(9).set(
            "g3 b3 c4 b3 a3 g3 e3 g3 f3 a3 c4 a3 g3 f3 e3 r",
            "TTTTTTTTTTTTTTTT", "3344554433445500", "NNNNNNNNNNNNNNNN", 24)
        pyxel.sound(10).set(
            "c3 d3 e3 g3 e3 d3 c3 d3 e3 g3 a3 g3 e3 d3 c3 r",
            "TTTTTTTTTTTTTTTT", "3344554433445500", "NNNNNNNNNNNNNNNN", 24)
        pyxel.sound(11).set(
            "a3 g3 e3 c3 e3 g3 a3 g3 f3 e3 d3 e3 f3 g3 e3 r",
            "TTTTTTTTTTTTTTTT", "5544332233445500", "NNNNNNNNNNNNNNNN", 24)
        pyxel.sound(12).set(
            "c2 r g2 r c2 r e2 r g2 r c2 r g2 r e2 r",
            "TTTTTTTTTTTTTTTT", "2020202020202020", "NNNNNNNNNNNNNNNN", 24)
        pyxel.sound(13).set(
            "f2 r c2 r f2 r a2 r c3 r g2 r c2 r e2 r",
            "TTTTTTTTTTTTTTTT", "2020202020202020", "NNNNNNNNNNNNNNNN", 24)
        pyxel.sound(14).set(
            "e2 f2 e2 d2 c2 d2 e2 f2 g2 f2 e2 d2 c2 d2 e2 r",
            "PPPPPPPPPPPPPPPP", "4444444444444444", "NNNNNNNNNNNNNNNN", 10)
        pyxel.sound(15).set(
            "g2 a2 g2 f2 e2 f2 g2 a2 b2 a2 g2 f2 e2 f2 g2 r",
            "PPPPPPPPPPPPPPPP", "4444444444444444", "NNNNNNNNNNNNNNNN", 10)
        pyxel.sound(16).set(
            "c3 b2 a2 g2 f2 e2 d2 c2 d2 e2 f2 g2 a2 b2 c3 r",
            "PPPPPPPPPPPPPPPP", "4444444444444444", "NNNNNNNNNNNNNNNN", 10)
        pyxel.sound(17).set(
            "c1 r c1 c1 g1 r g1 r c1 r c1 c1 g1 r g1 r",
            "NNNNNNNNNNNNNNNN", "4343434343434343", "NNNNNNNNNNNNNNNN", 10)
        pyxel.sound(18).set(
            "f1 r f1 f1 c1 r c1 r g1 r g1 g1 f1 r f1 r",
            "NNNNNNNNNNNNNNNN", "4343434343434343", "NNNNNNNNNNNNNNNN", 10)
        pyxel.music(0).set([], [], [8, 9, 10, 11], [12, 13, 12, 13])
        pyxel.music(1).set([], [], [14, 15, 16, 14], [17, 18, 17, 18])
        SOUNDS_OK = True
    except Exception:
        SOUNDS_OK = False

def play_sound(slot, channel=0):
    if SOUNDS_OK:
        try: pyxel.play(channel, slot)
        except Exception: pass

def set_music(track_id):
    global current_music
    if current_music == track_id:
        return
    current_music = track_id
    if not SOUNDS_OK:
        return
    try:
        if track_id == -1:
            pyxel.stop(2); pyxel.stop(3)
        else:
            pyxel.playm(track_id, loop=True)
    except Exception:
        pass


# =============================================================================
# SHARED DRAW HELPERS
# =============================================================================

def shadow_text(x, y, text, col, shadow_col=0):
    pyxel.text(x+1, y+1, text, shadow_col)
    pyxel.text(x,   y,   text, col)

def draw_mollie(x, y):
    pyxel.rect(x+1, y+3, 6, 5, 8)
    pyxel.rect(x+1, y+1, 6, 4, 8)
    pyxel.pset(x+1, y,   8); pyxel.pset(x+6, y, 8)
    pyxel.pset(x+2, y+2, 7); pyxel.pset(x+5, y+2, 7)
    pyxel.pset(x+3, y+3, 14)
    pyxel.line(x+7, y+5, x+9, y+3, 8)
    pyxel.pset(x+9, y+2, 8)
    pyxel.pset(x+2, y+5, 2); pyxel.pset(x+4, y+5, 2); pyxel.pset(x+6, y+5, 2)

def draw_key_icon(x, y, label, desc):
    w = len(label)*4+2
    pyxel.rect(x, y, w, 7, 5); pyxel.rectb(x, y, w, 7, 7)
    pyxel.text(x+1, y+1, label, 7)
    pyxel.text(x+w+3, y+1, desc, 6)

def draw_hund(x, y):
    xi, yi = int(x), int(y)
    f = (pyxel.frame_count // 7) % 4
    BODY=9; DARK=4; BELLY=15; BLACK=0
    tw = -2 if 2 > f else 1
    pyxel.pset(xi,    yi+5+tw, DARK)
    pyxel.pset(xi-1,  yi+4+tw, DARK)
    pyxel.pset(xi-2,  yi+3+tw, BODY)
    pyxel.rect(xi+1,  yi+5, 9, 6, BODY)
    pyxel.rect(xi+1,  yi+5, 9, 1, DARK)
    pyxel.rect(xi+3,  yi+8, 4, 2, BELLY)
    pyxel.rect(xi+8,  yi+4, 3, 3, BODY)
    pyxel.rect(xi+9,  yi+1, 6, 6, BODY)
    pyxel.rect(xi+9,  yi+1, 2, 4, DARK)
    pyxel.pset(xi+10, yi+3, BELLY)
    pyxel.rect(xi+12, yi+4, 3, 3, BELLY)
    pyxel.rect(xi+13, yi+4, 2, 1, BLACK)
    pyxel.pset(xi+14, yi+5, BLACK)
    pyxel.pset(xi+11, yi+2, BLACK)
    pyxel.pset(xi+12, yi+6, DARK)
    if f == 0:
        pyxel.rect(xi+2, yi+11, 2, 5, DARK); pyxel.rect(xi+5, yi+11, 2, 5, DARK)
        pyxel.rect(xi+7, yi+11, 2, 5, BODY); pyxel.rect(xi+9, yi+11, 2, 5, BODY)
    elif f == 1:
        pyxel.rect(xi+1, yi+11, 2, 4, DARK); pyxel.pset(xi+1, yi+14, DARK)
        pyxel.rect(xi+6, yi+11, 2, 5, DARK)
        pyxel.rect(xi+8, yi+11, 2, 5, BODY); pyxel.rect(xi+9, yi+10, 2, 5, BODY)
    elif f == 2:
        pyxel.rect(xi+2, yi+11, 2, 5, DARK); pyxel.rect(xi+5, yi+11, 2, 5, DARK)
        pyxel.rect(xi+7, yi+11, 2, 5, BODY); pyxel.rect(xi+9, yi+11, 2, 5, BODY)
    else:
        pyxel.rect(xi+3, yi+11, 2, 4, DARK); pyxel.rect(xi+4, yi+11, 2, 5, DARK)
        pyxel.rect(xi+6, yi+10, 2, 5, BODY); pyxel.rect(xi+9, yi+11, 2, 5, BODY)

def draw_big_dog(ox, oy, frame=0):
    f = frame % 4
    BODY=9; DARK=4; BELLY=15; BLACK=0; WHITE=7; COLLAR=8
    tw = -3 if 2 > f else 2
    for i in range(3):
        pyxel.pset(ox-i, oy+8+tw-i, DARK if 2 > i else BODY)
    pyxel.rect(ox+1,  oy+9,  18, 10, BODY)
    pyxel.rect(ox+1,  oy+9,  18,  2, DARK)
    pyxel.rect(ox+5,  oy+14,  8,  3, BELLY)
    pyxel.rect(ox+15, oy+7,  5,  5, BODY)
    pyxel.rect(ox+17, oy+2, 11, 10, BODY)
    pyxel.rect(ox+17, oy+2,  3,  7, DARK)
    pyxel.pset(ox+18, oy+5, BELLY)
    pyxel.rect(ox+23, oy+7,  5,  5, BELLY)
    pyxel.rect(ox+25, oy+7,  3,  2, BLACK)
    pyxel.pset(ox+27, oy+9, BLACK)
    pyxel.rect(ox+20, oy+4,  2,  2, WHITE)
    pyxel.pset(ox+21, oy+4, BLACK)
    pyxel.pset(ox+20, oy+5, BLACK)
    pyxel.line(ox+19, oy+3, ox+21, oy+2, DARK)
    pyxel.pset(ox+24, oy+11, DARK)
    pyxel.pset(ox+25, oy+11, DARK)
    pyxel.rect(ox+16, oy+8,  4,  2, COLLAR)
    pyxel.pset(ox+17, oy+9, 10)
    if f in (0,2):
        pyxel.rect(ox+3,  oy+19, 3, 8, DARK); pyxel.rect(ox+9,  oy+19, 3, 8, DARK)
    elif f == 1:
        pyxel.rect(ox+2,  oy+19, 3, 7, DARK); pyxel.rect(ox+8,  oy+18, 3, 7, DARK)
        pyxel.rect(ox+8,  oy+24, 4, 2, DARK)
    else:
        pyxel.rect(ox+4,  oy+19, 3, 8, DARK); pyxel.rect(ox+10, oy+19, 3, 8, DARK)
    if f in (0,2):
        pyxel.rect(ox+13, oy+19, 3, 8, BODY); pyxel.rect(ox+18, oy+19, 3, 8, BODY)
    elif f == 1:
        pyxel.rect(ox+14, oy+19, 3, 8, BODY); pyxel.rect(ox+18, oy+17, 3, 7, BODY)
        pyxel.rect(ox+18, oy+23, 4, 2, BODY)
    else:
        pyxel.rect(ox+12, oy+18, 3, 8, BODY); pyxel.rect(ox+18, oy+19, 3, 8, BODY)

def draw_big_mollie(x, y):
    BODY=8; STRIPE=2; EYE=7; NOSE=14; TAIL=8
    pyxel.rect(x+2,  y+5,  10, 8,  BODY)
    pyxel.rect(x+2,  y+1,  10, 6,  BODY)
    pyxel.pset(x+2, y,   BODY); pyxel.pset(x+3, y,   BODY)
    pyxel.pset(x+9, y,   BODY); pyxel.pset(x+10, y,  BODY)
    pyxel.pset(x+2, y-1, BODY); pyxel.pset(x+10, y-1, BODY)
    pyxel.rect(x+3, y+2, 2, 2, EYE); pyxel.pset(x+4, y+2, 0)
    pyxel.rect(x+8, y+2, 2, 2, EYE); pyxel.pset(x+9, y+2, 0)
    pyxel.pset(x+6, y+4, NOSE)
    pyxel.pset(x+5, y+5, STRIPE); pyxel.pset(x+7, y+5, STRIPE)
    pyxel.line(x+3, y+7, x+3, y+9, STRIPE)
    pyxel.line(x+6, y+7, x+6, y+9, STRIPE)
    pyxel.line(x+9, y+7, x+9, y+9, STRIPE)
    pyxel.line(x+12, y+9, x+15, y+6, TAIL)
    pyxel.pset(x+15, y+5, TAIL)

def draw_house(ox, oy, w=32, h=24, wall_col=5, roof_col=2, door_col=4, win_col=10):
    pyxel.rect(ox, oy+8, w, h, wall_col)
    for i in range(w//2+1):
        pyxel.line(ox+i, oy+8-i, ox+w-i, oy+8-i, roof_col)
    dw=6; dh=9
    pyxel.rect(ox+w//2-dw//2, oy+h+8-dh, dw, dh, door_col)
    pyxel.pset(ox+w//2-dw//2+dw-1, oy+h+8-dh//2, 7)
    pyxel.rect(ox+3,   oy+12, 6, 5, win_col)
    pyxel.pset(ox+6,   oy+12, 12); pyxel.line(ox+3, oy+14, ox+8, oy+14, 12)
    pyxel.rect(ox+w-9, oy+12, 6, 5, win_col)
    pyxel.pset(ox+w-6, oy+12, 12); pyxel.line(ox+w-9, oy+14, ox+w-4, oy+14, 12)

def draw_tree(ox, oy, h=28, trunk_col=4, leaf_col=3, leaf_col2=11):
    tw=4
    pyxel.rect(ox-tw//2, oy, tw, h, trunk_col)
    pyxel.line(ox-tw//2+1, oy+4, ox-tw//2+1, oy+h-4, 2)
    r = h // 3
    pyxel.circ(ox, oy-r+4, r+2, leaf_col)
    pyxel.circ(ox-4, oy-r+8, r, leaf_col2)
    pyxel.circ(ox+4, oy-r+6, r, leaf_col)
    pyxel.circ(ox, oy-r, r-2, leaf_col2)

def draw_moon(ox, oy, r=8):
    pyxel.circ(ox, oy, r, 7)
    pyxel.circ(ox+3, oy-2, r-2, 1)
    pyxel.pset(ox-2, oy+1, 6)
    pyxel.pset(ox-3, oy+3, 6)
    pyxel.pset(ox-1, oy+4, 6)

STAR_POSITIONS = [
    (8,6),(20,2),(35,9),(55,4),(75,11),(90,3),(108,7),(118,5),
    (13,20),(30,17),(48,22),(68,18),(86,24),(102,19),(122,15),
    (5,33),(25,30),(45,36),(65,29),(85,35),(105,31),(120,28),
    (10,45),(28,42),(50,48),(70,44),(92,46),(112,40),
    (18,58),(38,55),(60,60),(80,54),(100,57),(118,52),
]

def draw_starfield(t):
    for i,(sx,sy) in enumerate(STAR_POSITIONS):
        phase = (t//20 + i*3) % 6
        col   = 7 if 2 > phase else (6 if 4 > phase else 5)
        pyxel.pset(sx, sy, col)
        if i % 5 == 0:
            pyxel.pset(sx+1, sy, col)

def draw_fancy_border(x, y, w, h, col1=5, col2=6, col3=7):
    pyxel.rectb(x,   y,   w,   h,   col1)
    pyxel.rectb(x+1, y+1, w-2, h-2, col2)
    pyxel.rectb(x+2, y+2, w-4, h-4, col3)
    for cx, cy in [(x+1,y+1),(x+w-2,y+1),(x+1,y+h-2),(x+w-2,y+h-2)]:
        pyxel.pset(cx, cy, 10)

def draw_exclaim(x, y, col=8):
    pyxel.circ(x, y, 7, col)
    pyxel.circ(x, y, 6, 2)
    pyxel.text(x-1, y-4, "!", col)
    pyxel.text(x-2, y-4, "!", 7)

def draw_paw(x, y, col=5):
    pyxel.pset(x,   y,   col)
    pyxel.pset(x+2, y,   col)
    pyxel.pset(x+1, y+1, col)
    pyxel.pset(x,   y+2, col)
    pyxel.pset(x+2, y+2, col)

# FIX 2: idle-left now uses the mirrored right-idle sprite (blt with
# negative width), so stopping after walking left shows a standing frame.
def draw_player(px, py):
    anim_frame = (pyxel.frame_count // 8) % 2
    if not player_moving:
        if player_facing >= 0:
            pyxel.blt(px, py, 0, 32, 0, 16, 16, 1)    # idle right
        else:
            pyxel.blt(px, py, 0, 32, 0, 16, 16, 1)   # idle left = mirrored right idle
    elif player_facing >= 0:
        sx = 64 if anim_frame == 0 else 80
        pyxel.blt(px, py, 0, sx, 0, 16, 16, 1)
    else:
        sx = 96 if anim_frame == 0 else 112
        pyxel.blt(px, py, 0, sx, 0, 16, 16, 1)


# =============================================================================
# RESET / START
# =============================================================================

def reset_game():
    global state, player_x, player_y, hinweis_gefunden, hat_katzenfutter
    global hund_x, hund_y, cars, player_vy, on_ground, player_world_y
    global extra_found, baum_nofutter_timer, strasse_from_top, hund_intro_timer
    global visited_cat_no_futter
    state               = STATE_HAUS
    player_x            = 32; player_y = 32
    hinweis_gefunden    = False; hat_katzenfutter = False
    hund_x              = 120.0; hund_y = 80.0
    cars.clear()
    player_vy           = 0.0; on_ground = False; player_world_y = 490
    extra_found         = [False, False, False]
    baum_nofutter_timer = 0; strasse_from_top = False
    garten_from_top     = False; hund_intro_timer = 0
    visited_cat_no_futter = False
    player_facing = 1; player_moving = False
    set_music(0)

def start_garten(from_top=False):
    global player_x, player_y, hund_x, hund_y, garten_from_top, hund_speed
    garten_from_top = from_top
    if from_top:
        player_x = 200; player_y = 10
        hund_x   = 30.0; hund_y = 210.0
        hund_speed = 1.7
    else:
        player_x = 16; player_y = 200
        hund_x   = 120.0; hund_y = 80.0
        hund_speed = 2.2

def _spawn_cars():
    cars.clear()
    for li in range(len(LANE_DEFS)):
        for i in range(CARS_PER_LANE):
            cx = int((WORLD_W/CARS_PER_LANE)*i) + random.randint(0,15)
            cars.append([float(cx%WORLD_W), li, random.randint(0,len(CAR_SPRITES)-1)])

def start_strasse():
    global player_x, player_world_y, strasse_from_top
    player_x=60; player_world_y=490; strasse_from_top=False; _spawn_cars()

def start_strasse_top():
    global player_x, player_world_y, strasse_from_top
    player_x=60; player_world_y=GOAL_WORLD_Y+30; strasse_from_top=True; _spawn_cars()

def respawn_strasse():
    if strasse_from_top: start_strasse_top()
    else:                start_strasse()

def start_baum():
    global player_x, player_y, player_vy, on_ground, baum_nofutter_timer
    player_x=20; player_y=92; player_vy=0.0; on_ground=False; baum_nofutter_timer=0


# =============================================================================
# COLLISION
# =============================================================================

def collide_walls(x, y, walls):
    for wx,wy,ww,wh in walls:
        if wx+ww>x and x+player_w>wx and wy+wh>y and y+player_h>wy:
            return True
    return False


# =============================================================================
# UPDATE
# =============================================================================

def update():
    global state
    if   state == STATE_INTRO:      update_intro()
    elif state == STATE_HUND_INTRO: update_hund_intro()
    elif state == STATE_HAUS:
        set_music(0)
        update_haus()
    elif state == STATE_GARTEN:
        set_music(1)
        update_garten()
    elif state == STATE_STRASSE:
        set_music(0)
        update_strasse()
    elif state == STATE_BAUM:
        set_music(0)
        update_baum()
    elif state == STATE_GAMEOVER:
        set_music(-1)
        if pyxel.btnp(pyxel.KEY_R): reset_game()
    elif state == STATE_WIN:
        set_music(-1)
        if pyxel.btnp(pyxel.KEY_R): reset_game()

def update_intro():
    global intro_page, intro_timer, intro_cat_x, state, intro_jingle_played
    intro_timer += 1
    if intro_page == 0:
        intro_cat_x += 0.5
        if intro_cat_x > 136: intro_cat_x = -10.0
        if not intro_jingle_played and intro_timer == 1:
            play_sound(7); intro_jingle_played = True
    if pyxel.btnp(pyxel.KEY_SPACE) or pyxel.btnp(pyxel.KEY_RETURN):
        intro_page += 1; intro_timer = 0
        if intro_page >= INTRO_PAGES:
            intro_page=0; intro_cat_x=-10.0; intro_jingle_played=False; reset_game()

def update_hund_intro():
    global state, hund_intro_timer
    hund_intro_timer += 1
    if pyxel.btnp(pyxel.KEY_SPACE) or pyxel.btnp(pyxel.KEY_RETURN):
        state = STATE_GARTEN; start_garten()

def update_haus():
    global player_x, player_y, hinweis_gefunden, hat_katzenfutter, state, extra_found
    global player_facing, player_moving
    dx=dy=0
    if pyxel.btn(pyxel.KEY_RIGHT): dx= speed
    if pyxel.btn(pyxel.KEY_LEFT):  dx=-speed
    if pyxel.btn(pyxel.KEY_DOWN):  dy= speed
    if pyxel.btn(pyxel.KEY_UP):    dy=-speed
    player_moving = (dx != 0 or dy != 0)
    if dx > 0: player_facing = 1
    if 0 > dx: player_facing = -1

    # FIX 5: add exit blocker to walls when hinweis not yet found
    active_walls = list(walls_haus)
    if not hinweis_gefunden:
        active_walls.append(WALL_EXIT_BLOCK)

    for _ in range(abs(dx)):
        step=1 if dx>0 else -1
        if not collide_walls(player_x+offset_x+step, player_y+offset_y, active_walls):
            player_x+=step
    for _ in range(abs(dy)):
        step=1 if dy>0 else -1
        if not collide_walls(player_x+offset_x, player_y+offset_y+step, active_walls):
            player_y+=step

    if not hat_katzenfutter and 12>abs(player_x-futter_x) and 12>abs(player_y-futter_y):
        if pyxel.btnp(pyxel.KEY_E): hat_katzenfutter=True; play_sound(0)
    if not hinweis_gefunden and 12>abs(player_x-korb_x) and 12>abs(player_y-korb_y):
        if pyxel.btnp(pyxel.KEY_E): hinweis_gefunden=True; play_sound(0)
    for i,(ix,iy,label,_) in enumerate(extra_items):
        if not extra_found[i] and 14>abs(player_x-ix) and 14>abs(player_y-iy):
            if pyxel.btnp(pyxel.KEY_E): extra_found[i]=True; play_sound(0)
    if hinweis_gefunden and player_x>500:
        state=STATE_HUND_INTRO; hund_intro_timer=0

def update_garten():
    global player_x, player_y, hund_x, hund_y, state, hund_speed
    global player_facing, player_moving
    dx=dy=0
    if pyxel.btn(pyxel.KEY_RIGHT): dx= speed
    if pyxel.btn(pyxel.KEY_LEFT):  dx=-speed
    if pyxel.btn(pyxel.KEY_DOWN):  dy= speed
    if pyxel.btn(pyxel.KEY_UP):    dy=-speed
    player_moving = (dx != 0 or dy != 0)
    if dx > 0: player_facing = 1
    if 0 > dx: player_facing = -1
    if garten_from_top:
        active_walls = [
            (0,   0,   4,   256),
            (0,   0,   256, 4),
            (252, 0,   4,   256),
            (40,  40,  80,  8),
            (40,  160, 80,  8),
        ]
    else:
        active_walls = walls_garten
    for _ in range(abs(dx)):
        step = 1 if dx > 0 else -1
        if not collide_walls(player_x+offset_x+step, player_y+offset_y, active_walls):
            player_x += step
    for _ in range(abs(dy)):
        step = 1 if dy > 0 else -1
        if not collide_walls(player_x+offset_x, player_y+offset_y+step, active_walls):
            player_y += step
    dist_x = player_x - hund_x; dist_y = player_y - hund_y
    dist   = (dist_x**2 + dist_y**2) ** 0.5
    if dist > 0:
        hund_x += (dist_x / dist) * hund_speed
        hund_y += (dist_y / dist) * hund_speed
    if pyxel.frame_count % 90 == 0 and 60 > dist:
        play_sound(3, channel=1)
    if 10 > abs(player_x - hund_x) and 10 > abs(player_y - hund_y):
        play_sound(1); state = STATE_GAMEOVER
    if 8 > player_y:
        state = STATE_STRASSE; start_strasse()
    if garten_from_top and player_y >= 240:
        state = STATE_HAUS
        player_x = 490; player_y = 60

def update_strasse():
    global player_x, player_world_y, state, cars
    global player_facing, player_moving
    prev_x = player_x
    active_walls = list(walls_strasse)
    if pyxel.btnp(pyxel.KEY_RIGHT): player_x=      min(WORLD_W-player_w, player_x+8)
    if pyxel.btnp(pyxel.KEY_LEFT):  player_x=      max(0,                player_x-8)
    if pyxel.btnp(pyxel.KEY_UP):    player_world_y=max(0,                player_world_y-16)
    if pyxel.btnp(pyxel.KEY_DOWN):  player_world_y=min(WORLD_H-player_h, player_world_y+16)
    player_moving = (player_x != prev_x or pyxel.btnp(pyxel.KEY_UP) or pyxel.btnp(pyxel.KEY_DOWN))
    if player_x > prev_x: player_facing = 1
    if prev_x > player_x: player_facing = -1
    for car in cars:
        wy,d,spd=LANE_DEFS[car[1]]; car[0]+=spd*d
        if car[0]>WORLD_W:           car[0]=-CAR_SPRITE_W
        if -CAR_SPRITE_W > car[0]:   car[0]= WORLD_W
    px1=player_x+2; px2=player_x+player_w-2
    py1=player_world_y+2; py2=player_world_y+player_h-2
    for car in cars:
        wy,d,spd=LANE_DEFS[car[1]]; cx=car[0]
        mx=(CAR_SPRITE_W-CAR_HIT_W)//2; my=(CAR_SPRITE_H-CAR_HIT_H)//2
        if cx+mx+CAR_HIT_W>px1 and px2>cx+mx and wy+my+CAR_HIT_H>py1 and py2>wy+my:
            play_sound(4); respawn_strasse(); return
    if GOAL_WORLD_Y >= player_world_y: state=STATE_BAUM; start_baum()
    if player_world_y >= WORLD_H-player_h: state=STATE_GARTEN; start_garten(from_top=True)

def update_baum():
    global player_x, player_y, player_vy, on_ground, state
    global bird_x, bird_y, bird_dir, bird_flap, baum_nofutter_timer, visited_cat_no_futter
    global player_facing, player_moving
    dx=0
    if pyxel.btn(pyxel.KEY_RIGHT): dx= 2
    if pyxel.btn(pyxel.KEY_LEFT):  dx=-2
    player_moving = (dx != 0)
    if dx > 0: player_facing = 1
    if 0 > dx: player_facing = -1
    player_x=max(0,min(112,player_x+dx))
    if on_ground and pyxel.btnp(pyxel.KEY_UP):
        player_vy=JUMP_FORCE; on_ground=False; play_sound(5)
    player_vy+=GRAVITY; player_y+=player_vy
    prev_on_ground=on_ground; on_ground=False
    for px,py,pw,ph in platforms:
        feet_y=player_y+16; prev_feet=feet_y-player_vy
        in_x=player_x+4+player_w>px and px+pw>player_x+4
        if in_x and py+ph>=prev_feet and feet_y>=py and player_vy>=0:
            player_y=py-16; player_vy=0
            if not prev_on_ground: play_sound(6,channel=1)
            on_ground=True
    if player_y>130:
        baum_nofutter_timer=0; state=STATE_STRASSE; start_strasse_top(); return
    if CAT_Y+16>=player_y+16 and 22>abs(player_x-CAT_X) and hat_katzenfutter:
        play_sound(2); state=STATE_WIN; return
    if CAT_Y+20>=player_y+16 and 24>abs(player_x-CAT_X) and not hat_katzenfutter:
        baum_nofutter_timer+=1
        visited_cat_no_futter = True
        if baum_nofutter_timer>90:
            baum_nofutter_timer=0; state=STATE_STRASSE; start_strasse_top(); return
    else:
        baum_nofutter_timer=0
    bird_flap=(bird_flap+1)%20; bird_x+=0.5*bird_dir
    if bird_x>130: bird_dir=-1
    if -10 > bird_x: bird_dir= 1


# =============================================================================
# LEAF HELPERS
# =============================================================================

def init_leaves():
    global leaves, leaves_init
    leaves_init=True; leaves.clear()
    for _ in range(22):
        leaves.append([pyxel.rndi(0,127),pyxel.rndi(0,127),
                        pyxel.rndf(0.2,0.6),pyxel.rndf(-0.4,0.4)])

def update_leaves():
    for leaf in leaves:
        leaf[1]+=leaf[2]; leaf[0]+=leaf[3]
        if leaf[1]>128:
            leaf[1]=pyxel.rndi(-10,0); leaf[0]=pyxel.rndi(0,127)


# =============================================================================
# DRAW DISPATCHER
# =============================================================================

def draw():
    pyxel.cls(1)
    if   state == STATE_INTRO:      draw_intro()
    elif state == STATE_HUND_INTRO: draw_hund_intro()
    elif state == STATE_HAUS:       draw_haus()
    elif state == STATE_GARTEN:     draw_garten()
    elif state == STATE_STRASSE:    draw_strasse()
    elif state == STATE_BAUM:       draw_baum()
    elif state == STATE_GAMEOVER:   draw_gameover()
    elif state == STATE_WIN:        draw_win()


# =============================================================================
# INTRO PAGES
# =============================================================================

def draw_intro():
    t = pyxel.frame_count
    if   intro_page == 0: _draw_title(t)
    elif intro_page == 1: _draw_mission(t)
    alpha = (t // 18) % 2
    if alpha:
        shadow_text(14, 119, "LEERTASTE / ENTER  >", 10)
    for p in range(INTRO_PAGES):
        c = 10 if p == intro_page else 5
        pyxel.rect(59+p*7, 113, 4, 3, c)


# FIX 1a: Improved title screen
def _draw_title(t):
    # Rich night sky
    for row in range(82):
        if 40 > row:      c = 1
        elif 65 > row:    c = 5
        else:             c = 4
        pyxel.line(0, row, 127, row, c)
    pyxel.rect(0, 82, 128, 46, 4)
    pyxel.rect(0, 82, 128,  2, 11)
    pyxel.rect(0, 80, 128,  3, 3)

    draw_starfield(t)

    # Moon with glow ring
    pyxel.circ(108, 13, 12, 5)
    draw_moon(108, 13, r=9)

    # House with flickering window
    draw_house(3, 53, w=36, h=28, wall_col=5, roof_col=2, door_col=4, win_col=10)
    wc = 10 if (t//20)%3 != 0 else 14
    pyxel.rect(7, 65, 6, 5, wc)

    # Tree
    draw_tree(108, 82, h=32, trunk_col=4, leaf_col=3, leaf_col2=11)

    # Animated Mollie + paw trail
    mx = int(intro_cat_x)
    draw_big_mollie(mx, 74)
    for p in range(4):
        ppx = mx - 14 - p*14
        if ppx > 0 and 128 > ppx:
            draw_paw(ppx, 83, col=5 if 2 > p else 13)

    # Dark banner
    pyxel.rect(0, 4, 128, 42, 0)
    pyxel.line(0,  4,  127,  4, 2)
    pyxel.line(0, 45,  127, 45, 5)
    pyxel.line(0, 46,  127, 46, 2)

    shadow_text(34, 7, "W O   I S T", 5)

    # Big colorful MOLLIE? with double shadow
    title  = "MOLLIE?"
    colors = [8, 9, 10, 11, 12, 13, 14]
    for i, ch in enumerate(title):
        bx = 9 + i * 16
        pyxel.text(bx+3, 24, ch, 0)
        pyxel.text(bx+2, 23, ch, 0)
        pyxel.text(bx+1, 21, ch, colors[(i+3)%7])
        pyxel.text(bx-1, 21, ch, colors[(i+3)%7])
        pyxel.text(bx,   20, ch, colors[i])

    spark_col = 10 if (t//10)%2==0 else 9
    for sx, sy in [(12,34),(15,32),(113,34),(110,32)]:
        pyxel.pset(sx, sy, spark_col)
    shadow_text(20, 32, "Finde die Katze!", 7)


# FIX 1b: New mission + controls page
def _draw_mission(t):
    for row in range(128):
        c = 1 if 20 > row else 0
        pyxel.line(0, row, 127, row, c)

    draw_fancy_border(2, 2, 124, 124, col1=8, col2=2, col3=5)

    # Mission panel
    pyxel.rect(6, 6, 116, 48, 0)
    pyxel.rectb(6, 6, 116, 48, 8)

    hcol = 8 if (t//16)%2==0 else 14
    shadow_text(10, 10, "Deine Katze ist weg!", hcol)

    draw_big_mollie(8, 22)
    qcol = 14 if (t//20)%2==0 else 8
    shadow_text(24, 18, "?", qcol)

    shadow_text(30, 22, "Mollie ist verschwunden.", 7)
    shadow_text(30, 31, "Suche Hinweise im Haus,", 6)
    shadow_text(30, 40, "wo sie sein koennte!", 6)

    # Controls panel
    pyxel.rect(6, 58, 116, 52, 0)
    pyxel.rectb(6, 58, 116, 52, 5)

    shadow_text(10, 62, "Steuerung:", 9)

    pyxel.rect(10, 71, 28, 8, 5); pyxel.rectb(10, 71, 28, 8, 7)
    shadow_text(12, 73, "PFEILE", 7)
    shadow_text(41, 73, "Laufen / Springen", 6)

    pyxel.rect(10, 83, 8, 8, 5); pyxel.rectb(10, 83, 8, 8, 7)
    shadow_text(12, 85, "E", 7)
    shadow_text(21, 85, "Untersuchen", 6)

    pyxel.rect(10, 95, 8, 8, 5); pyxel.rectb(10, 95, 8, 8, 7)
    shadow_text(12, 97, "R", 7)
    shadow_text(21, 97, "Neu starten", 6)

    pyxel.line(6, 107, 121, 107, 5)
    pyxel.rect(8, 108, 5, 5, 8)
    shadow_text(16, 109, "Weiche dem Hund aus!", 7)


# =============================================================================
# HUND INTRO SCREEN
# =============================================================================

def draw_hund_intro():
    t = pyxel.frame_count
    for row in range(128):
        c = 0 if row % 2 == 0 else 2
        pyxel.line(0, row, 127, row, c)
    bc1 = 8 if (t//10)%2==0 else 2
    bc2 = 2 if (t//10)%2==0 else 8
    pyxel.rectb(0, 0, 128, 128, bc1)
    pyxel.rectb(2, 2, 124, 124, bc2)
    pyxel.rectb(4, 4, 120, 120, bc1)
    draw_exclaim(10, 10, col=8)
    draw_exclaim(118, 10, col=8)
    pyxel.rect(10, 18, 108, 14, 8)
    pyxel.rectb(10, 18, 108, 14, 14)
    shadow_text(22, 22, "! ACHTUNG ! HUND !", 14)
    pyxel.rect(10, 74, 108, 5, 4)
    pyxel.rect(10, 72, 108, 3, 3)
    pyxel.rect(38, 73, 52, 3, 2)
    draw_big_dog(40, 50, frame=(t//8)%4)
    bk = (t//15)%3
    if bk == 0:   shadow_text(10, 55, "!", 8)
    elif bk == 1: shadow_text(7, 50, "!!", 8)
    else:         shadow_text(5, 53, "WUFF!", 8)
    pyxel.rect(6, 80, 116, 42, 0)
    pyxel.rectb(6, 80, 116, 42, 8)
    shadow_text(8,  84, "Der Nachbarshund ist", 7)
    shadow_text(8,  93, "von der Leine!", 8)
    shadow_text(8, 102, "Weiche ihm aus -", 10)
    shadow_text(8, 111, "sonst ist es vorbei!", 14)
    if (t//18)%2:
        pyxel.rect(14, 120, 100, 8, 5)
        shadow_text(20, 122, "LEERTASTE: weiter", 7)


# =============================================================================
# LEVEL DRAWS
# =============================================================================

def draw_haus():
    MAP_W=MAP_H=512; SS=128
    cam_x=max(0,min(player_x-64,MAP_W-SS))
    cam_y=max(0,min(player_y-64,MAP_H-SS))
    pyxel.camera(cam_x,cam_y)
    pyxel.bltm(0,0,0,0,0,MAP_W,MAP_H, 1)
    if not hat_katzenfutter: pyxel.blt(futter_x,futter_y,0, 0,112,8,8, 1)
    pyxel.blt(korb_x,korb_y,0,48,16,16,16, 1)

    # FIX 5: draw the blocking wall visually
    if not hinweis_gefunden:
        wx,wy,ww,wh = WALL_EXIT_BLOCK
        t = pyxel.frame_count
        wc = 8 if (t//20)%2==0 else 2
        pyxel.rectb(wx, wy, ww, wh, wc)
        pyxel.rect(wx+1, 55, 6, 5, 0)
        pyxel.rectb(wx+1, 55, 6, 5, 7)
        pyxel.pset(wx+4, 57, 10)

    draw_player(player_x, player_y)
    pyxel.camera(0,0)

    hint=None
    if not hat_katzenfutter and 12>abs(player_x-futter_x) and 12>abs(player_y-futter_y):
        hint="E: Katzenfutter nehmen"
    if not hinweis_gefunden and 12>abs(player_x-korb_x) and 12>abs(player_y-korb_y):
        hint="E: Korb untersuchen"
    for i,(ix,iy,label,_) in enumerate(extra_items):
        if not extra_found[i] and 14>abs(player_x-ix) and 14>abs(player_y-iy):
            hint="E: "+label+" untersuchen"
    if hint:
        pyxel.rect(1,1,len(hint)*4+4,9,0); shadow_text(3,3,hint,7)

    # FIX 5: show message when bumping the locked exit
    if not hinweis_gefunden and player_x > 460:
        pyxel.rect(1,1,122,9,0)
        shadow_text(3,3,"Finde zuerst den Hinweis!",8)

    row=0
    if hinweis_gefunden: shadow_text(2,118-row*8,"Blutige Katzenhaare!",8); row+=1
    for i,found in enumerate(extra_found):
        if found: shadow_text(2,118-row*8,extra_texts[i],6); row+=1
    if hat_katzenfutter: shadow_text(88,2,"[Futter]",11)

def draw_garten():
    SS=128
    cam_x=max(0,min(player_x-64,256-SS))
    cam_y=max(0,min(player_y-64,256-SS))
    pyxel.camera(cam_x,cam_y)
    pyxel.bltm(0,0,1,0,0,256,256, 1)
    draw_hund(int(hund_x),int(hund_y))
    draw_player(player_x, player_y)
    pyxel.camera(0,0)
    shadow_text(5,5,"Weiche dem Hund aus!",8)
    shadow_text(5,14,"OBEN: Strasse",6)
    shadow_text(5,22,"UNTEN: Haus",6)
    if hat_katzenfutter: shadow_text(88,5,"[Futter]",11)
    if garten_from_top:
        t = pyxel.frame_count
        arrow_col = 8 if (t//14)%2==0 else 2
        ax, ay = 64, 108
        pyxel.line(ax, ay, ax, ay+8, arrow_col)
        pyxel.line(ax, ay+8, ax-4, ay+4, arrow_col)
        pyxel.line(ax, ay+8, ax+4, ay+4, arrow_col)
        shadow_text(ax-12, ay-6, "Raus hier!", arrow_col)

def draw_strasse():
    cam_y=max(0,min(player_world_y-SCREEN_H//2,WORLD_H-SCREEN_H))
    pyxel.camera(0,cam_y)
    pyxel.bltm(0,0,2,0,0,WORLD_W,WORLD_H, 1)
    for car in cars:
        wy,d,spd=LANE_DEFS[car[1]]; cx=int(car[0]); sx,sy=CAR_SPRITES[car[2]]
        if d>0: pyxel.blt(cx,wy,0,sx,sy, CAR_SPRITE_W,CAR_SPRITE_H, 1)
        else:   pyxel.blt(cx+CAR_SPRITE_W,wy,0,sx,sy,-CAR_SPRITE_W,CAR_SPRITE_H, 1)
    draw_player(player_x, player_world_y)
    pyxel.camera(0,0)
    if strasse_from_top: shadow_text(2,2,"Zurueck! Gehe UNTEN",8)
    else:                shadow_text(2,2,"Weiche den Autos aus!",7); shadow_text(2,10,"Gehe nach OBEN",6)
    if hat_katzenfutter: shadow_text(88,2,"[Futter]",11)

def draw_baum():
    global leaves_init
    pyxel.camera(0,0)
    pyxel.bltm(0,0,3,0,0,128,128, 1)
    # FIX 4: Simple solid platforms, no decorative stripes
    for i,(px,py,pw,ph) in enumerate(platforms):
        if i==len(platforms)-1:
            pyxel.rect(px, py, pw, ph, 11)   # top platform: green
        else:
            pyxel.rect(px, py, pw, ph, 4)    # normal: brown
    if not leaves_init: init_leaves()
    update_leaves()
    for leaf in leaves:
        lx,ly=int(leaf[0]),int(leaf[1]); col=11 if (lx+ly)%3!=0 else 3
        pyxel.pset(lx,ly,col); pyxel.pset(lx+1,ly,col)
        pyxel.pset(lx,ly+1,3 if col==11 else 11)
    bx,by=int(bird_x),int(bird_y); wu=10 > bird_flap
    pyxel.pset(bx,by,7); pyxel.pset(bx+1,by,7)
    pyxel.pset(bx-1,by-1 if wu else by+1,7)
    pyxel.pset(bx+2,by-1 if wu else by+1,7)
    draw_mollie(CAT_X,CAT_Y)
    shadow_text(CAT_X-10,CAT_Y-8,"Mollie!",8)
    draw_player(player_x, player_y)
    shadow_text(2,2,"HOCH = Springen",7)
    if CAT_Y+20>=player_y+16 and 24>abs(player_x-CAT_X) and not hat_katzenfutter:
        pyxel.rect(3,108,122,10,0); shadow_text(5,110,"Kein Futter! Zurueck...",8)
    if hat_katzenfutter: shadow_text(86,2,"[Futter]",11)


# =============================================================================
# GAME OVER
# =============================================================================

def draw_gameover():
    t = pyxel.frame_count
    for row in range(128):
        c = 0 if 60 > row else 1
        pyxel.line(0, row, 127, row, c)
    for i in range(20):
        rx = (i * 37 + t * 2) % 128
        ry = (i * 19 + t * 3) % 120
        pyxel.line(rx, ry, rx-2, ry+5, 5)
    pyxel.rect(0, 100, 128, 28, 1)
    pyxel.rect(0, 100, 128,  2, 5)
    draw_big_dog(46, 72, frame=(t//12)%4)
    pyxel.rect(46, 97, 36, 3, 0)
    x,y = 56, 22
    pyxel.line(x,  y,  x+6, y+7, 8)
    pyxel.line(x+6,y,  x,   y+7, 8)
    pyxel.line(x+2,y+2,x+4, y+4, 2)
    pyxel.rect(8, 36, 112, 26, 0)
    pyxel.rectb(8, 36, 112, 26, 8)
    pyxel.rectb(10, 38, 108, 22, 2)
    shadow_text(26, 40, "G A M E  O V E R", 8)
    shadow_text(34, 51, "Der Hund hat dich!", 7)
    pyxel.rect(22, 104, 84, 14, 5)
    pyxel.rectb(22, 104, 84, 14, 7)
    shadow_text(32, 108, "R = Neu starten", 10)
    pyxel.pset(4,110,7); pyxel.pset(5,110,7); pyxel.pset(6,110,7)
    pyxel.pset(4,111,7); pyxel.pset(6,111,7)
    pyxel.pset(120,110,7); pyxel.pset(121,110,7); pyxel.pset(122,110,7)
    pyxel.pset(120,111,7); pyxel.pset(122,111,7)


# =============================================================================
# WIN SCREEN
# =============================================================================

def draw_win():
    t = pyxel.frame_count
    for row in range(128):
        c = 1 if 40 > row else (5 if 80 > row else 4)
        pyxel.line(0, row, 127, row, c)
    pyxel.rect(0, 100, 128, 28, 4)
    pyxel.rect(0, 100, 128, 3, 11)
    pyxel.rect(0, 98, 128, 3, 3)
    draw_starfield(t)
    draw_moon(108, 12, r=8)
    for i in range(6):
        ang = (t * 3 + i * 60) % 360
        rad = 12 + (t % 8)
        fx = int(30 + rad * math.cos(math.radians(ang)))
        fy = int(20 + rad * math.sin(math.radians(ang)))
        pyxel.pset(fx, fy, [8,9,10,11,12,14][i])
    for i in range(6):
        ang = (t * 4 + i * 60 + 30) % 360
        rad = 10 + (t % 6)
        fx = int(98 + rad * math.cos(math.radians(ang)))
        fy = int(18 + rad * math.sin(math.radians(ang)))
        pyxel.pset(fx, fy, [10,11,12,13,14,7][i])
    pyxel.rect(6, 28, 116, 66, 0)
    draw_fancy_border(6, 28, 116, 66, col1=11, col2=3, col3=10)
    shadow_text(22, 34, "Du hast Mollie", 11)
    shadow_text(30, 44, "gefunden!  :)", 10)
    my = 58 + (1 if (t//18)%2==0 else -1)
    draw_big_mollie(56, my)
    for i, hx in enumerate([30, 90]):
        hy = 56 + ((t//20+i)%4) - 2
        pyxel.pset(hx,   hy,   8); pyxel.pset(hx+1, hy,   8)
        pyxel.pset(hx-1, hy+1, 8); pyxel.pset(hx+2, hy+1, 8)
        pyxel.pset(hx,   hy+2, 8); pyxel.pset(hx+1, hy+2, 8)
        pyxel.pset(hx,   hy+3, 8)
    for i in range(7):
        c = [8,9,10,11,12,13,14][i]
        sc = 10 + ((t//8+i)%4) - 2
        pyxel.pset(16+i*16, sc+74, c)
        pyxel.pset(16+i*16+1, sc+74, c)
    pyxel.rect(18, 98, 92, 14, 5)
    pyxel.rectb(18, 98, 92, 14, 11)
    shadow_text(26, 102, "R = Erneut spielen", 10)


# =============================================================================
# INIT
# =============================================================================
pyxel.init(128, 128, title="Wo ist Mollie?", display_scale=3)
pyxel.load("res.pyxres")
setup_sounds()

state               = STATE_INTRO
intro_page          = 0
intro_timer         = 0
intro_cat_x         = -10.0
intro_jingle_played = False
hinweis_gefunden    = False
hat_katzenfutter    = False
extra_found         = [False, False, False]
hund_x              = 120.0
hund_y              = 80.0
cars                = []
player_vy           = 0.0
on_ground           = False
player_world_y      = 490
baum_nofutter_timer   = 0
strasse_from_top      = False
hund_intro_timer      = 0
visited_cat_no_futter = False
current_music         = -1
player_facing         = 1
player_moving         = False

pyxel.run(update, draw)