• [^] # Re: python et lib de graph

    Posté par (Mastodon) . En réponse au message Avent du Code, jour 12. Évalué à 3.

    Pour faire du joli on fait ça :

    class display:
     def __init__(self, input, width, heights):
     self.heights = heights
     self.bgcolors = [[
     min(255, max(232, heights[c] + 231))
     for c in line] for line in input]
     self.background = "\n".join(
     "".join(
     f"{self.color(c)} "
     for c in line
     )
     for line in self.bgcolors
     )
     self.bgcolors = sum(self.bgcolors, [])
     self.width = width
     def color(self, bg=None, fg=None, rgb=None):
     bg = f"033円[48;5;{bg}m" if bg else ""
     fg = f"033円[38;5;{fg}m" if fg else ""
     fg = f"033円[38;2;{rgb[0]};{rgb[1]};{rgb[2]}m" if rgb else fg
     return f"{bg}{fg}"
     def cursor(self, row=0, col=0):
     return f"033円[{row};{col}H"
     def __call__(self, *visited):
     print(self.cursor(), end='')
     print(self.background)
     for v in visited:
     print(f"{self.cursor(*divmod(v, self.width))}{self.color(bg=self.bgcolors[v], fg=160)}o")
     time.sleep(.1)
    # On ajoute ces deux lignes juste avant la troisième :
    d = display(input, w, heights)
    d()
    input = "".join(input)
    # Et enfin en dernière ligne de main():
     d(*v1, *v2)
    # Et pour éviter d'avoir les résultats en tout moche au milieu :
    s1 = main([start], [end])
    s2 = main([pos for pos, height in enumerate(map) if height == 1], [end])
    print(d.cursor(row=h+1), end='')
    print("033円[0m", end='')
    print(f"Path found in {s1} moves !")
    print(f"Path found in {s2} moves !")

    Et il faut mettre le terminal en grand, parce que sinon ça va faire très très moche et buggé !
    On a une animation des cases en cours de visite, sur fond de montagne enneigée (dégradé noir en bas, et blanc en haut).

    • Yth.