|
|
@ -1,55 +1,16 @@ |
|
|
|
import os |
|
|
|
from graphics.colors import Colors |
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
class Screen: |
|
|
|
"""Moteur graphique basé sur des claques""" |
|
|
|
def __init__(self): |
|
|
|
def __init__(self, game): |
|
|
|
size = os.get_terminal_size() |
|
|
|
self.x = size.columns |
|
|
|
self.y = size.lines - 2 |
|
|
|
self.frame = {} |
|
|
|
self.__layers = {} |
|
|
|
|
|
|
|
self.game = game |
|
|
|
|
|
|
|
self.handlers = [] |
|
|
|
|
|
|
@ -88,4 +49,45 @@ class Screen: |
|
|
|
"""Envoie les touches reçues à tous les calques actifs""" |
|
|
|
for layer in list(self.__layers.values()): |
|
|
|
if layer.handle_keys == True: |
|
|
|
layer.key_handler(key) |
|
|
|
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 |