Browse Source

Layer fonctionellent

master
BARRAUX Arthur 2 years ago
parent
commit
59cdf71d6e
  1. 51
      cli.py
  2. 39
      main.py

51
cli.py

@ -1,5 +1,6 @@
import os import os
import platform import platform
import shutil
""" ANSI color codes """ """ ANSI color codes """
@ -31,8 +32,8 @@ colors = {
class Cli(): class Cli():
"""Classe gérant l'interface graphique""" """Classe gérant l'interface graphique"""
def __init__(self, **kwargs): def __init__(self, **kwargs):
self.width = os.get_terminal_size().columns self.width = shutil.get_terminal_size().columns
self.height = os.get_terminal_size().lines -1 self.height = shutil.get_terminal_size().lines -1
for key, value in kwargs.items(): for key, value in kwargs.items():
if key == 'width': if key == 'width':
self.width = value self.width = value
@ -59,26 +60,26 @@ class Cli():
def display(self): def display(self):
"""affiche le contenu de self.screen""" """affiche le contenu de self.screen"""
os.system('clear') self.__clear()
for line in self.screen: for line in self.screen:
print(''.join(line)) print(''.join(line))
self.screen = [[' ' for i in range(self.width)] for j in range(self.height)]
def draw(self, content, x, y, **kwargs): def draw(self, content, x, y, **kwargs):
"""dessine aux coordonées""" """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))): for i in range(len(str(content))):
try: try:
if content[i] != ' ': if content[i] != ' ':
self.screen[y][x+i] = content[i] self.screen[y][x+i] = content[i]
except(IndexError): except(IndexError):
break 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): def draw_life(self, percent, x, y):
"""dessine la barre de vie""" """dessine la barre de vie"""
@ -87,22 +88,30 @@ class Cli():
if percent > 25: 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) self.draw('|{}{}|'.format(''.join(['' for i in range(part_to_draw)]), ''.join([' ' for i in range(length - part_to_draw)])), x, y)
else: 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""" """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""" """shape doit être fournis sous forme de tableau"""
super().__init__() self.cli = cli
self.shape = shape
self.x = x self.x = x
self.y = y self.y = y
self.width = max(map(len, shape)) self.shape_width = max(map(len, self.shape))
self.height = len(shape) self.shape_height = len(self.shape)
def refresh(self): def refresh(self):
"""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 pass
print(platform.system())

39
main.py

@ -5,13 +5,25 @@ import time
class Animation: class Animation:
""" class de gestion des animations""" """ class de gestion des animations"""
def __init__(self): def __init__(self, x, y, shape):
self.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 blink(self):
pass
def display(self, x, y, side, **kwargs): def display(self, side, **kwargs):
if side == 'right': if side == 'right':
cuted_shape = self.shape[0].split('\n') cuted_shape = self.shape[0].split('\n')
else: else:
@ -19,18 +31,21 @@ class Animation:
for i in range(len(cuted_shape)): for i in range(len(cuted_shape)):
for key, value in kwargs.items(): for key, value in kwargs.items():
if key == 'color': 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): class Personnage(Animation):
def __init__(self, nom): def __init__(self, nom):
super().__init__()
self.nom = nom self.nom = nom
self.pdv = 2 self.pdv = 2
self.max_pdv = 20 self.max_pdv = 20
self.xp = 1 self.xp = 1
self.inv = ['potion'] 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): def jet_attaque(self):
return random.randint(20) + self.xp * 2 return random.randint(20) + self.xp * 2
@ -82,11 +97,13 @@ gui = cli.Cli(width=120, height=36)
guerrier = Guerrier('arthur') guerrier = Guerrier('arthur')
for i in range(60): gui.wipe()
gui.draw(guerrier.nom, 53, 31, color='GREEN') gui.draw(guerrier.nom, 53, 31, color='GREEN')
gui.draw_life(round(guerrier.pdv / guerrier.max_pdv * 100), 30, 30) gui.draw_life(round(guerrier.pdv / guerrier.max_pdv * 100), 30, 30)
Elfe('Legolas').display(110-i*2, 10, 'right', color='BLUE') guerrier.display('right', color='BLUE')
gui.display() gui.display()
time.sleep(0.1) guerrier.blink('RED', 'WHITE')
gui.display()
Loading…
Cancel
Save