diff --git a/cli.py b/cli.py index 43dc2ec..1fd27bb 100644 --- a/cli.py +++ b/cli.py @@ -1,5 +1,6 @@ import os import platform +import shutil """ ANSI color codes """ @@ -31,8 +32,8 @@ colors = { class Cli(): """Classe gérant l'interface graphique""" def __init__(self, **kwargs): - self.width = os.get_terminal_size().columns - self.height = os.get_terminal_size().lines -1 + self.width = shutil.get_terminal_size().columns + self.height = shutil.get_terminal_size().lines -1 for key, value in kwargs.items(): if key == 'width': self.width = value @@ -59,26 +60,26 @@ class Cli(): def display(self): """affiche le contenu de self.screen""" - os.system('clear') + self.__clear() for line in self.screen: print(''.join(line)) - self.screen = [[' ' for i in range(self.width)] for j in range(self.height)] + def draw(self, content, x, y, **kwargs): """dessine aux coordonées""" - for key, value in kwargs.items(): - if key == 'color': - try: - self.screen[y][x-1] = colors[value] - self.screen[y][x+len(str(content))] = colors['END'] - except: - pass for i in range(len(str(content))): try: if content[i] != ' ': self.screen[y][x+i] = content[i] except(IndexError): break + for key, value in kwargs.items(): + if key == 'color': + try: + self.screen[y][x] = colors[value] + self.screen[y][x] + self.screen[y][x+len(str(content))-1] += colors['END'] + except: + pass def draw_life(self, percent, x, y): """dessine la barre de vie""" @@ -87,22 +88,30 @@ class Cli(): if percent > 25: self.draw('|{}{}|'.format(''.join(['█' for i in range(part_to_draw)]), ''.join([' ' for i in range(length - part_to_draw)])), x, y) else: - self.draw('{}|{}{}|{}'.format(colors['RED'], ''.join(['█' for i in range(part_to_draw)]), ''.join([' ' for i in range(length - part_to_draw)]), colors['END']), x, y) + self.draw('|{}{}|'.format(''.join(['█' for i in range(part_to_draw)]), ''.join([' ' for i in range(length - part_to_draw)])), x, y, color='RED') + + def wipe(self): + """vide le contenu de self.screen""" + self.screen = [[' ' for i in range(self.width)] for j in range(self.height)] -class Layer(Cli): + +class Layer(): """defini une zone indépendante dans l'écran pour ne pas tout recharger""" - def __init__(self, x, y, shape): + def __init__(self, cli, x, y, shape): """shape doit être fournis sous forme de tableau""" - super().__init__() - + self.cli = cli + self.shape = shape self.x = x self.y = y - self.width = max(map(len, shape)) - self.height = len(shape) + self.shape_width = max(map(len, self.shape)) + self.shape_height = len(self.shape) def refresh(self): - pass - - -print(platform.system()) \ No newline at end of file + """efface seulement le contenu du calque""" + for i in range(self.shape_height): + for j in range(self.shape_width): + try: + self.cli.screen[self.y+i][self.x+j] = ' ' + except: + pass diff --git a/main.py b/main.py index 1c5ed1b..aa6a307 100644 --- a/main.py +++ b/main.py @@ -5,13 +5,25 @@ import time class Animation: """ class de gestion des animations""" - def __init__(self): - self.shape = ['', ''] - - def blink(self): - pass - - def display(self, x, y, side, **kwargs): + def __init__(self, x, y, shape): + self.x = x + self.y = y + self.shape = shape + + def blink(self, start_color, end_color): + self.layer = cli.Layer(gui, self.x, self.y, self.shape[0].split('\n')) + for i in range(8): + self.layer.refresh() + if i % 2 == 0: + self.display('right', color = start_color) + else: + self.display('right', color = end_color) + gui.display() + time.sleep(0.4) + del self.layer + + + def display(self, side, **kwargs): if side == 'right': cuted_shape = self.shape[0].split('\n') else: @@ -19,18 +31,21 @@ class Animation: for i in range(len(cuted_shape)): for key, value in kwargs.items(): if key == 'color': - gui.draw(cuted_shape[i], x, y + i, color=value) + gui.draw(cuted_shape[i], self.x, self.y + i, color=value) class Personnage(Animation): def __init__(self, nom): - super().__init__() self.nom = nom self.pdv = 2 self.max_pdv = 20 self.xp = 1 self.inv = ['potion'] - self.shape = '' + + self.shape = ['', ''] + self.x = 80 + self.y = 5 + super().__init__(self.x, self.y, self.shape) def jet_attaque(self): return random.randint(20) + self.xp * 2 @@ -82,11 +97,13 @@ gui = cli.Cli(width=120, height=36) guerrier = Guerrier('arthur') -for i in range(60): - gui.draw(guerrier.nom, 53, 31, color='GREEN') - gui.draw_life(round(guerrier.pdv / guerrier.max_pdv * 100), 30, 30) - Elfe('Legolas').display(110-i*2, 10, 'right', color='BLUE') +gui.wipe() +gui.draw(guerrier.nom, 53, 31, color='GREEN') +gui.draw_life(round(guerrier.pdv / guerrier.max_pdv * 100), 30, 30) +guerrier.display('right', color='BLUE') + +gui.display() - gui.display() +guerrier.blink('RED', 'WHITE') - time.sleep(0.1) \ No newline at end of file +gui.display() \ No newline at end of file