Compare commits

..

No commits in common. "77d99659a99ee7e3f4db33b462e96e954cda1dba" and "1b0a3a35000830eacdf8f736b6ed3d51f4f96157" have entirely different histories.

1 changed files with 30 additions and 32 deletions

62
asci.py
View File

@ -1,12 +1,9 @@
# Asci (1.9.2)
# Asci (1.9.1)
from math import floor, ceil
SCREEN_WIDTH = 21
SCREEN_HEIGHT = 7
class Asci:
def __init__(self, maps, entities, events_mapping, keys_mapping, behaviors=None):
def __init__(self, maps, entities, events_mapping, keys_mapping, behaviors=None, screen_width=21, screen_height=7):
# Load maps and entities
self.maps = [Map(*i) for i in maps]
self.entities = {}
@ -30,7 +27,7 @@ class Asci:
for i in behaviors: self._behaviors[i] = behaviors[i]
# Screen initialisation
self.screen = Screen()
self.screen = Screen(screen_width, screen_height)
self.current_map = None
def _looked_case(self, direction):
@ -187,7 +184,7 @@ class Asci:
data_copy = self.data[:]
for entity in self.current_map.entities.values():
self._behaviors[entity.behavior](entity, data_copy, self.stat, self.screen, walkable)
if entity.map_id == self.data[1] and (0 <= entity.pos_x - self.data[2] + self.screen.pos_player[0] < SCREEN_WIDTH) and (0 <= entity.pos_y - self.data[3] + self.screen.pos_player[1] < SCREEN_HEIGHT):
if entity.map_id == self.data[1] and (0 <= entity.pos_x - self.data[2] + self.screen.pos_player[0] < self.screen.screen_width) and (0 <= entity.pos_y - self.data[3] + self.screen.pos_player[1] < self.screen.screen_height):
self.screen.set_cell(entity.pos_x, entity.pos_y, entity.symbol)
self.screen.set_cell(self.data[2], self.data[3], player)
@ -222,10 +219,12 @@ class Asci:
# Classes used by Asci
class Screen:
def __init__(self):
def __init__(self, screen_width=21, screen_height=7):
# Screen configuration
self.pos_player = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
self._on_screen = [[" " for _ in range(SCREEN_WIDTH)] for _ in range(SCREEN_HEIGHT)]
self.screen_width = screen_width
self.screen_height = screen_height
self.pos_player = (screen_width // 2, screen_height // 2)
self._on_screen = [[" " for _ in range(screen_width)] for _ in range(screen_height)]
self._asci_data = []
def load_data(self, data):
@ -241,8 +240,8 @@ class Screen:
def set_screen(self):
x = self._asci_data[2] - self.pos_player[0] ; y = self._asci_data[3] - self.pos_player[1]
for x_map in range(x, x + SCREEN_WIDTH):
for y_map in range(y, y + SCREEN_HEIGHT):
for x_map in range(x, x + self.screen_width):
for y_map in range(y, y + self.screen_height):
self._on_screen[y_map - y][x_map - x] = " "
if 0 <= x_map < self.map_width and 0 <= y_map < self.map_height:
try: self._on_screen[y_map - y][x_map - x] = self._world[y_map][x_map]
@ -251,7 +250,7 @@ class Screen:
def display(self, return_input=True, low_bar=None):
for line_no in range(len(self._on_screen)):
line = "".join(self._on_screen[line_no])
if line_no + 1 == SCREEN_HEIGHT and return_input:
if line_no + 1 == self.screen_height and return_input:
if not low_bar: line = line[:-6] + ">"
else: line = low_bar + ">"
print(line, end="")
@ -260,10 +259,10 @@ class Screen:
print(line)
def clear(self):
print("\n" * SCREEN_HEIGHT)
print("\n" * self.screen_height)
def display_text(self, string):
paragraphs = [i for i in text_formater(string, SCREEN_WIDTH, SCREEN_HEIGHT) if i]
paragraphs = [i for i in text_formater(string, self.screen_width, self.screen_height) if i]
nb_par = len(paragraphs)
for index in range(nb_par):
self.clear()
@ -274,13 +273,13 @@ class Screen:
def set_cell(self, x, y, value):
x = x - (self._asci_data[2] - self.pos_player[0])
y = y - (self._asci_data[3] - self.pos_player[1])
if 0 <= x < SCREEN_WIDTH and 0 <= y < SCREEN_HEIGHT:
if 0 <= x < self.screen_width and 0 <= y < self.screen_height:
self._on_screen[y][x] = value
def get_cell(self, x, y):
x = x - (self._asci_data[2] - self.pos_player[0])
y = y - (self._asci_data[3] - self.pos_player[1])
if 0 <= x < SCREEN_WIDTH and 0 <= y < SCREEN_HEIGHT:
if 0 <= x < self.screen_width and 0 <= y < self.screen_height:
return self._on_screen[y][x]
else: return " "
@ -327,35 +326,34 @@ def convert(string, force_int=False):
else: return string
def text_formater(string):
screen_displayable_height = SCREEN_HEIGHT - 1
def text_formater(string, screen_width=21, screen_height=6):
def line_formater(string):
def line_formater(string, screen_width):
string_result = ""
while len(string) > SCREEN_WIDTH:
stop_index = SCREEN_WIDTH
while len(string) > screen_width:
stop_index = screen_width
while stop_index > 0 and not string[stop_index].isspace(): stop_index -= 1
if not stop_index: stop_index = SCREEN_WIDTH
if not stop_index: stop_index = screen_width
string_result += string[:stop_index].strip() + "\n"
string = string[stop_index:].strip()
return string_result + string
def paragraph_formater(lines):
def paragraph_formater(lines, screen_height):
paragraphs = ""
while len(lines) >= screen_displayable_height:
paragraphs += "\n".join(lines[:screen_displayable_height]) + "\n\n"
lines = lines[screen_displayable_height:]
while len(lines) >= screen_height:
paragraphs += "\n".join(lines[:screen_height]) + "\n\n"
lines = lines[screen_height:]
return paragraphs + "\n".join(lines)
lines = []
for line in string.split("\n"):
for formated_line in line_formater(line).split("\n"):
for formated_line in line_formater(line, screen_width).split("\n"):
lines.append(formated_line)
return paragraph_formater(lines).split("\n\n")
return paragraph_formater(lines, screen_height).split("\n\n")
def read_event(data, event, quest):
@ -475,8 +473,8 @@ def _walk_engine(entity, frame):
# Extra functions
def print_text(text, min_value=0, max_value=0, default_value=0):
paragraphs = [i for i in text_formater(text) if i]
def print_text(text, min_value=0, max_value=0, default_value=0, screen_width=21, screen_height=7):
paragraphs = [i for i in text_formater(text, screen_width, screen_height) if i]
nb = len(paragraphs)
for index in range(nb):
print("\n" * 7)
@ -501,4 +499,4 @@ def center(string, total_length, symbol):
def enumerate(data):
return [(i, data[i]) for i in range(len(data))]
return [(i, data[i]) for i in range(len(data))]