Colorama stretching when CMD resized

23 Views Asked by At

I am working on a CMD 2048 and I added colorama to make it look better however when resizing the command prompt window the last color fills in the rest of the line. I have some images attached: Problamatic Original

Could someone tell me what I am doing wrong?

from colorama import init
init(autoreset=True)
board = [[2,4,8,16],
         [256,128,64,32],
         [512,1024,2048,1024],
         [512,256,128,64]]

backgrounds = {-1:"cdc1b4",
           2:"#eee4da",
           4:"#ede0c8",
           8:"#f2b179",
           16:"#f59563",
           32:"#f67c5f",
           64:"#f65e3b",
           128:"#edcf72",
           256:"#edcc61",
           512:"#edc850",
           1024:"#edc53f",
           2048:"#edc22e"}

textColour = {-1:"cdc1b4",
          2:"#776e65",
          4:"#776e65",
          8:"#f9f6f2",
          16:"#f9f6f2",
          32:"#f9f6f2",
          64:"#f9f6f2",
          128:"#f9f6f2",
          256:"#f9f6f2",
          512:"#f9f6f2",
          1024:"#f9f6f2",
          2048:"#f9f6f2"}
def strN(n):
    return f"{n:<4}"

def hexToANSI(h, f=False):
    h=h.replace("#","")
    if len(h) != 6:
        raise SyntaxError(f"This hex code is not 6 letters long: {h}")
    r = int(h[0:2], 16)
    g = int(h[2:4], 16)
    b = int(h[4:6], 16)
    if (f == False):
        return f"\x1b[48;2;{r};{g};{b}m"
    else:
        return f"\x1b[38;2;{r};{g};{b}m"
def printBoard(array=None):
    for j in range(0, 4):
        n = strN(board[j][0]) + strN(board[j][1]) + strN(board[j][2]) + strN(board[j][3]) 
        n = n.replace("-1", " ")
        for i in range(0, len(n), 4):
            t = n[i:i+4]
            f = hexToANSI(backgrounds[int(t)]) + hexToANSI(textColour[int(t)], True) + t
            print(f, end="")
        print("\x1b[48;2;0;0;0m ")
printBoard()  
0

There are 0 best solutions below