Browse Source

hotfixes

old
Kalyax 2 years ago
parent
commit
2cb2ca4e25
  1. 23
      charaters/personnage.py
  2. 0
      graphics/key_listener.py
  3. 41
      graphics/layers.py
  4. 49
      graphics/writer.py
  5. 19
      main.py

23
charaters/personnage.py

@ -1,6 +1,7 @@
from random import randint from random import randint
from enum import Enum
class StatsSet(): class StatsSet:
#HP, ATK%, DEF%, CRIT, CRITRATE%, XPCOEF #HP, ATK%, DEF%, CRIT, CRITRATE%, XPCOEF
def __init__(self, hp, atkP, defP, crit, critrateP, xpcoef): def __init__(self, hp, atkP, defP, crit, critrateP, xpcoef):
self.hp = hp self.hp = hp
@ -10,22 +11,20 @@ class StatsSet():
self.critrateP = critrateP self.critrateP = critrateP
self.xpcoef = xpcoef self.xpcoef = xpcoef
class ClassType:
class ClassType(Enum):
GUERRIER = StatsSet(20, 10, 12, 5, 10, 8) GUERRIER = StatsSet(20, 10, 12, 5, 10, 8)
MAGICIEN = StatsSet(16, 12, 6, 8, 15, 7) MAGICIEN = StatsSet(16, 12, 6, 8, 15, 7)
#VOLEUR = #TODO: Changer stats
#ELF = VOLEUR = StatsSet(16, 12, 6, 8, 15, 7)
@staticmethod ELF = StatsSet(16, 12, 6, 8, 15, 7)
def class_name(class_set):
if class_set == ClassType.GUERRIER:
return "GUERRIER"
elif class_set == ClassType.MAGICIEN:
return "MAGICIEN"
class Personnage: class Personnage:
def __init__(self, nom, class_type): def __init__(self, nom, class_type):
self.nom = nom self.nom = nom
self.class_type = class_type self.class_name = class_type.name
self.class_type = class_type.value
self.__hp = self.class_type.hp self.__hp = self.class_type.hp
self.stats = StatsSet(0, 0, 0, 0, 0, 0) self.stats = StatsSet(0, 0, 0, 0, 0, 0)
@ -61,7 +60,7 @@ class Personnage:
def affiche_caracteristiques(self): def affiche_caracteristiques(self):
return (f"Nom: {self.nom}", return (f"Nom: {self.nom}",
f"Type de classe: {ClassType.class_name(self.class_type)}", f"Type de classe: {self.class_name}",
f"Vie: {self.__hp}", f"Vie: {self.__hp}",
f"Expérience: {self.__xp}", f"Expérience: {self.__xp}",
"Stats (classe + personnage):", "Stats (classe + personnage):",

0
listener.py → graphics/key_listener.py

41
graphics/layers.py

@ -1,3 +1,5 @@
from charaters.personnage import Personnage, ClassType
from graphics.colors import Colors from graphics.colors import Colors
from graphics.writer import Layer from graphics.writer import Layer
@ -5,15 +7,16 @@ from getkey import keys
from re import match from re import match
class GUI(Layer): class GUI(Layer):
def __init__(self, screen, z_index, buttons, personnage): def __init__(self, screen, z_index, buttons):
super().__init__(screen, z_index) super().__init__(screen, z_index, "gui")
screen.add_key_handler(self.key_handler)
self.buttons = buttons self.buttons = buttons
self.current = 0 self.current = 0
self.__personnage = personnage self.handle_keys = False
self.personnage = Personnage(" ", ClassType.GUERRIER)
def draw(self): def draw(self):
super().draw()
x = 1 x = 1
for button in self.buttons: for button in self.buttons:
color = (Colors.WHITEBG, Colors.BLACK) color = (Colors.WHITEBG, Colors.BLACK)
@ -22,7 +25,7 @@ class GUI(Layer):
self.put_string(button, x, self.y-10, color) self.put_string(button, x, self.y-10, color)
x += len(button)+1 x += len(button)+1
stats = self.__personnage.affiche_caracteristiques() stats = self.personnage.affiche_caracteristiques()
for i in range(4): for i in range(4):
self.put_string(stats[i], 0, self.y-8+i, [Colors.RED2]) self.put_string(stats[i], 0, self.y-8+i, [Colors.RED2])
for i in range(7): for i in range(7):
@ -36,30 +39,32 @@ class GUI(Layer):
elif key == keys.LEFT and self.current > 0: elif key == keys.LEFT and self.current > 0:
self.current -= 1 self.current -= 1
class PopUp(Layer): class PopUp(Layer):
def __init__(self, screen, z_index): def __init__(self, screen, z_index):
super().__init__(screen, z_index) super().__init__(screen, z_index, "popup")
screen.add_key_handler(self.key_handler) #screen.add_key_handler(self.key_handler)
self.classes = ["GUERRIER", "MAGICIEN", "VOLEUR", "ELF"] self.__classes = ["GUERRIER", "MAGICIEN", "VOLEUR", "ELF"]
self.choosen_class = 0 self.__choosen_class = 0
self.name = "" self.__username = ""
def draw(self): def draw(self):
super().draw()
#bg #bg
self.rect(self.x//3, self.y//6, self.x//3 + self.x//4, self.y//4 + self.y//3, Colors.WHITEBG) self.rect(self.x//3, self.y//6, self.x//3 + self.x//4, self.y//4 + self.y//3, Colors.WHITEBG)
#name #name
self.put_string("Nom: [A-z]", self.x//3 + 1, self.y//6 + 1, [Colors.WHITEBG, Colors.BLACK]) self.put_string("Nom: [A-z]", self.x//3 + 1, self.y//6 + 1, [Colors.WHITEBG, Colors.BLACK])
self.rect(self.x//3 + 1, self.y//6 + 2, self.x//3 + self.x//4 - 1, self.y//6 + 2, Colors.BLACKBG) self.rect(self.x//3 + 1, self.y//6 + 2, self.x//3 + self.x//4 - 1, self.y//6 + 2, Colors.BLACKBG)
self.put_string(self.name, self.x//3 + 1, self.y//6 + 2, [Colors.WHITE, Colors.BLACKBG]) self.put_string(self.__username, self.x//3 + 1, self.y//6 + 2, [Colors.WHITE, Colors.BLACKBG])
#Classes #Classes
self.put_string("Classe perso.: SHIFT+[1-4]", self.x//3 + 1, self.y//6 + 4, [Colors.WHITEBG, Colors.BLACK]) self.put_string("Classe perso.: SHIFT+[1-4]", self.x//3 + 1, self.y//6 + 4, [Colors.WHITEBG, Colors.BLACK])
y = 0 y = 0
for user_class in self.classes: for user_class in self.__classes:
colors = [Colors.GREYBG, Colors.WHITE] colors = [Colors.GREYBG, Colors.WHITE]
if y == self.choosen_class: if y == self.__choosen_class:
colors = [Colors.REDBG, Colors.BLACK] colors = [Colors.REDBG, Colors.BLACK]
self.put_string(user_class, self.x//3 + 2, self.y//6 + 5 + y, colors) self.put_string(user_class, self.x//3 + 2, self.y//6 + 5 + y, colors)
y += 1 y += 1
@ -70,10 +75,12 @@ class PopUp(Layer):
def key_handler(self, key): def key_handler(self, key):
if match("[a-zA-Z]", key): if match("[a-zA-Z]", key):
self.name += key self.__username += key
elif key == keys.BACKSPACE: elif key == keys.BACKSPACE:
self.name = self.name[:-1] self.__username = self.__username[:-1]
elif match("[1-4]", key): elif match("[1-4]", key):
self.choosen_class = int(key)-1 self.__choosen_class = int(key)-1
elif key == keys.TAB: elif key == keys.TAB:
print("TAB") self.screen.get_layer("gui").handle_keys = True
self.screen.del_layer("popup")
self.screen.get_layer("gui").personnage = Personnage(self.__username, ClassType[self.__classes[self.__choosen_class]])

49
graphics/writer.py

@ -2,14 +2,16 @@ import os
from graphics.colors import Colors from graphics.colors import Colors
class Layer: class Layer:
def __init__(self, screen, z_index): def __init__(self, screen, z_index, name):
self.name = name
self.frame = {} self.frame = {}
self.z_index = z_index self.z_index = z_index
self.x = screen.x self.x = screen.x
self.y = screen.y self.y = screen.y
self.screen = screen
self.handle_keys = True
def clear(self): screen.set_layer(self)
self.frame = {}
def put_char(self, char, x, y, colors=[]): def put_char(self, char, x, y, colors=[]):
if x > self.x or x < 0 or y > self.y or y < 0: if x > self.x or x < 0 or y > self.y or y < 0:
@ -33,8 +35,15 @@ class Layer:
for y in range(y1, y2+1): for y in range(y1, y2+1):
self.frame[(x, y)] = "".join(colors) + template + Colors.RESET self.frame[(x, y)] = "".join(colors) + template + Colors.RESET
def draw(self):
self.frame = {}
def key_handler(self):
pass
class Screen: class Screen:
"""Moteur graphique basé sur des claques"""
def __init__(self): def __init__(self):
size = os.get_terminal_size() size = os.get_terminal_size()
self.x = size.columns self.x = size.columns
@ -44,33 +53,39 @@ class Screen:
self.handlers = [] self.handlers = []
def set_layer(self, name, layer: Layer): def set_layer(self, layer):
self.__layers[name] = layer self.__layers[layer.name] = layer
def del_layer(self, name):
del self.__layers[name]
def get_layer(self, name):
if not name in self.__layers.keys():
raise ValueError("unknown key")
return self.__layers[name]
def __combine_layers(self): def __combine_layers(self):
self.frame = {} self.frame = {}
layers = sorted(self.__layers.values(), key=lambda layer: layer.z_index) layers = sorted(self.__layers.values(), key=lambda layer: layer.z_index)
for layer in layers: for layer in layers:
layer.draw()
self.frame.update(layer.frame) self.frame.update(layer.frame)
def draw(self): def draw(self):
self.__combine_layers() self.__combine_layers()
content = "" buffer = ""
for y in range(self.y): for y in range(self.y):
for x in range(self.x): for x in range(self.x):
if (x, y) in self.frame: if (x, y) in self.frame:
content += self.frame[(x, y)] buffer += self.frame[(x, y)]
else: else:
content += " " buffer += " "
content += "\n" buffer += "\n"
print("\033[H\033[J", end="") print("\033[H\033[J", end="")
print(content) print(buffer)
def add_key_handler(self, handler):
self.handlers.append(handler)
def send_key(self, key): def send_key(self, key):
for handler in self.handlers: """Envoie les touches reçues à tous les calques actifs"""
handler(key) for layer in list(self.__layers.values()):
if layer.handle_keys == True:
layer.key_handler(key)

19
main.py

@ -1,24 +1,19 @@
from charaters.personnage import Personnage, ClassType from charaters.personnage import Personnage, ClassType
import graphics.layers as layers import graphics.layers as layers
from graphics.writer import * from graphics.writer import Screen
import listener import graphics.key_listener as listener
from time import sleep from time import sleep
personnage = Personnage("Pierre", ClassType.GUERRIER) if __name__ == "__main__":
#Initialise la partie graphique
screen = Screen() screen = Screen()
gui = layers.GUI(screen, 2, ["Attaquer", "Inventaire"], personnage) layers.PopUp(screen, 1)
popup = layers.PopUp(screen, 1) layers.GUI(screen, 2, ["Attaquer", "Inventaire"])
def draw():
screen.set_layer("gui", gui.draw())
screen.set_layer("popup", popup.draw())
if __name__ == "__main__":
listener.build_thread(screen).start() listener.build_thread(screen).start()
while True: while True:
draw()
screen.draw() screen.draw()
sleep(0.4) sleep(0.4)
Loading…
Cancel
Save