You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.9 KiB
93 lines
2.9 KiB
2 years ago
|
import os
|
||
|
from graphics.colors import Colors
|
||
|
|
||
|
class Screen:
|
||
2 years ago
|
"""Moteur graphique basé sur des claques"""
|
||
2 years ago
|
def __init__(self, game):
|
||
2 years ago
|
size = os.get_terminal_size()
|
||
|
self.x = size.columns
|
||
|
self.y = size.lines - 2
|
||
|
self.frame = {}
|
||
|
self.__layers = {}
|
||
2 years ago
|
|
||
|
self.game = game
|
||
2 years ago
|
|
||
2 years ago
|
self.handlers = []
|
||
|
|
||
2 years ago
|
def set_layer(self, 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]
|
||
2 years ago
|
|
||
|
def __combine_layers(self):
|
||
|
self.frame = {}
|
||
|
layers = sorted(self.__layers.values(), key=lambda layer: layer.z_index)
|
||
|
for layer in layers:
|
||
2 years ago
|
layer.draw()
|
||
2 years ago
|
self.frame.update(layer.frame)
|
||
|
|
||
|
def draw(self):
|
||
|
self.__combine_layers()
|
||
2 years ago
|
buffer = ""
|
||
2 years ago
|
for y in range(self.y):
|
||
|
for x in range(self.x):
|
||
|
if (x, y) in self.frame:
|
||
2 years ago
|
buffer += self.frame[(x, y)]
|
||
2 years ago
|
else:
|
||
2 years ago
|
buffer += " "
|
||
|
buffer += "\n"
|
||
2 years ago
|
print("\033[H\033[J", end="")
|
||
2 years ago
|
print(buffer)
|
||
2 years ago
|
|
||
|
def send_key(self, key):
|
||
2 years ago
|
"""Envoie les touches reçues à tous les calques actifs"""
|
||
|
for layer in list(self.__layers.values()):
|
||
|
if layer.handle_keys == True:
|
||
2 years ago
|
layer.key_handler(key)
|
||
|
|
||
|
|
||
|
class Layer:
|
||
|
def __init__(self, screen, z_index, name):
|
||
|
self.name = name
|
||
|
self.frame = {}
|
||
|
self.z_index = z_index
|
||
|
self.x = screen.x
|
||
|
self.y = screen.y
|
||
|
self.screen = screen
|
||
|
self.handle_keys = True
|
||
|
|
||
|
screen.set_layer(self)
|
||
|
|
||
|
def put_char(self, char, x, y, colors=[]):
|
||
|
if x > self.x or x < 0 or y > self.y or y < 0:
|
||
|
raise ValueError("out of range pixel")
|
||
|
self.frame[(x, y)] = "".join(colors) + char + Colors.RESET
|
||
|
|
||
|
def put_string(self, string, x, y, colors=[]):
|
||
|
if x > self.x or x < 0 or y > self.y or y < 0:
|
||
|
raise ValueError("out of range pixel")
|
||
|
string = string.split("\n")
|
||
|
for string_y in range(len(string)):
|
||
|
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
|
||
|
|
||
|
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):
|
||
|
raise ValueError("out of range pixel")
|
||
|
if len(template) > 1:
|
||
|
raise ValueError("template should be 1 char long")
|
||
|
for x in range(x1, x2+1):
|
||
|
for y in range(y1, y2+1):
|
||
|
self.frame[(x, y)] = "".join(colors) + template + Colors.RESET
|
||
|
|
||
|
def draw(self):
|
||
|
self.frame = {}
|
||
|
|
||
|
def key_handler(self):
|
||
|
pass
|