import pyxel

class SchoolEscape:

    def __init__(self):
        pyxel.init(160, 120, title="School Escape")
        pyxel.load("res.pyxres")

        # Spieler Position
        self.p1_x = 20
        self.p1_y = 90
        self.p2_x = 40
        self.p2_y = 90

        # Geschwindigkeit
        self.p1_vy = 0
        self.p2_vy = 0

        self.player_w = 8
        self.player_h = 16

        # Physik
        self.gravity = 0.5
        self.jump = -6
        self.ground = 100

        # Kamera
        self.camera_x = 0
        self.level_width = 500

        # Checkpoint
        self.checkpoint_x = 20
        self.checkpoint_y = 90

        # Ziel
        self.exit_x = 450
        self.exit_y = 90

        # Plattformen (x, y, breite, höhe)
        self.platforms = [
            (60, 90, 40, 5),
            (120, 80, 30, 5),
            (180, 70, 30, 5),
            (240, 85, 40, 5),
            (320, 75, 30, 5),
            (380, 90, 40, 5),
        ]

        # Animation
        self.p1_frame = 0
        self.p1_dir = 1
        self.p1_anim_timer = 0

        self.p2_frame = 0
        self.p2_dir = 1
        self.p2_anim_timer = 0

        pyxel.run(self.update, self.draw)

    # ---------------- Plattform Kollision ----------------
    def collide_platform(self, x, y, vy):
        for px, py, pw, ph in self.platforms:
            if (x + self.player_w > px and
                x < px + pw and
                y + self.player_h >= py and
                y + self.player_h <= py + ph and
                vy >= 0):
                return py - self.player_h, 0
        return y, vy

    # ---------------- Spieler auf Spieler ----------------
    def collide_players(self):
        # Player 1 steht auf Player 2
        if (self.p1_x + self.player_w > self.p2_x and
            self.p1_x < self.p2_x + self.player_w and
            self.p1_y + self.player_h >= self.p2_y and
            self.p1_y + self.player_h <= self.p2_y + 3 and
            self.p1_vy >= 0):
            self.p1_y = self.p2_y - self.player_h
            self.p1_vy = 0

        # Player 2 steht auf Player 1
        if (self.p2_x + self.player_w > self.p1_x and
            self.p2_x < self.p1_x + self.player_w and
            self.p2_y + self.player_h >= self.p1_y and
            self.p2_y + self.player_h <= self.p1_y + 3 and
            self.p2_vy >= 0):
            self.p2_y = self.p1_y - self.player_h
            self.p2_vy = 0

    # ---------------- UPDATE ----------------
    def update(self):

        # -------- Player 1 Steuerung --------
        moving_p1 = False

        if pyxel.btn(pyxel.KEY_A):
            self.p1_x -= 2
            self.p1_dir = -1
            moving_p1 = True

        if pyxel.btn(pyxel.KEY_D):
            self.p1_x += 2
            self.p1_dir = 1
            moving_p1 = True

        if pyxel.btnp(pyxel.KEY_W) and self.p1_vy == 0:
            self.p1_vy = self.jump

        # Animation Player 1
        if moving_p1:
            self.p1_anim_timer += 1
            if self.p1_anim_timer % 5 == 0:
                self.p1_frame = (self.p1_frame + 1) % 4
        else:
            self.p1_frame = 0

        # -------- Player 2 Steuerung --------
        moving_p2 = False

        if pyxel.btn(pyxel.KEY_LEFT):
            self.p2_x -= 2
            self.p2_dir = -1
            moving_p2 = True

        if pyxel.btn(pyxel.KEY_RIGHT):
            self.p2_x += 2
            self.p2_dir = 1
            moving_p2 = True

        if pyxel.btnp(pyxel.KEY_UP) and self.p2_vy == 0:
            self.p2_vy = self.jump

        # Animation Player 2
        if moving_p2:
            self.p2_anim_timer += 1
            if self.p2_anim_timer % 5 == 0:
                self.p2_frame = (self.p2_frame + 1) % 4
        else:
            self.p2_frame = 0

        # -------- Gravitation --------
        self.p1_vy += self.gravity
        self.p1_y += self.p1_vy

        self.p2_vy += self.gravity
        self.p2_y += self.p2_vy

        # -------- Boden --------
        if self.p1_y > self.ground:
            self.p1_y = self.ground
            self.p1_vy = 0

        if self.p2_y > self.ground:
            self.p2_y = self.ground
            self.p2_vy = 0

        # -------- Plattform Kollision --------
        self.p1_y, self.p1_vy = self.collide_platform(self.p1_x, self.p1_y, self.p1_vy)
        self.p2_y, self.p2_vy = self.collide_platform(self.p2_x, self.p2_y, self.p2_vy)

        # -------- Spieler Kollision --------
        self.collide_players()

        # -------- Checkpoint --------
        if abs(self.p1_x - 80) < 5 and abs(self.p2_x - 80) < 5:
            self.checkpoint_x = 80

        # -------- Respawn --------
        if self.p1_y > 120:
            self.p1_x = self.checkpoint_x
            self.p1_y = self.checkpoint_y

        if self.p2_y > 120:
            self.p2_x = self.checkpoint_x + 10
            self.p2_y = self.checkpoint_y

        # -------- Win --------
        if abs(self.p1_x - self.exit_x) < 5 and abs(self.p2_x - self.exit_x) < 5:
            pyxel.quit()

        # -------- Kamera --------
        center = (self.p1_x + self.p2_x) / 2
        self.camera_x = center - 80

        if self.camera_x < 0:
            self.camera_x = 0
        if self.camera_x > self.level_width - 160:
            self.camera_x = self.level_width - 160

    # ---------------- DRAW ----------------
    def draw(self):

        pyxel.cls(1)

        # Hintergrund – Tilemap – nur diese 4 Werte anpassen:
        BG_TM = 0       # Tilemap-Nummer (0, 1 oder 2)
        BG_U = 0        # X-Kachel-Offset in der Tilemap
        BG_V = 64       # Y-Kachel-Offset in der Tilemap
        BG_W = 160      # Breite in Pixeln
        BG_H = 128      # Höhe in Pixeln
        pyxel.bltm(0, 0, BG_TM, BG_U, BG_V, BG_W, BG_H)

        # Boden
        pyxel.rect(0, 108, 160, 20, 3)

        # Plattformen
        for px, py, pw, ph in self.platforms:
            pyxel.rect(px - self.camera_x, py, pw, ph, 5)

        # Checkpoint
        pyxel.rect(78 - self.camera_x, 95, 4, 5, 11)

        # Exit
        pyxel.rect(self.exit_x - self.camera_x, self.exit_y, 8, 10, 8)

        # Player 1
        u1 = self.p1_frame * 16
        v1 = 16 if self.p1_dir == 1 else 32
        pyxel.blt(self.p1_x - self.camera_x, self.p1_y, 0, u1, v1, 16, 16, 0)

        # Player 2
        u2 = self.p2_frame * 16
        v2 = 16 if self.p2_dir == 1 else 32
        pyxel.blt(self.p2_x - self.camera_x, self.p2_y, 1, u2, v2, 16, 16, 0)

        # UI
        pyxel.text(5, 5, "P1: WASD", 7)
        pyxel.text(5, 12, "P2: ARROWS", 7)


SchoolEscape()