import pyxel
import random
import math

# ── Konstanten ───────────────────────────────────────────────────────────────
WIDTH  = 512
HEIGHT = 512

# Farben
COL_BLACK      = 0
COL_DARK_BLUE  = 1
COL_PURPLE     = 2
COL_DARK_GREEN = 3
COL_BROWN      = 4
COL_DARK_GREY  = 5
COL_GREY       = 6
COL_WHITE      = 7
COL_RED        = 8
COL_ORANGE     = 9
COL_YELLOW     = 10
COL_GREEN      = 11
COL_LIGHT_BLUE = 12
COL_BLUE       = 13
COL_PINK       = 14
COL_PEACH      = 15

# Objekttypen
TYPE_APPLE      = 0
TYPE_WATERMELON = 1
TYPE_BOMB       = 2
TYPE_BANANA_S   = 3
TYPE_BANANA_L   = 4
TYPE_LIGHTNING  = 5
TYPE_PEACH      = 6
TYPE_LIFE       = 7

SPAWN_POOL = (
    [TYPE_APPLE]      * 45 +
    [TYPE_WATERMELON] * 15 +
    [TYPE_BOMB]       * 20 +
    [TYPE_BANANA_S]   * 7  +
    [TYPE_BANANA_L]   * 5  +
    [TYPE_LIGHTNING]  * 4  +
    [TYPE_PEACH]      * 4
)

POINTS     = [1, 3, 0, 0, 0, 0, 0, 0]
MISS_LIVES = [1, 1, 0, 0, 0, 0, 0, 0]
OBJ_RADIUS = [7, 10, 7, 5, 8, 6, 7, 7]

# Leben-Objekt alle 45 Sek = 1350 Frames
LIFE_SPAWN_INTERVAL = 1350

POWERUP_DURATION  = 150
BASKET_BASE_WIDTH = 34
BASKET_BASE_SPEED = 3.0

# ── Idee 1: Korb-Evolution ────────────────────────────────────────────────────
# Stufen: 0=Normal, 1=Gross(10Pkt), 2=Raeder(25Pkt), 3=Netz(50Pkt), 4=Magnet(80Pkt)
EVO_THRESHOLDS = [0, 10, 25, 50, 80]
EVO_NAMES      = ["Normal", "Gross", "Raeder", "Netz", "Magnet"]
EVO_COLORS     = [COL_BROWN, COL_ORANGE, COL_YELLOW, COL_LIGHT_BLUE, COL_PURPLE]

# ── Idee 4: Combo-System ──────────────────────────────────────────────────────
COMBO_NEEDED = 3   # 3x gleiche Frucht hintereinander = Kettenreaktion

# Wetter
WEATHER_CLEAR = 0
WEATHER_RAIN  = 1
WEATHER_STORM = 2
WEATHER_HEAT  = 3
WEATHER_NIGHT = 4

WEATHER_NAMES = {
    WEATHER_CLEAR: "Sonnig",
    WEATHER_RAIN:  "Regen!",
    WEATHER_STORM: "Sturm!!",
    WEATHER_HEAT:  "Hitze!",
    WEATHER_NIGHT: "Nacht...",
}
WEATHER_COLORS = {
    WEATHER_CLEAR: COL_YELLOW,
    WEATHER_RAIN:  COL_LIGHT_BLUE,
    WEATHER_STORM: COL_PURPLE,
    WEATHER_HEAT:  COL_ORANGE,
    WEATHER_NIGHT: COL_DARK_GREY,
}

WEATHER_DURATION  = 300
WEATHER_WARN_TIME = 60


# ── Klassen ───────────────────────────────────────────────────────────────────

class FallingObject:
    def __init__(self, score, weather, wind):
        self.obj_type = random.choice(SPAWN_POOL)
        self.radius   = OBJ_RADIUS[self.obj_type]
        self.x        = float(random.randint(-5, WIDTH + 5))
        self.y        = float(-self.radius)
        speed_v       = min(0.7 + score * 0.013 + random.uniform(0, 0.5), 4.5)
        if weather == WEATHER_RAIN:
            speed_v *= 1.5
        if weather == WEATHER_STORM:
            speed_v *= random.uniform(0.8, 1.8)
        self.vx = random.uniform(0.4, 1.6) * random.choice([-1, 1])
        if weather == WEATHER_STORM:
            self.vx += wind * 0.5
        self.vy     = speed_v
        self.caught = False

    def update(self, weather, wind):
        if weather == WEATHER_STORM:
            self.vx += wind * 0.04
        self.x += self.vx
        self.y += self.vy
        if self.x - self.radius < 0:
            self.x  = float(self.radius)
            self.vx = abs(self.vx)
        elif self.x + self.radius > WIDTH:
            self.x  = float(WIDTH - self.radius)
            self.vx = -abs(self.vx)

    def off_screen(self):
        return self.y - self.radius > HEIGHT

    def draw(self):
        x = int(self.x)
        y = int(self.y)
        r = self.radius
        t = self.obj_type

        if t == TYPE_APPLE:
            # Roter Kreis mit Glanzpunkt
            pyxel.circ(x, y, r, COL_RED)
            pyxel.circ(x, y, r - 2, COL_RED)
            pyxel.pset(x - 2, y - 2, 15)          # Glanzpunkt
            # Stiel
            pyxel.line(x, y - r, x, y - r - 3, COL_DARK_GREEN)
            pyxel.pset(x + 1, y - r - 2, COL_DARK_GREEN)
            # Blatt
            pyxel.line(x + 1, y - r - 1, x + 4, y - r - 3, COL_GREEN)

        elif t == TYPE_WATERMELON:
            pyxel.circ(x, y, r, COL_GREEN)
            pyxel.circ(x, y, r - 1, COL_DARK_GREEN)
            pyxel.circ(x, y, r - 3, COL_RED)
            # Kerne
            for kx, ky in [(-3,-1),(0,-2),(3,-1),(-2,2),(2,2),(0,3)]:
                pyxel.pset(x + kx, y + ky, COL_BLACK)
            # Glanz
            pyxel.pset(x - 3, y - 3, COL_WHITE)

        elif t == TYPE_BOMB:
            pyxel.circ(x, y, r, COL_DARK_GREY)
            pyxel.circ(x, y, r - 1, COL_BLACK)
            # Zündschnur
            pyxel.line(x, y - r, x + 2, y - r - 4, COL_BROWN)
            pyxel.pset(x + 2, y - r - 4, COL_ORANGE)
            pyxel.pset(x + 3, y - r - 5, COL_YELLOW)
            # Glanz
            pyxel.pset(x - 2, y - 2, COL_DARK_GREY)

        elif t == TYPE_BANANA_S:
            # Kleine Banane: erkennbare Sichelform
            # Koerper (gelbe Kurve)
            pyxel.line(x - 4, y + 3, x - 2, y - 1, COL_YELLOW)
            pyxel.line(x - 2, y - 1, x + 1, y - 3, COL_YELLOW)
            pyxel.line(x + 1, y - 3, x + 4, y - 1, COL_YELLOW)
            pyxel.line(x + 4, y - 1, x + 4, y + 2, COL_YELLOW)
            # Dicke: zweite parallele Kurve
            pyxel.line(x - 3, y + 4, x - 1, y,     COL_YELLOW)
            pyxel.line(x - 1, y,     x + 2, y - 2, COL_YELLOW)
            pyxel.line(x + 2, y - 2, x + 5, y,     COL_YELLOW)
            # Spitzen (Enden) orange
            pyxel.pset(x - 4, y + 4, COL_ORANGE)
            pyxel.pset(x - 5, y + 3, COL_ORANGE)
            pyxel.pset(x + 5, y + 2, COL_ORANGE)
            pyxel.pset(x + 5, y + 3, COL_ORANGE)
            # Glanzpunkt
            pyxel.pset(x, y - 2, COL_WHITE)

        elif t == TYPE_BANANA_L:
            # Grosse Banane: breite Sichelform
            # Aeussere Kurve (obere Kante)
            pyxel.line(x - r,     y + 4, x - r + 2, y - 2, COL_YELLOW)
            pyxel.line(x - r + 2, y - 2, x,         y - 5, COL_YELLOW)
            pyxel.line(x,         y - 5, x + r - 2, y - 2, COL_YELLOW)
            pyxel.line(x + r - 2, y - 2, x + r,     y + 3, COL_YELLOW)
            # Innere Kurve (untere Kante, etwas versetzt)
            pyxel.line(x - r + 2, y + 5, x - r + 4, y,     COL_YELLOW)
            pyxel.line(x - r + 4, y,     x,         y - 3, COL_YELLOW)
            pyxel.line(x,         y - 3, x + r - 4, y,     COL_YELLOW)
            pyxel.line(x + r - 4, y,     x + r - 1, y + 4, COL_YELLOW)
            # Fuelllinien fuer Dicke
            for fy in range(-2, 3):
                curve_x = int((fy + 2) * 1.5)
                pyxel.pset(x - curve_x, y + fy, COL_YELLOW)
                pyxel.pset(x + curve_x, y + fy, COL_YELLOW)
            # Enden (Spitzen) orange
            pyxel.rect(x - r - 1, y + 3, 3, 3, COL_ORANGE)
            pyxel.rect(x + r - 1, y + 2, 3, 3, COL_ORANGE)
            # Glanzlinie oben
            pyxel.line(x - 3, y - 4, x + 3, y - 4, COL_WHITE)
            pyxel.pset(x, y - 5, COL_WHITE)

        elif t == TYPE_LIGHTNING:
            # Blitz: Zickzack
            pyxel.line(x + 3, y - r,     x - 2, y - 1,  COL_YELLOW)
            pyxel.line(x - 2, y - 1,     x + 3, y - 1,  COL_YELLOW)
            pyxel.line(x + 3, y - 1,     x - 2, y + r,  COL_YELLOW)
            # Leuchteffekt
            pyxel.pset(x + 3, y - r,    COL_WHITE)
            pyxel.pset(x - 2, y + r,    COL_WHITE)
            pyxel.pset(x,     y,        COL_WHITE)

        elif t == TYPE_PEACH:
            # Pfirsich: runder Koerper mit Kerbe
            pyxel.circ(x, y, r, COL_ORANGE)
            pyxel.circ(x, y, r - 1, COL_PINK)
            pyxel.circ(x, y, r - 2, COL_PEACH)
            # Kerbe oben
            pyxel.line(x, y - r, x, y - r + 2, COL_ORANGE)
            # Stiel
            pyxel.line(x, y - r, x + 1, y - r - 3, COL_DARK_GREEN)
            # Blatt
            pyxel.line(x + 1, y - r - 1, x + 4, y - r - 4, COL_GREEN)
            pyxel.line(x + 2, y - r - 2, x + 5, y - r - 3, COL_GREEN)
            # Glanz
            pyxel.pset(x - 2, y - 2, COL_WHITE)

        elif t == TYPE_LIFE:
            # Leben-Herz: goldenes Herz mit Leuchten
            # Aeusserer Schimmer
            pyxel.pset(x - 4, y - 1, COL_YELLOW)
            pyxel.pset(x + 4, y - 1, COL_YELLOW)
            pyxel.pset(x,     y + 5, COL_YELLOW)
            # Herz (rot mit orangem Rand)
            heart = [
                (-3, -1), (-2, -1), (2, -1), (3, -1),
                (-4, 0),  (-3, 0),  (-2, 0), (-1, 0), (0, 0), (1, 0), (2, 0), (3, 0), (4, 0),
                (-4, 1),  (-3, 1),  (-2, 1), (-1, 1), (0, 1), (1, 1), (2, 1), (3, 1), (4, 1),
                (-3, 2),  (-2, 2),  (-1, 2), (0, 2),  (1, 2), (2, 2), (3, 2),
                (-2, 3),  (-1, 3),  (0, 3),  (1, 3),  (2, 3),
                (-1, 4),  (0, 4),   (1, 4),
            ]
            for hx2, hy2 in heart:
                pyxel.pset(x + hx2, y + hy2, COL_RED)
            # Glanz oben links
            pyxel.pset(x - 2, y,     COL_ORANGE)
            pyxel.pset(x - 3, y + 1, COL_ORANGE)
            # Weisser Glanzpunkt
            pyxel.pset(x - 2, y - 1, COL_WHITE)


class Raindrop:
    def __init__(self):
        self.reset()

    def reset(self):
        self.x   = random.randint(0, WIDTH)
        self.y   = random.randint(-HEIGHT, 0)
        self.spd = random.uniform(4.0, 7.0)
        self.len = random.randint(3, 6)

    def update(self):
        self.y += self.spd
        self.x += 0.8
        if self.y > HEIGHT:
            self.reset()

    def draw(self):
        x1 = int(self.x)
        y1 = int(self.y)
        pyxel.line(x1, y1, x1 + 1, y1 + self.len, COL_LIGHT_BLUE)


class StormParticle:
    def __init__(self, wind):
        self.x   = float(random.randint(0, WIDTH))
        self.y   = float(random.randint(0, HEIGHT))
        self.spd = random.uniform(1.5, 3.5)
        self.col = COL_GREY if random.random() > 0.5 else COL_WHITE
        self.size = random.randint(1, 2)

    def update(self, wind):
        self.x += wind * 2.0 + random.uniform(-0.3, 0.3)
        self.y += self.spd
        if self.y > HEIGHT:
            self.y = 0.0
            self.x = float(random.randint(0, WIDTH))
        if self.x < -10:
            self.x = float(WIDTH + 10)
        elif self.x > WIDTH + 10:
            self.x = -10.0

    def draw(self):
        x = int(self.x)
        y = int(self.y)
        if self.size == 1:
            pyxel.pset(x, y, self.col)
        else:
            pyxel.rect(x, y, 2, 1, self.col)


class Basket:
    def __init__(self):
        self.x           = float(WIDTH // 2)
        self.y           = float(HEIGHT - 12)
        self.base_y      = float(HEIGHT - 12)
        self.vel_x       = 0.0
        self.jumping     = False
        self.jump_vy     = 0.0
        self.small_timer = 0
        self.large_timer = 0
        self.fast_timer  = 0
        self.jump_timer  = 0

    @property
    def eff_width(self):
        # Evolution bestimmt Basis-Breite
        evo_bonus = [0, 10, 10, 16, 16]
        base = BASKET_BASE_WIDTH + evo_bonus[evo_level]
        if self.small_timer > 0:
            return max(14, base - 12)
        if self.large_timer > 0:
            return min(70, base + 20)
        return base

    @property
    def eff_speed(self):
        # Stufe 2 (Räder): dauerhaft +30% Geschwindigkeit
        evo_spd = [1.0, 1.0, 1.3, 1.3, 1.3]
        base    = BASKET_BASE_SPEED * evo_spd[evo_level]
        return base * (2.2 if self.fast_timer > 0 else 1.0)

    def update(self, weather, wind):
        if self.small_timer > 0: self.small_timer -= 1
        if self.large_timer > 0: self.large_timer -= 1
        if self.fast_timer  > 0: self.fast_timer  -= 1
        if self.jump_timer  > 0: self.jump_timer  -= 1

        spd = self.eff_speed

        if weather == WEATHER_RAIN:
            if pyxel.btn(pyxel.KEY_LEFT):
                self.vel_x -= 0.5
            elif pyxel.btn(pyxel.KEY_RIGHT):
                self.vel_x += 0.5
            else:
                self.vel_x *= 0.88
            self.vel_x = max(-spd * 1.4, min(spd * 1.4, self.vel_x))
            self.x += self.vel_x
        elif weather == WEATHER_STORM:
            if pyxel.btn(pyxel.KEY_LEFT):
                self.x -= spd
            if pyxel.btn(pyxel.KEY_RIGHT):
                self.x += spd
            self.x += wind * 0.3
            self.vel_x = 0.0
        else:
            if pyxel.btn(pyxel.KEY_LEFT):
                self.x -= spd
            if pyxel.btn(pyxel.KEY_RIGHT):
                self.x += spd
            self.vel_x = 0.0

        if self.jump_timer > 0 and not self.jumping:
            if pyxel.btnp(pyxel.KEY_UP) or pyxel.btnp(pyxel.KEY_SPACE):
                self.jumping = True
                self.jump_vy = -5.0

        if self.jumping:
            self.y      += self.jump_vy
            self.jump_vy += 0.3
            if self.y >= self.base_y:
                self.y       = self.base_y
                self.jumping = False
                self.jump_vy = 0.0

        half   = self.eff_width // 2
        self.x = max(float(half), min(float(WIDTH - half), self.x))

    def catches(self, obj):
        half = self.eff_width // 2
        oy   = obj.y + obj.radius
        return (self.x - half - obj.radius <= obj.x <= self.x + half + obj.radius
                and self.y - 6 <= oy <= self.y + 8)

    def draw(self, weather):
        half = self.eff_width // 2
        bx   = int(self.x)
        by   = int(self.y)
        w    = self.eff_width
        ecol = EVO_COLORS[evo_level]

        # Korb-Schatten
        pyxel.rect(bx - half + 2, by + 5, w, 4, COL_BLACK)

        # Korb-Boden – Farbe je nach Evolutionsstufe
        pyxel.rect(bx - half, by, w, 5, ecol)
        # Korb-Seiten
        pyxel.rect(bx - half,     by - 7, 4, 7, ecol)
        pyxel.rect(bx + half - 4, by - 7, 4, 7, ecol)
        # Mittelstuetze
        pyxel.rect(bx - 2, by - 7, 4, 7, ecol)

        # Korbgitter
        grid_cols = w // 6
        for i in range(1, grid_cols):
            lx = bx - half + i * 6
            pyxel.line(lx, by, lx, by + 5, COL_DARK_GREY)
        pyxel.line(bx - half, by + 2, bx + half, by + 2, COL_DARK_GREY)

        # Stufe 2: Räder zeichnen
        if evo_level >= 2:
            pyxel.circ(bx - half + 3, by + 6, 3, COL_DARK_GREY)
            pyxel.circ(bx - half + 3, by + 6, 2, COL_GREY)
            pyxel.circ(bx + half - 3, by + 6, 3, COL_DARK_GREY)
            pyxel.circ(bx + half - 3, by + 6, 2, COL_GREY)

        # Stufe 3: Netz – extra Linien oben
        if evo_level >= 3:
            for i in range(0, w, 4):
                lx = bx - half + i
                pyxel.line(lx, by - 7, lx + 2, by - 12, COL_LIGHT_BLUE)
            pyxel.line(bx - half, by - 12, bx + half, by - 12, COL_LIGHT_BLUE)

        # Stufe 4: Magnet – lila Hufeisen über dem Korb
        if evo_level >= 4:
            pyxel.circ(bx - 6, by - 14, 4, COL_PURPLE)
            pyxel.circ(bx - 6, by - 14, 2, COL_BLACK)
            pyxel.circ(bx + 6, by - 14, 4, COL_PURPLE)
            pyxel.circ(bx + 6, by - 14, 2, COL_BLACK)
            pyxel.rect(bx - 8, by - 14, 16, 3, COL_PURPLE)
            pyxel.rect(bx - 6, by - 10, 3, 4, COL_RED)
            pyxel.rect(bx + 3, by - 10, 3, 4, COL_BLUE)

        # Regen: nasse Tropfen
        if weather == WEATHER_RAIN:
            for i in range(0, w, 6):
                pyxel.pset(bx - half + i, by + 6, COL_LIGHT_BLUE)

        # Power-up Rahmen
        if self.fast_timer > 0:
            pyxel.rectb(bx - half - 2, by - 9, w + 4, 16, COL_YELLOW)
            pyxel.rectb(bx - half - 1, by - 8, w + 2, 14, COL_YELLOW)
        elif self.jump_timer > 0:
            pyxel.rectb(bx - half - 2, by - 9, w + 4, 16, COL_PINK)
            pyxel.rectb(bx - half - 1, by - 8, w + 2, 14, COL_PINK)
        elif self.small_timer > 0:
            pyxel.rectb(bx - half - 2, by - 9, w + 4, 16, COL_RED)
        elif self.large_timer > 0:
            pyxel.rectb(bx - half - 2, by - 9, w + 4, 16, COL_GREEN)


class Particle:
    def __init__(self, x, y, color):
        self.x    = float(x)
        self.y    = float(y)
        self.vx   = random.uniform(-2.0, 2.0)
        self.vy   = random.uniform(-3.0, -0.8)
        self.col  = color
        self.life = random.randint(14, 28)
        self.size = random.randint(1, 2)

    def update(self):
        self.x    += self.vx
        self.y    += self.vy
        self.vy   += 0.15
        self.life -= 1

    def draw(self):
        if self.life > 0:
            x = int(self.x)
            y = int(self.y)
            if self.size == 1:
                pyxel.pset(x, y, self.col)
            else:
                pyxel.rect(x, y, 2, 2, self.col)


# ── Globaler Zustand ──────────────────────────────────────────────────────────

basket            = None
objects           = []
particles         = []
score_anim        = []
raindrops         = []
storm_particles   = []
score             = 0
lives             = 3
frame_count       = 0
flash             = 0
flash_col         = COL_RED
bomb_hit          = False
powerup_msg       = ""
powerup_msg_timer = 0
state             = "title"

weather           = WEATHER_CLEAR
weather_timer     = WEATHER_DURATION
next_weather      = WEATHER_CLEAR
weather_warning   = 0
weather_announce  = 0
wind              = 0.0
wind_target       = 0.0
heat_flicker      = 0

# Idee 1: Evolution
evo_level         = 0
evo_announce      = 0   # Frames fuer "EVOLUTION!" Anzeige

# Idee 4: Combo
combo_count       = 0
combo_last_type   = -1
combo_announce    = 0   # Frames fuer "KETTENREAKTION!" Anzeige


def init_game():
    global basket, objects, particles, score_anim, raindrops, storm_particles
    global score, lives, frame_count, flash, flash_col, bomb_hit
    global powerup_msg, powerup_msg_timer, state
    global weather, weather_timer, next_weather, weather_warning, weather_announce
    global wind, wind_target, heat_flicker

    basket            = Basket()
    objects           = []
    particles         = []
    score_anim        = []
    raindrops         = [Raindrop() for _ in range(50)]
    storm_particles   = [StormParticle(0) for _ in range(60)]
    score             = 0
    lives             = 3
    frame_count       = 0
    flash             = 0
    flash_col         = COL_RED
    bomb_hit          = False
    powerup_msg       = ""
    powerup_msg_timer = 0
    state             = "title"
    weather           = WEATHER_CLEAR
    weather_timer     = WEATHER_DURATION
    next_weather      = WEATHER_CLEAR
    weather_warning   = 0
    weather_announce  = 0
    wind              = 0.0
    wind_target       = 0.0
    heat_flicker      = 0
    evo_level         = 0
    evo_announce      = 0
    combo_count       = 0
    combo_last_type   = -1
    combo_announce    = 0


def pick_next_weather(current):
    choices = [w for w in range(5) if w != current]
    return random.choice(choices)


def spawn_particles(x, y, color, n):
    for _ in range(n):
        particles.append(Particle(x, y, color))


# ── Update ────────────────────────────────────────────────────────────────────

def update():
    global state, frame_count, flash, flash_col, score, lives
    global bomb_hit, powerup_msg, powerup_msg_timer
    global weather, weather_timer, next_weather, weather_warning, weather_announce
    global wind, wind_target, heat_flicker
    global evo_level, evo_announce
    global combo_count, combo_last_type, combo_announce

    if state == "title":
        if pyxel.btnp(pyxel.KEY_RETURN) or pyxel.btnp(pyxel.KEY_SPACE):
            state         = "playing"
            frame_count   = 0
            weather       = WEATHER_CLEAR
            weather_timer = WEATHER_DURATION
            next_weather  = pick_next_weather(WEATHER_CLEAR)
            weather_warning = WEATHER_DURATION - WEATHER_WARN_TIME

    elif state == "playing":
        frame_count += 1

        # Wetter
        weather_timer -= 1
        wind += (wind_target - wind) * 0.05
        if weather == WEATHER_STORM and frame_count % 90 == 0:
            wind_target = random.uniform(-2.5, 2.5)
        if weather == WEATHER_HEAT:
            heat_flicker = (heat_flicker + 1) % 8
        if weather_timer == WEATHER_WARN_TIME:
            weather_warning  = WEATHER_WARN_TIME
            weather_announce = 0
        if weather_warning > 0:
            weather_warning -= 1
        if weather_timer <= 0:
            weather          = next_weather
            weather_timer    = WEATHER_DURATION
            next_weather     = pick_next_weather(weather)
            weather_warning  = 0
            weather_announce = 90
            if weather == WEATHER_STORM:
                wind_target = random.uniform(-2.5, 2.5)
            else:
                wind_target = 0.0
        if weather_announce > 0:
            weather_announce -= 1

        # Wetter-Effekte
        if weather == WEATHER_RAIN:
            for rd in raindrops:
                rd.update()
        elif weather == WEATHER_STORM:
            for sp in storm_particles:
                sp.update(wind)

        basket.update(weather, wind)

        interval = max(22, 50 - score // 5)
        if frame_count % interval == 0:
            objects.append(FallingObject(score, weather, wind))

        # Leben-Objekt alle 45 Sekunden (1350 Frames)
        if frame_count % LIFE_SPAWN_INTERVAL == 0:
            life_obj          = FallingObject.__new__(FallingObject)
            life_obj.obj_type = TYPE_LIFE
            life_obj.radius   = OBJ_RADIUS[TYPE_LIFE]
            life_obj.x        = float(random.randint(20, WIDTH - 20))
            life_obj.y        = float(-life_obj.radius)
            life_obj.vx       = random.uniform(-0.6, 0.6)
            life_obj.vy       = 1.2
            life_obj.caught   = False
            objects.append(life_obj)

        for p in particles:
            p.update()
        particles[:] = [p for p in particles if p.life > 0]
        score_anim[:] = [(x, y, t, ti - 1) for x, y, t, ti in score_anim if ti > 1]
        if powerup_msg_timer > 0:
            powerup_msg_timer -= 1

        # ── Idee 1: Evolution prüfen ─────────────────────────────────────────
        new_evo = 0
        for lvl, threshold in enumerate(EVO_THRESHOLDS):
            if score >= threshold:
                new_evo = lvl
        if new_evo > evo_level:
            evo_level    = new_evo
            evo_announce = 120   # 4 Sek Ankuendigung
        if evo_announce > 0:
            evo_announce -= 1

        if combo_announce > 0:
            combo_announce -= 1

        to_remove   = []
        extra_bombs = []

        for obj in objects:
            obj.update(weather, wind)

            # ── Idee 1: Magnet (Stufe 4) zieht Früchte an ───────────────────
            if evo_level >= 4 and obj.obj_type not in (TYPE_BOMB, TYPE_LIFE):
                dx = basket.x - obj.x
                dy = basket.y - obj.y
                dist = max(1.0, (dx * dx + dy * dy) ** 0.5)
                if dist < 80:
                    obj.x += dx / dist * 1.2
                    obj.y += dy / dist * 0.8

            if basket.catches(obj):
                to_remove.append(obj)
                t  = obj.obj_type
                ox = int(obj.x)
                oy = int(obj.y)

                if t == TYPE_APPLE:
                    pts = 2 if weather == WEATHER_CLEAR else 1
                    score += pts
                    spawn_particles(ox, oy, COL_RED, 8)
                    score_anim.append((ox, oy - 8, "+" + str(pts), 35))
                    # Combo-Check
                    if combo_last_type == TYPE_APPLE:
                        combo_count += 1
                    else:
                        combo_count     = 1
                        combo_last_type = TYPE_APPLE
                    if combo_count >= COMBO_NEEDED:
                        combo_count    = 0
                        combo_announce = 90
                        objects[:] = [o for o in objects if o.obj_type != TYPE_BOMB]
                        spawn_particles(WIDTH // 2, HEIGHT // 2, COL_ORANGE, 30)

                elif t == TYPE_WATERMELON:
                    pts = 5 if weather == WEATHER_CLEAR else 3
                    score += pts
                    spawn_particles(ox, oy, COL_GREEN, 14)
                    score_anim.append((ox, oy - 8, "+" + str(pts), 45))
                    # Combo-Check
                    if combo_last_type == TYPE_WATERMELON:
                        combo_count += 1
                    else:
                        combo_count     = 1
                        combo_last_type = TYPE_WATERMELON
                    if combo_count >= COMBO_NEEDED:
                        combo_count    = 0
                        combo_announce = 90
                        objects[:] = [o for o in objects if o.obj_type != TYPE_BOMB]
                        spawn_particles(WIDTH // 2, HEIGHT // 2, COL_GREEN, 30)

                elif t == TYPE_BOMB:
                    bomb_hit  = True
                    flash     = 20
                    flash_col = COL_RED
                    state     = "gameover"
                    break

                elif t == TYPE_BANANA_S:
                    basket.small_timer = POWERUP_DURATION
                    basket.large_timer = 0
                    spawn_particles(ox, oy, COL_YELLOW, 8)
                    powerup_msg       = "Korb kleiner!"
                    powerup_msg_timer = 90

                elif t == TYPE_BANANA_L:
                    basket.large_timer = POWERUP_DURATION
                    basket.small_timer = 0
                    spawn_particles(ox, oy, COL_YELLOW, 10)
                    powerup_msg       = "Korb groesser!"
                    powerup_msg_timer = 90

                elif t == TYPE_LIGHTNING:
                    basket.fast_timer = POWERUP_DURATION
                    spawn_particles(ox, oy, COL_YELLOW, 10)
                    powerup_msg       = "Turbo-Speed!"
                    powerup_msg_timer = 90

                elif t == TYPE_PEACH:
                    basket.jump_timer = POWERUP_DURATION
                    spawn_particles(ox, oy, COL_PINK, 10)
                    powerup_msg       = "Sprung aktiv!"
                    powerup_msg_timer = 90

                elif t == TYPE_LIFE:
                    if lives < 3:
                        lives += 1
                    spawn_particles(ox, oy, COL_RED, 14)
                    score_anim.append((ox, oy - 8, "+LEBEN!", 50))

            elif obj.off_screen():
                to_remove.append(obj)
                cost = MISS_LIVES[obj.obj_type]
                if cost > 0:
                    lives    -= cost
                    flash     = 8
                    flash_col = COL_RED
                if weather == WEATHER_HEAT and obj.obj_type == TYPE_BOMB:
                    for _ in range(2):
                        nb          = FallingObject.__new__(FallingObject)
                        nb.obj_type = TYPE_BOMB
                        nb.radius   = OBJ_RADIUS[TYPE_BOMB]
                        nb.x        = float(random.randint(20, WIDTH - 20))
                        nb.y        = 0.0
                        nb.vx       = random.uniform(-1.0, 1.0)
                        nb.vy       = random.uniform(1.5, 3.0)
                        nb.caught   = False
                        extra_bombs.append(nb)

        for obj in to_remove:
            if obj in objects:
                objects.remove(obj)
        objects.extend(extra_bombs)

        if lives <= 0 and state == "playing":
            lives     = 0
            flash     = 20
            flash_col = COL_DARK_GREY
            state     = "gameover"

    elif state == "gameover":
        if flash > 0:
            flash -= 1
        if pyxel.btnp(pyxel.KEY_RETURN) or pyxel.btnp(pyxel.KEY_SPACE):
            init_game()


# ── Draw ──────────────────────────────────────────────────────────────────────

def draw():
    if state == "title":
        draw_title()
    elif state == "playing":
        draw_playing()
    elif state == "gameover":
        draw_gameover()


def draw_title():
    pyxel.cls(COL_DARK_BLUE)

    # Sterne
    random.seed(42)
    for _ in range(70):
        sx  = random.randint(0, WIDTH - 1)
        sy  = random.randint(0, HEIGHT - 1)
        col = COL_WHITE if random.random() > 0.5 else COL_GREY
        pyxel.pset(sx, sy, col)
    random.seed()

    # Titel mit Schatten
    tx = WIDTH // 2 - 40
    pyxel.text(tx + 1, 17, "FRUIT CATCHER", COL_DARK_GREY)
    pyxel.text(tx,     16, "FRUIT CATCHER", COL_ORANGE)
    pyxel.text(tx - 1, 15, "FRUIT CATCHER", COL_YELLOW)

    # Untertitel
    sub = "Das ultimative Frucht-Fang-Spiel!"
    pyxel.text(WIDTH // 2 - len(sub) * 2, 26, sub, COL_WHITE)

    # Trennlinie
    pyxel.line(20, 34, WIDTH - 20, 34, COL_DARK_GREY)

    # Demo Objekte mit Beschriftungen
    pyxel.text(WIDTH // 2 - 44, 39, "Objekte:", COL_GREY)
    demo_list = [
        (22,  58, TYPE_APPLE,      "+1 Pkt",   COL_RED),
        (60,  58, TYPE_WATERMELON, "+3 Pkt",   COL_GREEN),
        (100, 58, TYPE_BOMB,       "TOD!",     COL_RED),
        (136, 58, TYPE_BANANA_L,   "Groesse",  COL_YELLOW),
        (172, 58, TYPE_LIGHTNING,  "Speed",    COL_YELLOW),
        (206, 58, TYPE_PEACH,      "Sprung",   COL_PINK),
        (238, 58, TYPE_LIFE,       "+Leben",   COL_RED),
    ]
    for dx, dy, dt, dlbl, dcol in demo_list:
        tmp          = FallingObject.__new__(FallingObject)
        tmp.obj_type = dt
        tmp.radius   = OBJ_RADIUS[dt]
        tmp.x        = float(dx)
        tmp.y        = float(dy)
        tmp.draw()
        pyxel.text(dx - len(dlbl) * 2, dy + 13, dlbl, dcol)

    # Trennlinie
    pyxel.line(20, 78, WIDTH - 20, 78, COL_DARK_GREY)

    # Wetter-Erklaerung
    pyxel.text(WIDTH // 2 - 48, 83, "Wetter-System:", COL_WHITE)
    weathers_demo = [
        (20,  95, COL_YELLOW,     "Sonnig",  "Bonus-Punkte!"),
        (20, 107, COL_LIGHT_BLUE, "Regen",   "Korb rutscht!"),
        (138, 95, COL_PURPLE,     "Sturm",   "Wind lenkt ab!"),
        (138,107, COL_ORANGE,     "Hitze",   "Bomben x2!"),
        (20, 119, COL_DARK_GREY,  "Nacht",   "Nur Lichtkegel!"),
    ]
    for wx, wy, wc, wname, wdesc in weathers_demo:
        pyxel.rectb(wx, wy, 114, 10, wc)
        pyxel.text(wx + 3, wy + 2, wname + ": " + wdesc, wc)

    # Steuerung
    pyxel.line(20, 132, WIDTH - 20, 132, COL_DARK_GREY)
    pyxel.text(20, 137, "Pfeiltasten Links/Rechts: Korb bewegen",  COL_LIGHT_BLUE)
    pyxel.text(20, 147, "Pfirsich fangen  = Sprung (Hoch/Space)",  COL_PINK)
    pyxel.text(20, 157, "3x gleiche Frucht = KETTENREAKTION!",      COL_ORANGE)
    pyxel.text(20, 167, "Punkte sammeln = Korb-EVOLUTION!",         COL_PURPLE)

    # Start-Hinweis
    if (pyxel.frame_count // 15) % 2 == 0:
        hint = "ENTER oder SPACE zum Starten"
        pyxel.rect(WIDTH // 2 - len(hint) * 2 - 4, 178,
                   len(hint) * 4 + 8, 11, COL_DARK_BLUE)
        pyxel.rectb(WIDTH // 2 - len(hint) * 2 - 4, 178,
                    len(hint) * 4 + 8, 11, COL_YELLOW)
        pyxel.text(WIDTH // 2 - len(hint) * 2, 181, hint, COL_WHITE)


def draw_playing():
    global flash

    if flash > 0:
        pyxel.cls(flash_col)
        flash -= 1
        return

    # Hintergrund je nach Wetter
    if weather == WEATHER_NIGHT:
        pyxel.cls(COL_BLACK)
    elif weather == WEATHER_RAIN:
        pyxel.cls(COL_DARK_BLUE)
    elif weather == WEATHER_STORM:
        pyxel.cls(COL_PURPLE)
    elif weather == WEATHER_HEAT:
        bg = COL_BROWN if heat_flicker < 4 else COL_DARK_GREY
        pyxel.cls(bg)
    else:
        pyxel.cls(COL_DARK_BLUE)

    # Regen
    if weather == WEATHER_RAIN:
        for rd in raindrops:
            rd.draw()

    # Sturm
    elif weather == WEATHER_STORM:
        for sp in storm_particles:
            sp.draw()
        # Wind-Pfeil gross in der Mitte
        arrow  = "  >>>  " if wind > 0 else "  <<<  "
        intensity = min(int(abs(wind) * 1.5), 3)
        w_cols = [COL_DARK_GREY, COL_GREY, COL_WHITE, COL_YELLOW]
        pyxel.text(WIDTH // 2 - 16, HEIGHT // 2 - 4, arrow, w_cols[intensity])

    # Hitzewelle: Sonne
    elif weather == WEATHER_HEAT:
        pyxel.circ(WIDTH - 20, 20, 14, COL_YELLOW)
        pyxel.circ(WIDTH - 20, 20, 11, COL_ORANGE)
        pyxel.circ(WIDTH - 20, 20,  8, COL_YELLOW)
        for i in range(8):
            a   = i * (math.pi / 4)
            sx1 = int((WIDTH - 20) + 15 * math.cos(a))
            sy1 = int(20 + 15 * math.sin(a))
            sx2 = int((WIDTH - 20) + 20 * math.cos(a))
            sy2 = int(20 + 20 * math.sin(a))
            pyxel.line(sx1, sy1, sx2, sy2, COL_YELLOW)

    # Nacht: Sterne und Mond
    elif weather == WEATHER_NIGHT:
        random.seed(77)
        for _ in range(60):
            sx  = random.randint(0, WIDTH - 1)
            sy  = random.randint(0, HEIGHT - 30)
            col = COL_WHITE if random.random() > 0.6 else COL_GREY
            if random.random() > 0.9:
                pyxel.rect(sx, sy, 2, 2, col)
            else:
                pyxel.pset(sx, sy, col)
        random.seed()
        # Mond
        pyxel.circ(24, 20, 12, COL_YELLOW)
        pyxel.circ(29, 16, 10, COL_BLACK)

    # Normale Sterne (Sonnig)
    else:
        random.seed(99)
        for _ in range(35):
            sx  = random.randint(0, WIDTH - 1)
            sy  = random.randint(0, HEIGHT - 30)
            col = COL_GREY if (pyxel.frame_count // 30 + sx) % 3 == 0 else COL_DARK_GREY
            pyxel.pset(sx, sy, col)
        random.seed()

    # Boden-Linie
    pyxel.line(0, HEIGHT - 2, WIDTH - 1, HEIGHT - 2, COL_DARK_GREY)
    pyxel.line(0, HEIGHT - 1, WIDTH - 1, HEIGHT - 1, COL_DARK_GREY)

    # Objekte (Nacht = Lichtkegel)
    if weather == WEATHER_NIGHT:
        bx      = int(basket.x)
        by      = int(basket.y)
        light_r = 90
        for obj in objects:
            dx = int(obj.x) - bx
            dy = int(obj.y) - by
            if dx * dx + dy * dy < light_r * light_r:
                obj.draw()
        # Lichtkegel-Rand
        for angle_step in range(0, 360, 8):
            a  = angle_step * math.pi / 180
            lx = bx + int(light_r * math.cos(a))
            ly = by + int(light_r * math.sin(a))
            if 0 <= lx < WIDTH and 0 <= ly < HEIGHT:
                pyxel.pset(lx, ly, COL_DARK_GREY)
        # Zweiter innerer Ring fuer weicheren Effekt
        for angle_step in range(0, 360, 6):
            a  = angle_step * math.pi / 180
            lx = bx + int((light_r - 5) * math.cos(a))
            ly = by + int((light_r - 5) * math.sin(a))
            if 0 <= lx < WIDTH and 0 <= ly < HEIGHT:
                pyxel.pset(lx, ly, COL_DARK_GREY)
        # Dritter Ring fuer noch weicheren Uebergang
        for angle_step in range(0, 360, 5):
            a  = angle_step * math.pi / 180
            lx = bx + int((light_r - 10) * math.cos(a))
            ly = by + int((light_r - 10) * math.sin(a))
            if 0 <= lx < WIDTH and 0 <= ly < HEIGHT:
                pyxel.pset(lx, ly, COL_DARK_GREY)
    else:
        for obj in objects:
            obj.draw()

    for p in particles:
        p.draw()

    basket.draw(weather)

    # ── HUD ───────────────────────────────────────────────────────────────────
    # Score-Box
    pyxel.rect(0, 0, 80, 13, COL_BLACK)
    pyxel.rectb(0, 0, 80, 13, COL_DARK_GREY)
    pyxel.text(4, 3, "SCORE: " + str(score), COL_YELLOW)

    # Evolution-Anzeige (links unten neben Score)
    evo_txt = "EVO:" + EVO_NAMES[evo_level]
    ecol    = EVO_COLORS[evo_level]
    pyxel.rect(0, 13, len(evo_txt) * 4 + 8, 11, COL_BLACK)
    pyxel.rectb(0, 13, len(evo_txt) * 4 + 8, 11, ecol)
    pyxel.text(4, 16, evo_txt, ecol)

    # Naechste Evo-Schwelle anzeigen
    if evo_level < 4:
        next_thresh = EVO_THRESHOLDS[evo_level + 1]
        bar_w       = 60
        filled      = int((score - EVO_THRESHOLDS[evo_level]) /
                          max(1, next_thresh - EVO_THRESHOLDS[evo_level]) * bar_w)
        filled      = min(bar_w, filled)
        pyxel.rect( 0, 25, bar_w, 4, COL_DARK_GREY)
        pyxel.rect( 0, 25, filled, 4, ecol)
        pyxel.rectb(0, 25, bar_w, 4, COL_BLACK)
        pyxel.text( bar_w + 2, 24, str(next_thresh), COL_GREY)

    # Wetter-Anzeige Mitte oben
    w_name = WEATHER_NAMES[weather]
    w_col  = WEATHER_COLORS[weather]
    wt_x   = WIDTH // 2 - len(w_name) * 2
    pyxel.rect(wt_x - 4, 0, len(w_name) * 4 + 8, 13, COL_BLACK)
    pyxel.rectb(wt_x - 4, 0, len(w_name) * 4 + 8, 13, w_col)
    pyxel.text(wt_x, 3, w_name, w_col)

    # Leben als Herzen (rechts oben)
    for i in range(3):
        col = COL_RED if i < lives else COL_DARK_GREY
        hx  = WIDTH - 10 - i * 14
        hy  = 2
        heart = [
            (-2, 0), (-1, 0), (1, 0), (2, 0),
            (-3, 1), (-2, 1), (-1, 1), (0, 1), (1, 1), (2, 1), (3, 1),
            (-3, 2), (-2, 2), (-1, 2), (0, 2), (1, 2), (2, 2), (3, 2),
            (-2, 3), (-1, 3), (0, 3), (1, 3), (2, 3),
            (-1, 4), (0, 4), (1, 4),
            (0, 5),
        ]
        for dx, dy in heart:
            pyxel.pset(hx + dx, hy + dy, col)

    # Wetter-Vorwarnung
    if weather_warning > 0:
        nxt_name = WEATHER_NAMES[next_weather]
        nxt_col  = WEATHER_COLORS[next_weather]
        if (weather_warning // 8) % 2 == 0:
            warn_txt = "Kommt: " + nxt_name + "!"
            wx_pos   = WIDTH // 2 - len(warn_txt) * 2
            pyxel.rect( wx_pos - 3, 15, len(warn_txt) * 4 + 6, 11, COL_BLACK)
            pyxel.rectb(wx_pos - 3, 15, len(warn_txt) * 4 + 6, 11, nxt_col)
            pyxel.text( wx_pos, 18, warn_txt, nxt_col)

    # Wetter-Ankuendigung
    if weather_announce > 0:
        ann_txt = ">>> " + WEATHER_NAMES[weather] + " <<<"
        ax_pos  = WIDTH // 2 - len(ann_txt) * 2
        pyxel.rect( ax_pos - 4, HEIGHT // 2 - 10, len(ann_txt) * 4 + 8, 14, COL_BLACK)
        pyxel.rectb(ax_pos - 4, HEIGHT // 2 - 10, len(ann_txt) * 4 + 8, 14,
                    WEATHER_COLORS[weather])
        pyxel.text( ax_pos,     HEIGHT // 2 - 6,  ann_txt, WEATHER_COLORS[weather])

    # Score-Animationen
    for x, y, text, timer in score_anim:
        col = COL_YELLOW if "+" in text else COL_RED
        ay  = y - (35 - timer) // 4
        pyxel.text(x - len(text) * 2, ay, text, col)

    # Idee 4: Kettenreaktion-Banner
    if combo_announce > 0:
        ktxt  = "*** KETTENREAKTION! ***"
        kx    = WIDTH // 2 - len(ktxt) * 2
        alpha = min(combo_announce, 20)
        pyxel.rect( kx - 4, HEIGHT // 3 - 8, len(ktxt) * 4 + 8, 14, COL_BLACK)
        pyxel.rectb(kx - 4, HEIGHT // 3 - 8, len(ktxt) * 4 + 8, 14, COL_ORANGE)
        pyxel.rectb(kx - 5, HEIGHT // 3 - 9, len(ktxt) * 4 + 10, 16, COL_YELLOW)
        pyxel.text( kx,     HEIGHT // 3 - 4, ktxt, COL_YELLOW)
        pyxel.text( WIDTH // 2 - 28, HEIGHT // 3 + 8, "Alle Bomben weg!", COL_ORANGE)

    # Idee 1: Evolution-Banner
    if evo_announce > 0:
        etxt = "EVOLUTION: " + EVO_NAMES[evo_level] + "!"
        ex   = WIDTH // 2 - len(etxt) * 2
        ecl  = EVO_COLORS[evo_level]
        pyxel.rect( ex - 4, HEIGHT // 2 - 12, len(etxt) * 4 + 8, 14, COL_BLACK)
        pyxel.rectb(ex - 4, HEIGHT // 2 - 12, len(etxt) * 4 + 8, 14, ecl)
        pyxel.rectb(ex - 5, HEIGHT // 2 - 13, len(etxt) * 4 + 10, 16, COL_WHITE)
        pyxel.text( ex,     HEIGHT // 2 - 8,  etxt, ecl)
        # Erklärung der neuen Fähigkeit
        evo_descs = [
            "",
            "Korb ist groesser!",
            "Raeder: schneller!",
            "Netz: greift hoeher!",
            "Magnet: zieht Fruechte an!",
        ]
        desc = evo_descs[evo_level]
        pyxel.text(WIDTH // 2 - len(desc) * 2, HEIGHT // 2 + 4, desc, ecl)

    # Power-up Nachricht
    if powerup_msg_timer > 0:
        msg   = powerup_msg
        px    = WIDTH // 2 - len(msg) * 2
        pyxel.rect( px - 4, HEIGHT // 2 + 6, len(msg) * 4 + 8, 13, COL_BLACK)
        pyxel.rectb(px - 4, HEIGHT // 2 + 6, len(msg) * 4 + 8, 13, COL_YELLOW)
        pyxel.text( px,     HEIGHT // 2 + 10, msg, COL_YELLOW)

    # Power-up Balken (unten links)
    bars = [
        (basket.fast_timer,  COL_YELLOW, "SPD"),
        (basket.jump_timer,  COL_PINK,   "JMP"),
        (basket.small_timer, COL_RED,    "SML"),
        (basket.large_timer, COL_GREEN,  "LRG"),
    ]
    offset = 0
    for timer, col, label in bars:
        if timer > 0:
            bx  = 4 + offset * 50
            bby = HEIGHT - 22
            pyxel.text(bx, bby, label, col)
            w = int((timer / POWERUP_DURATION) * 44)
            pyxel.rect(bx, bby + 8, 44, 5, COL_DARK_GREY)
            pyxel.rect(bx, bby + 8, w,  5, col)
            pyxel.rectb(bx, bby + 8, 44, 5, COL_BLACK)
            offset += 1

    # Wetter-Timer-Balken (unten rechts)
    t_pct = int((weather_timer / WEATHER_DURATION) * 50)
    pyxel.text(WIDTH - 56, HEIGHT - 22, "Wetter", WEATHER_COLORS[weather])
    pyxel.rect( WIDTH - 56, HEIGHT - 13, 50, 5, COL_DARK_GREY)
    pyxel.rect( WIDTH - 56, HEIGHT - 13, t_pct, 5, WEATHER_COLORS[weather])
    pyxel.rectb(WIDTH - 56, HEIGHT - 13, 50, 5, COL_BLACK)


def draw_gameover():
    if flash > 10:
        pyxel.cls(flash_col)
        return

    pyxel.cls(COL_BLACK)

    # Schatten-Titel
    if bomb_hit:
        t1 = "BOOM!"
        t2 = "Du hast eine Bombe gefangen!"
    else:
        t1 = "GAME OVER"
        t2 = "Alle Leben verloren!"

    pyxel.text(WIDTH // 2 - len(t1) * 2 + 1, 23, t1, COL_DARK_GREY)
    pyxel.text(WIDTH // 2 - len(t1) * 2,     22, t1, COL_RED)
    pyxel.text(WIDTH // 2 - len(t2) * 2,     34, t2, COL_ORANGE)

    # Trennlinie
    pyxel.line(30, 46, WIDTH - 30, 46, COL_DARK_GREY)

    # Score gross
    score_text = "Endpunktestand:  " + str(score)
    pyxel.text(WIDTH // 2 - len(score_text) * 2, 52, score_text, COL_YELLOW)

    # Bewertung
    if score >= 50:
        rating, rcol = "LEGENDAER!!!", COL_YELLOW
    elif score >= 30:
        rating, rcol = "Hervorragend!", COL_GREEN
    elif score >= 15:
        rating, rcol = "Gut gemacht!", COL_LIGHT_BLUE
    elif score >= 5:
        rating, rcol = "Weiter ueben!", COL_GREY
    else:
        rating, rcol = "Nicht aufgeben!", COL_DARK_GREY

    pyxel.rect( WIDTH // 2 - len(rating) * 2 - 4, 62,
                len(rating) * 4 + 8, 11, COL_DARK_GREY)
    pyxel.text( WIDTH // 2 - len(rating) * 2, 65, rating, rcol)

    pyxel.line(30, 78, WIDTH - 30, 78, COL_DARK_GREY)

    # Wetter-Erinnerung
    pyxel.text(WIDTH // 2 - 52, 83, "Wetter-Erinnerung:", COL_WHITE)
    rows = [
        (COL_YELLOW,     "Sonnig  = Bonus-Punkte (Apfel+2, Melone+5)"),
        (COL_LIGHT_BLUE, "Regen   = Korb rutscht, Fruechte schneller"),
        (COL_PURPLE,     "Sturm   = Wind lenkt Korb und Fruechte ab"),
        (COL_ORANGE,     "Hitze   = Verpasste Bomben spawnen 2 neue!"),
        (COL_DARK_GREY,  "Nacht   = Nur Lichtkegel sichtbar"),
        (COL_RED,        "Herz    = alle 45 Sek: +1 Leben!"),
        (COL_ORANGE,     "Combo   = 3x gleiche Frucht: Bomben weg!"),
        (COL_PURPLE,     "Evo     = 10/25/50/80 Pkt: Korb entwickelt!"),
    ]
    for i, (rc, rt) in enumerate(rows):
        pyxel.text(20, 93 + i * 10, rt, rc)

    pyxel.line(30, 146, WIDTH - 30, 146, COL_DARK_GREY)

    if (pyxel.frame_count // 15) % 2 == 0:
        hint = "ENTER oder SPACE: Nochmal spielen"
        pyxel.text(WIDTH // 2 - len(hint) * 2, 154, hint, COL_WHITE)


# ── Start ──────────────────────────────────────────────────────────────────────

pyxel.init(WIDTH, HEIGHT, title="Fruit Catcher", fps=30)
init_game()
pyxel.run(update, draw)