Browse Source

key_handler et popup; TODO: draw_handler

old
Kalyax 2 years ago
parent
commit
47179b6969
  1. 29
      charaters/personnage.py
  2. 20
      graphics/layer/menu.py
  3. 79
      graphics/layers.py
  4. 21
      graphics/writer.py
  5. 9
      listener.py
  6. 32
      main.py

29
charaters/personnage.py

@ -15,6 +15,12 @@ class ClassType:
MAGICIEN = StatsSet(16, 12, 6, 8, 15, 7) MAGICIEN = StatsSet(16, 12, 6, 8, 15, 7)
#VOLEUR = #VOLEUR =
#ELF = #ELF =
@staticmethod
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):
@ -54,14 +60,15 @@ class Personnage:
raise ValueError("nb_exp attends un nombre positif") raise ValueError("nb_exp attends un nombre positif")
def affiche_caracteristiques(self): def affiche_caracteristiques(self):
print(f"Nom: {self.nom}") return (f"Nom: {self.nom}",
print(f"Type de classe: {self.class_type.name}") f"Type de classe: {ClassType.class_name(self.class_type)}",
print(f"Vie: {self.__hp}") f"Vie: {self.__hp}",
print(f"Expérience: {self.__xp}") f"Expérience: {self.__xp}",
print("Stats (classe + personnage):") "Stats (classe + personnage):",
print(f"- HP: {self.class_type.hp} + {self.stats.hp}") f"- HP: {self.class_type.hp} + {self.stats.hp}",
print(f"- ATK%: {self.class_type.atkP} + {self.stats.atkP}") f"- ATK%: {self.class_type.atkP} + {self.stats.atkP}",
print(f"- DEF%: {self.class_type.defP} + {self.stats.defP}") f"- DEF%: {self.class_type.defP} + {self.stats.defP}",
print(f"- CRIT: {self.class_type.crit} + {self.stats.crit}") f"- CRIT: {self.class_type.crit} + {self.stats.crit}",
print(f"- CRITRATE%: {self.class_type.critrateP} + {self.stats.critrateP}") f"- CRITRATE%: {self.class_type.critrateP} + {self.stats.critrateP}",
print(f"- XPCOEF: {self.class_type.xpcoef} + {self.stats.xpcoef}") f"- XPCOEF: {self.class_type.xpcoef} + {self.stats.xpcoef}"
)

20
graphics/layer/menu.py

@ -1,20 +0,0 @@
from graphics.colors import Colors
from graphics.writer import Layer
class Menu:
def __init__(self, cli, buttons):
self.cli = cli
#self.stats = stats
self.buttons = buttons
self.current = 0
def draw_layer(self):
layer = Layer(self.cli, 1)
x = 1
for button in self.buttons:
color = (Colors.WHITEBG, Colors.BLACK)
if self.buttons[self.current] == button:
color = (Colors.REDBG, Colors.BLACK)
layer.put_string(button, x, self.cli.y-10, color)
x += 11
return layer

79
graphics/layers.py

@ -0,0 +1,79 @@
from graphics.colors import Colors
from graphics.writer import Layer
from getkey import keys
from re import match
class GUI(Layer):
def __init__(self, screen, z_index, buttons, personnage):
super().__init__(screen, z_index)
screen.add_key_handler(self.key_handler)
self.buttons = buttons
self.current = 0
self.__personnage = personnage
def draw(self):
x = 1
for button in self.buttons:
color = (Colors.WHITEBG, Colors.BLACK)
if self.buttons[self.current] == button:
color = (Colors.REDBG, Colors.BLACK)
self.put_string(button, x, self.y-10, color)
x += len(button)+1
stats = self.__personnage.affiche_caracteristiques()
for i in range(4):
self.put_string(stats[i], 0, self.y-8+i, [Colors.RED2])
for i in range(7):
self.put_string(stats[i+4], 40, self.y-8+i, [Colors.RED2])
return self
def key_handler(self, key):
if key == keys.RIGHT and self.current < len(self.buttons)-1:
self.current += 1
elif key == keys.LEFT and self.current > 0:
self.current -= 1
class PopUp(Layer):
def __init__(self, screen, z_index):
super().__init__(screen, z_index)
screen.add_key_handler(self.key_handler)
self.classes = ["GUERRIER", "MAGICIEN", "VOLEUR", "ELF"]
self.choosen_class = 0
self.name = ""
def draw(self):
#bg
self.rect(self.x//3, self.y//6, self.x//3 + self.x//4, self.y//4 + self.y//3, Colors.WHITEBG)
#name
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.put_string(self.name, self.x//3 + 1, self.y//6 + 2, [Colors.WHITE, Colors.BLACKBG])
#Classes
self.put_string("Classe perso.: SHIFT+[1-4]", self.x//3 + 1, self.y//6 + 4, [Colors.WHITEBG, Colors.BLACK])
y = 0
for user_class in self.classes:
colors = [Colors.GREYBG, Colors.WHITE]
if y == self.choosen_class:
colors = [Colors.REDBG, Colors.BLACK]
self.put_string(user_class, self.x//3 + 2, self.y//6 + 5 + y, colors)
y += 1
self.put_string("TAB pour confirmer", self.x//3 + 1, self.y//4 + self.y//3 -1, [Colors.WHITEBG, Colors.BLACK])
return self
def key_handler(self, key):
if match("[a-zA-Z]", key):
self.name += key
elif key == keys.BACKSPACE:
self.name = self.name[:-1]
elif match("[1-4]", key):
self.choosen_class = int(key)-1
elif key == keys.TAB:
print("TAB")

21
graphics/writer.py

@ -1,5 +1,4 @@
import os import os
from typing import List
from graphics.colors import Colors from graphics.colors import Colors
class Layer: class Layer:
@ -12,12 +11,12 @@ class Layer:
def clear(self): def clear(self):
self.frame = {} self.frame = {}
def put_char(self, char, x, y, colors: List[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:
raise ValueError("out of range pixel") raise ValueError("out of range pixel")
self.frame[(x, y)] = "".join(colors) + char + Colors.RESET self.frame[(x, y)] = "".join(colors) + char + Colors.RESET
def put_string(self, string, x, y, colors: List[Colors]): def put_string(self, string, 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:
raise ValueError("out of range pixel") raise ValueError("out of range pixel")
string = string.split("\n") string = string.split("\n")
@ -25,7 +24,7 @@ class Layer:
for string_x in range(len(string[string_y])): for string_x in range(len(string[string_y])):
self.frame[(x+string_x, y+string_y)] = "".join(colors) + string[string_y][string_x] + Colors.RESET self.frame[(x+string_x, y+string_y)] = "".join(colors) + string[string_y][string_x] + Colors.RESET
def rect(self, x1, y1, x2, y2, colors: List[Colors], template=' '): def rect(self, x1, y1, x2, y2, colors=[], template=' '):
if (x1 > self.x or x1 < 0 or y1 > self.y or y1 < 0) or (x2 > self.x or x2 < 0 or y2 > self.y or y2 < 0): if (x1 > self.x or x1 < 0 or y1 > self.y or y1 < 0) or (x2 > self.x or x2 < 0 or y2 > self.y or y2 < 0):
raise ValueError("out of range pixel") raise ValueError("out of range pixel")
if len(template) > 1: if len(template) > 1:
@ -43,6 +42,8 @@ class Screen:
self.frame = {} self.frame = {}
self.__layers = {} self.__layers = {}
self.handlers = []
def set_layer(self, name, layer: Layer): def set_layer(self, name, layer: Layer):
self.__layers[name] = layer self.__layers[name] = layer
@ -56,7 +57,6 @@ class Screen:
self.__combine_layers() self.__combine_layers()
content = "" content = ""
for y in range(self.y): for y in range(self.y):
#content += "\n"
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)] content += self.frame[(x, y)]
@ -64,4 +64,13 @@ class Screen:
content += " " content += " "
content += "\n" content += "\n"
print("\033[H\033[J", end="") print("\033[H\033[J", end="")
print(content) print(content)
def add_key_handler(self, handler):
self.handlers.append(handler)
def send_key(self, key):
for handler in self.handlers:
handler(key)

9
listener.py

@ -1,11 +1,10 @@
from threading import Thread from threading import Thread
from getkey import getkey from getkey import getkey
from main import key_handler def __listener(screen):
def __listener():
while True: while True:
key = getkey() key = getkey()
key_handler(key) screen.send_key(key)
key_listener = Thread(target=__listener) def build_thread(screen):
return Thread(target=__listener, args=(screen,))

32
main.py

@ -1,32 +1,24 @@
from charaters.personnage import Personnage from charaters.personnage import Personnage, ClassType
from graphics.colors import Colors import graphics.layers as layers
from graphics.layer.menu import Menu
from graphics.writer import * from graphics.writer import *
import listener
from getkey import keys
from time import sleep from time import sleep
game = Screen() personnage = Personnage("Pierre", ClassType.GUERRIER)
menu = Menu(game, ["Inventaire", "Teset"])
def key_handler(key): screen = Screen()
print(key) gui = layers.GUI(screen, 2, ["Attaquer", "Inventaire"], personnage)
if key == keys.RIGHT and menu.current < len(menu.buttons)-1: popup = layers.PopUp(screen, 1)
main.menu.current += 1
elif key == keys.LEFT and menu.current > 0:
menu.current -= 1
print(menu.current)
def draw(): def draw():
game.set_layer("menu", menu.draw_layer()) screen.set_layer("gui", gui.draw())
screen.set_layer("popup", popup.draw())
if __name__ == "__main__": if __name__ == "__main__":
from listener import key_listener listener.build_thread(screen).start()
#key_listener.start()
while True: while True:
draw() draw()
game.draw() screen.draw()
print("t") sleep(0.4)
sleep(1)
Loading…
Cancel
Save