import pyxel


class App:
    def __init__(self):
        pyxel.init(200, 150, title="Pyxel Triangle API")

        self.triangles = [(100, 24, 7, 143, 193, 143, 7)]

        pyxel.cls(13)
        pyxel.text(6, 6, "tri(x1,y1,x2,y2,x3,y3,col)", 7)
        pyxel.text(6, 14, "trib(x1,y1,x2,y2,x3,y3,col)", 7)

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

    def update(self):
        if pyxel.btnp(pyxel.KEY_Q):
            pyxel.quit()

    def draw(self):
        if self.triangles:
            triangle = self.triangles.pop(0)
            self.draw_triangle(*triangle)

    def draw_triangle(self, x1, y1, x2, y2, x3, y3, n):
        if n == 0:
            return

        col = n + 7
        if n % 2 == 0:
            pyxel.tri(x1, y1, x2, y2, x3, y3, col)
        else:
            pyxel.trib(x1, y1, x2, y2, x3, y3, col)

        mx1 = (x1 + x2) / 2
        my1 = (y1 + y2) / 2
        mx2 = (x2 + x3) / 2
        my2 = (y2 + y3) / 2
        mx3 = (x3 + x1) / 2
        my3 = (y3 + y1) / 2

        self.triangles.append((x1, y1, mx1, my1, mx3, my3, n - 1))
        self.triangles.append((mx1, my1, x2, y2, mx2, my2, n - 1))
        self.triangles.append((mx3, my3, mx2, my2, x3, y3, n - 1))


App()
