From a529038ee275326d4e12446a67b1258a2f1bc56e Mon Sep 17 00:00:00 2001 From: Shadow15510 Date: Fri, 28 Jan 2022 12:33:28 +0100 Subject: [PATCH 1/6] Change on entities system and update samples --- asci.py | 76 +++++++++++++++++++++++++++++++-------------- samples/sample_1.py | 74 ++++++++++++++++++++++++++++++++----------- samples/sample_2.py | 37 ++++++++++++---------- samples/sample_3.py | 22 ++++++++----- samples/sample_4.py | 65 +++++++++++++++++++------------------- 5 files changed, 175 insertions(+), 99 deletions(-) diff --git a/asci.py b/asci.py index 3bcd6c9..f7650f1 100644 --- a/asci.py +++ b/asci.py @@ -1,4 +1,4 @@ -# Asci (version 1.6.3) +# Asci (version 1.7.0) class Screen: def __init__(self, screen_width=21, screen_height=6): @@ -15,8 +15,7 @@ class Screen: self.map_width = max([len(line) for line in self._world]) self.map_height = len(self._world) - def set_data(self, coords): - x, y = coords + def set_data(self, x, y): for x_map in range(x, x + self.screen_width): for y_map in range(y, y + self.screen_height): self._data[y_map - y][x_map - x] = " " @@ -61,6 +60,8 @@ class Asci: # Screen initialisation self.screen = Screen(screen_width, screen_height) + self.current_map = None + self.visible_entities = [] def _looked_case(self, direction): # Left @@ -83,16 +84,16 @@ class Asci: def _cell_test(self, direction): if direction == 1: - if self.data[-2] + 9 < 0: return -1 + if self.data[2] + 9 < 0: return -1 else: cell = self.screen.get_cell(9, 3) if direction == 3: - if self.data[-2] + 11 >= self.map_width: return -1 + if self.data[2] + 11 >= self.map_width: return -1 else: cell = self.screen.get_cell(11, 3) if direction == 5: - if self.data[-1] + 2 < 0: return -1 + if self.data[3] + 2 < 0: return -1 else: cell = self.screen.get_cell(10, 2) if direction == 2: - if self.data[-1] + 4 >= self.map_height: return -1 + if self.data[3] + 4 >= self.map_height: return -1 else: cell = self.screen.get_cell(10, 4) cell_patterns = self.legend @@ -116,7 +117,9 @@ class Asci: # Change map elif interaction and cell_test == len(self.legend) - 2: # or (self.data[1] and cell_test < 0): self.data[1], self.data[2], self.data[3] = self._get_map(key) - self.screen.set_world(self.maps[self.data[1]].map_data) + self.current_map = self.maps[self.data[1]] + self.screen.set_world(self.current_map.map_data) + self._get_visible_entities() self.map_width, self.map_height = self.screen.get_map_size() @@ -130,10 +133,10 @@ class Asci: def _interaction(self, direction, cell_content): x, y = self._looked_case(direction) - data_copy = [self.data[0], self.data[1], x, y] + data_copy = [self.data[0], self.data[1], x, y, self.data[4]] # Get the event - event = self._game_events_mapping[cell_content](data_copy, self.stat) + event = self._game_events_mapping[cell_content](data_copy, self.stat, self._get_entity_id(x, y)) if type(event) == tuple: quest, event = event else: @@ -166,14 +169,27 @@ class Asci: def _get_map(self, direction): current_coords = self._looked_case(direction) - current_map = self.data[1] - for coords in self.maps[current_map].coords: + for coords in self.current_map.coords: if coords[:2] == current_coords: return coords[2], coords[3] - 10, coords[4] - 3 - return current_map, self.data[2], self.data[3] + return self.data[1], self.data[2], self.data[3] + # Entites gestion + def _get_visible_entities(self): + self.visible_entities = [] + for entity in self.current_map.entities: + symbol, x, y = self.current_map.entities[entity] + if (0 <= x - self.data[2] < self.screen.screen_width) and (0 <= y - self.data[3] < self.screen.screen_height): + self.visible_entities.append((symbol, x, y, entity)) + + def _get_entity_id(self, x, y): + for _, entity_x, entity_y, entity_id in self.visible_entities: + if entity_x == x and entity_y == y: + return entity_id + + # Mainloop def mainloop(self, end_game, stat=None, data=None, routine=None, player="@", door="^", walkable=" ", exit_key=9, multi_move="."): if exit_key in self._game_keys_mapping: raise ValueError("'{}' is already assigned to a function.".format(exit_key)) @@ -182,40 +198,51 @@ class Asci: if not stat or type(stat) != list: self.stat = [100] else: self.stat = stat - if not data: self.data = [{"main": 0}, 0, 0, 0] - else: self.data = [data[0], data[1], data[2] - 10, data[3] - 3] + if not data: self.data = [{"main": 0}, 0, 0, 0, 0] + else: self.data = [data[0], data[1], data[2] - 10, data[3] - 3, 0] self.legend.append(door) self.legend.append(walkable) # Screen and map configuration - self.screen.set_world(self.maps[self.data[1]].map_data) + self.current_map = self.maps[self.data[1]] + self.screen.set_world(self.current_map.map_data) self.map_width, self.map_height = self.screen.get_map_size() - key = key_buffer = 0 + key = 0 while key != exit_key and self.stat[0] > 0 and self.data[0]["main"] < end_game: - self.screen.set_data(self.data[-2:]) - + # Update the map + self.screen.set_data(self.data[2], self.data[3]) + + # Display the player and entites over it self.screen.set_cell(10, 3, player) + self._get_visible_entities() + for symbol, x, y, _ in self.visible_entities: + self.screen.set_cell(x - self.data[2], y - self.data[3], symbol) + + # Display map and get the key key = convert(self.screen.display()) - if not key: key = key_buffer - else: key_buffer = key + if not key: key = self.data[4] + else: self.data[4] = key if type(key) == str and key[0] == multi_move: for i in list(key[1:]): self._keyboard(convert(i), False) + self.data[4] = convert(key[-1]) else: self._keyboard(key) # Launching the game routine - if routine: routine(self.data, self.stat) + if routine: + data_copy = [self.data[0], self.data[1], self.data[2] + 10, self.data[3] + 3, self.data[4]] + routine(data_copy, self.stat) if self.stat[0] <= 0: self.stat[0] = 100 self.data[2] += 10 self.data[3] += 3 - return self.stat, self.data + return self.stat, self.data[:-1] class Event: @@ -227,8 +254,9 @@ class Event: class Map: - def __init__(self, map_data, *coords): + def __init__(self, map_data, entities, *coords): self.map_data = map_data + self.entities = entities self.coords = coords diff --git a/samples/sample_1.py b/samples/sample_1.py index f2f6eb1..4bfc5b1 100644 --- a/samples/sample_1.py +++ b/samples/sample_1.py @@ -1,36 +1,74 @@ from asci import * -carte_monde = (( -r""" +monde = (r""" _ ### /o\__ ##### -| <>\ ### -|____| /_\ +|_ <>\ ### +|^|__| /_\ - * + -|==|==|==|==|==|==|==|""",),) +|==|==|==|==|==|==|==|""", +# Entités +{ + "sdf": ["*", 2, 5] +}, +# Portes +(1, 3, 1, 5, 7)) + + +maison = (r""" ++--+--+--------+--+--+ +| | | | | | +| + + + + | +| | +| + + + + | ++--/ \--------/ \--+ +| | ++---|^|--------------+""", +{}, +(5, 7, 0, 1, 3)) + +carte_monde = (monde, maison) -def pnj(data, stat): - xp, carte_actuelle, x, y = data - coords = (x, y) +def pnj(data, stat, identifiant): + if identifiant == "sdf": return { + 0: [0, "Mon bon monsieur, vous n'auriez pas quelques sous pour moi ?\n1. He non mon brave...\n2. Mais si, bien sur, tenez.", 2], + 1: [6, "Radin !"], + 2: [1, "Merci !", 0, (1, -1)], # 0 réponse possibles, -1 Argent - if carte_actuelle == 0: - if coords == (2, 5): return { - 0: [0, "Mon bon monsieur, vous n'auriez pas quelques sous pour moi ? 1. He non mon brave... 2. Mais si, bien sur, tenez.", 2], - 1: [2, "Radin !"], - # 0 réponse possibles, -1 Argent - 2: [1, "Merci !", 0, (1, -1)], + 3: [0, "Hmm ?\n1. Arretez de me suivre !\n2. Non rien.", 2], + 4: [2, "Soit..."], + 5: [-2, "Bien"], - "base": [0, "Hmm ?"] - } + "base": [0, "Hmm ?"] + } return [0, "Hmm ?"] +def routine(data, stat): + if data[0]["main"] == 3: + if not "sdf" in carte_monde[data[1]][1]: carte_monde[data[1]][1]["sdf"] = ["*", data[2] + 1, data[3]] + + if data[4] == 1: carte_monde[data[1]][1]["sdf"] = ["*", data[2] + 1, data[3]] + elif data[4] == 2: carte_monde[data[1]][1]["sdf"] = ["*", data[2], data[3] - 1] + elif data[4] == 3: carte_monde[data[1]][1]["sdf"] = ["*", data[2] - 1, data[3]] + elif data[4] == 5: carte_monde[data[1]][1]["sdf"] = ["*", data[2], data[3] + 1] + + elif data[0]["main"] == 6: + for i in range(len(carte_monde)): + if "sdf" in carte_monde[i][1]: carte_monde[i][1].pop("sdf") + + carte_monde[0][1]["sdf"] = ["*", 2, 5] + + data[0]["main"] = 0 + + + def affichage_stat(data, stat): pv, argent = stat print("Statistiques") @@ -45,4 +83,4 @@ touche = {6: affichage_stat} def mon_jeu(): rpg_python = Asci(carte_monde, evenements, touche) - rpg_python.mainloop(4, stat=[100, 5]) \ No newline at end of file + rpg_python.mainloop(7, stat=[100, 5], routine=routine) \ No newline at end of file diff --git a/samples/sample_2.py b/samples/sample_2.py index c4e08ed..708fc6f 100644 --- a/samples/sample_2.py +++ b/samples/sample_2.py @@ -8,57 +8,60 @@ cartes = ( |_ <>\ ### |^|__| /_\ - ? + |==|==|==|==|==|==|==|""", +{1: ["*", 2, 5]}, (1, 3, 1, 5, 7)), (r""" +--+--+--------+--+--+ -| | | ? | | ?| +| | | | | | | + + + + | | | | + + + + | +--/ \--------/ \--+ | | +---|^|--------------+""", +{"habitant": ["?", 9, 1], +"voleur": ["?", 20, 1]}, (5, 7, 0, 1, 3)) ) -def pnj(data, stat): - xp, carte_actuelle, x, y = data - coords = (x, y) - +def pnj(data, stat, identifiant): + carte_actuelle = data[1] + if carte_actuelle == 0: - if coords == (2, 5): return { + if identifiant == 1: return { 0: [0, "Hey ! J'ai entendu du bruit dans la maison, mais je n'ose pas rentrer... 1. Rien entendu. 2. Je vais jeter un oeil.", 2], 1: [3, "Etes-vous sourd ?"], 2: [1, "J'etais sur que vous m'ecouteriez !"], - 3: [2, "C'est la maison juste au nord."], + 3: [2, "C'est la maison juste au nord. Soyez prudent !"], 4: [0, "Enfin, vous entendez bien du bruit la ? Et si c'etait un voleur ? 1. Bon ok j'y vais. 2. Mais foutez moi la paix !", 2], 6: [0, "..."], - 5: [2, "Soyez prudent !"], + 5: [0, "Alors ?"], - 12: [1, "J'etais sur d'avoir entendu un truc !"], + 10: [1, "J'etais sur d'avoir entendu un truc !"], "base": [0, "Vous avez entendu quelque chose ?"] } elif carte_actuelle == 1: - if coords == (9, 1): return { - 7: [0, "Je crois que le voleur est dans la piece d'a cote... 1. Je vais regarder. 2. Debrouillez-vous !", 2], - 8: [2, "Merci !"], - 9: [0, "Pleutre ! Hors de ma vue !"], + if identifiant == "habitant": return { + 5: [0, "Je crois que le voleur est dans la piece d'a cote... 1. Je vais regarder. 2. Debrouillez-vous !", 2], + 6: [2, "Merci !"], + 7: [0, "Pleutre ! Hors de ma vue !"], - 11: [1, "Ah, merci !"], + 9: [1, "Ah, merci !"], "base": [0, "J'ai peur de sortir de cette piece"] } - elif coords == (20, 1): return { - 10: [1, "Ciel, je suis fait !"], + elif identifiant == "voleur": return { + 8: [1, "Ciel, je suis fait !"], + 9: [0, "Je pars, je pars !"], "base": [0, "File avant que je ne te detrousse !"] } diff --git a/samples/sample_3.py b/samples/sample_3.py index 09af7f2..121ee3a 100644 --- a/samples/sample_3.py +++ b/samples/sample_3.py @@ -15,6 +15,11 @@ cartes = ( #### |<> <>| #### ## |_________| ## || ||""", +{ + "medecin": ["*", 24, 4], + "ami": ["*", 16, 1], + "bandit": ["$", 4, 7] +}, (1, 3, 1, 5, 7), (19, 4, 2, 4, 4)), @@ -27,6 +32,7 @@ cartes = ( +--/ *\--------/ \--+ | | +---|^|--------------+""", +{0: ["*", 5, 5]}, (5, 7, 0, 1, 3)), (r""" @@ -36,32 +42,32 @@ cartes = ( | | +--|^|--+ """, +{}, (4, 4, 0, 19, 4)) ) -def pnj(data, stat): +def pnj(data, stat, identifiant): carte_actuelle = data[1] - coords = data[2], data[3] xp = data[0]["main"] if carte_actuelle == 0: - if coords == (24, 4): + if identifiant == "medecin": if stat[0] < 100: return [0, "Oh, mais tu es blesse !", 0, (0, 50)] else: return [0, "Reviens me voir quand tu seras blesse."] - elif coords == (16, 1): return { + elif identifiant == "ami": return { "base": [0, "Alors ? T'en sorts-tu ?"], 0: [0, "J'ai une quete pour toi ! Un ami a moi a des problemes : un personnage louche traine autour de sa maison... Si tu pouvais l'en debarasser, il t'en serai reconnaissant. 1. Je m'en charge ! 2. Trouve quelqu'un d'autre.", 2], - 1: [2, "J'etais sur que je pouvais compter sur toi ! Tiens, voila une dague et une petit bouclier.", 0, 0, 10, 10], + 1: [2, "J'etais sur que je pouvais compter sur toi ! Tiens, voila une dague et une petit bouclier.", 0, (1, 10), (2, 10)], 2: [3, "Si un jour tu as besoin de moi, tu seras sympa de m'oublier."], 3: [0, "Alors ? Il est mort ce bandit ?"], 4: [1, "Merci, tu as rendu un grand service a mon ami !"] } - elif coords == (4, 7): + elif identifiant == "bandit": # Si le bandit vient d'être tué if xp == 3: return [1, "Vous avez reussi la quete !"] @@ -74,13 +80,13 @@ def pnj(data, stat): return [0, "Hmm ?"] -def ennemi(data, stat): +def ennemi(data, stat, identifiant): carte_actuelle = data[1] coords = data[2], data[3] xp = data[0]["main"] if carte_actuelle == 0: - if coords == (4, 7): + if identifiant == "bandit": # Bandit vivant if xp == 3: if combat(stat, [75, randint(5, 10), randint(5, 10)]): diff --git a/samples/sample_4.py b/samples/sample_4.py index 35cd5eb..0bbf7a2 100644 --- a/samples/sample_4.py +++ b/samples/sample_4.py @@ -12,6 +12,9 @@ cartes = ( |==|==|==|==|==|==|==|""", +{ + "pnj": ["?", 2, 5] +}, (1, 3, 1, 5, 7)), (r""" @@ -23,46 +26,45 @@ cartes = ( +--/ \--------/ \--+ | | +---|^|--------------+""", +{ + "boulanger": ["?", 9, 1], + "kiosque": ["?", 20, 1], +}, (5, 7, 0, 1, 3)) ) -def pnj(data, stat): - xp, carte_actuelle, x, y = data - coords = (x, y) +def pnj(data, stat, identifiant): + if identifiant == "pnj": + # Si les deux quêtes annexes sont terminées, on incrémente la quête principale + if "pain" in data[0] and "journal" in data[0] and data[0]["pain"] == 3 and data[0]["journal"] == 3: + data[0]["main"] += 1 - if carte_actuelle == 0: - if coords == (2, 5): - # Si les deux quêtes annexes sont terminées, on incrémente la quête principale - if "pain" in xp and "journal" in xp and xp["pain"] == 3 and xp["journal"] == 3: - data[0]["main"] += 1 + # Si le joueur accepte la quête, on initialise les deux quêtes annexes + if data[0]["main"] == 1: + data[0]["pain"] = 1 + data[0]["journal"] = 1 + return [2, "Tu es un ange !"] # data[0]["main"] = 3 - # Si le joueur accepte la quête, on initialise les deux quêtes annexes - if xp["main"] == 1: - data[0]["pain"] = 1 - data[0]["journal"] = 1 - return [2, "Tu es un ange !"] # data[0]["main"] = 3 + elif "pain" in data[0] and data[0]["pain"] == 2: return "pain", [1, "Ah merci pour le pain."] # Si joueur a été chercher le pain + elif "journal" in data[0] and data[0]["journal"] == 2: return "journal", [1, "Aha enfin de la lecture !"] # Si le joueur a été chercher le journal - elif "pain" in xp and xp["pain"] == 2: return "pain", [1, "Ah merci pour le pain."] # Si joueur a été chercher le pain - elif "journal" in xp and xp["journal"] == 2: return "journal", [1, "Aha enfin de la lecture !"] # Si le joueur a été chercher le journal - - else: - return { - 0: [0, "J'ai que tu ailles m'acheter un journal et du pain.\n1. J'y vais m'sieur !\n2. Humm... Non.", 2], - 2: [-2, "Vilain garnement !"], # Si le joueur refuse la quête - 3: [0, "Alors ? J'attend moi !"], # Si le joueur a accepté la quête, mais n'a été cherché ni le pain, ni le journal - 4: [1, "Merci, pour ces commissions !"] # Si le joueur a terminé la quête - } + else: + return { + 0: [0, "J'aimerais que que tu ailles m'acheter un journal et du pain.\n1. J'y vais m'sieur !\n2. Humm... Non.", 2], + 2: [-2, "Vilain garnement !"], # Si le joueur refuse la quête + 3: [0, "Alors ? J'attend moi !"], # Si le joueur a accepté la quête, mais n'a été cherché ni le pain, ni le journal + 4: [1, "Merci, pour ces commissions !"] # Si le joueur a terminé la quête + } - elif carte_actuelle == 1: - if coords == (9, 1): - if "pain" in xp and xp["pain"] == 1: return "pain", [1, "Tient voila pour toi ! [+PAIN]"] - else: return [0, "Je suis boulanger mon jeune ami !"] + elif identifiant == "boulanger": + if "pain" in data[0] and data[0]["pain"] == 1: return "pain", [1, "Tient voila pour toi ! [+PAIN]"] + else: return [0, "Je suis boulanger mon jeune ami !"] - elif coords == (20, 1): - if "journal" in xp and xp["journal"] == 1: return "journal", [1, "Voila ton journal"] - else: return [0, "Ce kiosque est dans la famille depuis plusieurs générations !"] + elif identifiant == "kiosque": + if "journal" in data[0] and data[0]["journal"] == 1: return "journal", [1, "Voila ton journal"] + else: return [0, "Ce kiosque est dans la famille depuis plusieurs générations !"] return [0, "Hmm ?"] @@ -80,5 +82,4 @@ touche = {8: affichage_statistique} def mon_jeu(): rpg_python = Asci(cartes, evenements, touche) - stat, data = rpg_python.mainloop(5, [100]) - print(stat, data) \ No newline at end of file + stat, data = rpg_python.mainloop(5, [100]) \ No newline at end of file From 453b916db9474e4f3779eefd22b3935968f14717 Mon Sep 17 00:00:00 2001 From: Shadow15510 Date: Fri, 28 Jan 2022 12:36:05 +0100 Subject: [PATCH 2/6] minor fix on samples --- samples/sample_3.py | 8 ++++---- samples/sample_4.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/samples/sample_3.py b/samples/sample_3.py index 121ee3a..7ccac5a 100644 --- a/samples/sample_3.py +++ b/samples/sample_3.py @@ -5,13 +5,13 @@ from random import randint cartes = ( (r""" __ -/ \___ ### * +/ \___ ### |<> \ ##### _ |^|____| ### / \ - /_\ |^| * + /_\ |^| __ __ - $ ## / \___/ \ ## + ## / \___/ \ ## #### |<> <>| #### ## |_________| ## || ||""", @@ -29,7 +29,7 @@ cartes = ( | + + + + | | | | + + + + | -+--/ *\--------/ \--+ ++--/ \--------/ \--+ | | +---|^|--------------+""", {0: ["*", 5, 5]}, diff --git a/samples/sample_4.py b/samples/sample_4.py index 0bbf7a2..1c80186 100644 --- a/samples/sample_4.py +++ b/samples/sample_4.py @@ -8,7 +8,7 @@ cartes = ( |_ <>\ ### |^|__| /_\ - ? + |==|==|==|==|==|==|==|""", @@ -19,7 +19,7 @@ cartes = ( (r""" +--+--+--------+--+--+ -| | | ? | | ?| +| | | | | | | + + + + | | | | + + + + | From 92d27edc3d497554800201fba9d9f591fa845722 Mon Sep 17 00:00:00 2001 From: Shadow15510 Date: Fri, 28 Jan 2022 19:17:10 +0100 Subject: [PATCH 3/6] Update the Tiled converter to support the new map format --- tiled/converter | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/tiled/converter b/tiled/converter index 120288c..c72d467 100755 --- a/tiled/converter +++ b/tiled/converter @@ -3,7 +3,7 @@ from sys import argv import xmltodict -def convert_to_string(filename, doors, misc): +def convert_to_string(filename, doors, entities): output_filename, extension = filename.split('.') char_list = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ " @@ -18,32 +18,36 @@ def convert_to_string(filename, doors, misc): data = [[int(char_id) for char_id in line.split(",") if char_id] for line in data] doors_coords = [] - misc_coords = [] + entities_data = [] + entities_count = 0 output = r"" for line_index, line in enumerate(data): for char_index, char_id in enumerate(line): - output += char_list[char_id] + + if char_list[char_id] not in entities: + output += char_list[char_id] + else: + output += " " + entities_data.append(f"({entities_count}, '{char_list[char_id]}', {char_index}, {line_index}, 'stand_by'),") + entities_count += 1 if char_list[char_id] in doors: doors_coords.append(f"\t({char_index}, {line_index}, , 0, 0),") - - if char_list[char_id] in misc: - misc_coords.append(f"# {char_list[char_id]} : ({char_index}, {line_index})") output += "\n" doors_coords = "\n".join(doors_coords) - misc_coords = "\n".join(misc_coords) + entities_data = "[\n\t" + "\n\t".join(entities_data) + "\n]" with open(f"{output_filename}.py", "w") as file: - file.write(f"{output_filename} = (r\"\"\"\n{output[:-1]}\"\"\",\n{doors_coords}\n)\n\n{misc_coords}") + file.write(f"{output_filename} = (r\"\"\"\n{output[:-1]}\"\"\",\n{entities_data},\n\n{doors_coords}\n)") filename, doors, misc = argv[1], "", "" for arg in argv[2:]: if arg.startswith("door"): doors = arg.split("=", 1)[1] - elif arg.startswith("misc"): - misc = arg.split("=", 1)[1] + elif arg.startswith("entities"): + entities = arg.split("=", 1)[1] -convert_to_string(filename, doors, misc) \ No newline at end of file +convert_to_string(filename, doors, entities) \ No newline at end of file From 134393ec60ad0808364f9714eee619fc6df5e3b2 Mon Sep 17 00:00:00 2001 From: Shadow15510 Date: Fri, 28 Jan 2022 19:17:48 +0100 Subject: [PATCH 4/6] Update samples with new entities system --- samples/sample_1.py | 58 ++++++++++++++++++--------------------------- samples/sample_2.py | 12 ++++++---- samples/sample_3.py | 18 +++++++------- samples/sample_4.py | 17 +++++++------ 4 files changed, 48 insertions(+), 57 deletions(-) diff --git a/samples/sample_1.py b/samples/sample_1.py index 4bfc5b1..d7d2655 100644 --- a/samples/sample_1.py +++ b/samples/sample_1.py @@ -1,19 +1,19 @@ -from asci import * +from asci_v2 import * monde = (r""" _ ### /o\__ ##### |_ <>\ ### |^|__| /_\ - + |==|==|==|==|==|==|==|""", # Entités -{ - "sdf": ["*", 2, 5] -}, +[ + ("sdf", "*", 2, 5, "stand by") +], # Portes (1, 3, 1, 5, 7)) @@ -27,47 +27,35 @@ maison = (r""" +--/ \--------/ \--+ | | +---|^|--------------+""", -{}, +[], (5, 7, 0, 1, 3)) carte_monde = (monde, maison) -def pnj(data, stat, identifiant): - if identifiant == "sdf": return { - 0: [0, "Mon bon monsieur, vous n'auriez pas quelques sous pour moi ?\n1. He non mon brave...\n2. Mais si, bien sur, tenez.", 2], - 1: [6, "Radin !"], - 2: [1, "Merci !", 0, (1, -1)], # 0 réponse possibles, -1 Argent +def pnj(data, stat, entites, identifiant): + xp = data[0]["main"] - 3: [0, "Hmm ?\n1. Arretez de me suivre !\n2. Non rien.", 2], - 4: [2, "Soit..."], - 5: [-2, "Bien"], + if identifiant == "sdf": + if xp == 2: entites["sdf"].change_behavior("follow") + elif xp == 4: entites["sdf"].change_behavior("stand by") - "base": [0, "Hmm ?"] - } + return { + 0: [0, "Mon bon monsieur, vous n'auriez pas quelques sous pour moi ?\n1. He non mon brave...\n2. Mais si, bien sur, tenez.", 2], + 1: [5, "Radin !"], + 2: [1, "Merci !", 0, (1, -1)], # 0 réponse possibles, -1 Argent + + 3: [0, "Hmm ?\n1. Arretez de me suivre !\n2. Non rien.", 2], + 4: [2, "Soit..."], + 5: [-2, "Bien"], + + "base": [0, "Hmm ?"] + } return [0, "Hmm ?"] -def routine(data, stat): - if data[0]["main"] == 3: - if not "sdf" in carte_monde[data[1]][1]: carte_monde[data[1]][1]["sdf"] = ["*", data[2] + 1, data[3]] - - if data[4] == 1: carte_monde[data[1]][1]["sdf"] = ["*", data[2] + 1, data[3]] - elif data[4] == 2: carte_monde[data[1]][1]["sdf"] = ["*", data[2], data[3] - 1] - elif data[4] == 3: carte_monde[data[1]][1]["sdf"] = ["*", data[2] - 1, data[3]] - elif data[4] == 5: carte_monde[data[1]][1]["sdf"] = ["*", data[2], data[3] + 1] - - elif data[0]["main"] == 6: - for i in range(len(carte_monde)): - if "sdf" in carte_monde[i][1]: carte_monde[i][1].pop("sdf") - - carte_monde[0][1]["sdf"] = ["*", 2, 5] - - data[0]["main"] = 0 - - def affichage_stat(data, stat): pv, argent = stat @@ -83,4 +71,4 @@ touche = {6: affichage_stat} def mon_jeu(): rpg_python = Asci(carte_monde, evenements, touche) - rpg_python.mainloop(7, stat=[100, 5], routine=routine) \ No newline at end of file + rpg_python.mainloop(7, stat=[100, 5], data=[{"main": 0}, 0, 10, 3]) \ No newline at end of file diff --git a/samples/sample_2.py b/samples/sample_2.py index 708fc6f..71ff75e 100644 --- a/samples/sample_2.py +++ b/samples/sample_2.py @@ -12,7 +12,9 @@ cartes = ( |==|==|==|==|==|==|==|""", -{1: ["*", 2, 5]}, +[ + (1, "*", 2, 5, "stand by") +], (1, 3, 1, 5, 7)), (r""" @@ -24,13 +26,15 @@ cartes = ( +--/ \--------/ \--+ | | +---|^|--------------+""", -{"habitant": ["?", 9, 1], -"voleur": ["?", 20, 1]}, +[ + ("habitant", "?", 9, 1, "stand by"), + ("voleur", "?", 20, 1, "stand by") +], (5, 7, 0, 1, 3)) ) -def pnj(data, stat, identifiant): +def pnj(data, stat, entites, identifiant): carte_actuelle = data[1] if carte_actuelle == 0: diff --git a/samples/sample_3.py b/samples/sample_3.py index 7ccac5a..e171dd5 100644 --- a/samples/sample_3.py +++ b/samples/sample_3.py @@ -15,11 +15,11 @@ cartes = ( #### |<> <>| #### ## |_________| ## || ||""", -{ - "medecin": ["*", 24, 4], - "ami": ["*", 16, 1], - "bandit": ["$", 4, 7] -}, +[ + ("medecin", "*", 24, 4, "stand by"), + ("ami", "*", 16, 1, "stand by"), + ("bandit", "$", 4, 7, "walk", 0, ((4, 7), (3, 7), (3, 6), (4, 6))) +], (1, 3, 1, 5, 7), (19, 4, 2, 4, 4)), @@ -32,7 +32,7 @@ cartes = ( +--/ \--------/ \--+ | | +---|^|--------------+""", -{0: ["*", 5, 5]}, +[(0, "*", 5, 5, "stand by")], (5, 7, 0, 1, 3)), (r""" @@ -42,12 +42,12 @@ cartes = ( | | +--|^|--+ """, -{}, +[], (4, 4, 0, 19, 4)) ) -def pnj(data, stat, identifiant): +def pnj(data, stat, entites, identifiant): carte_actuelle = data[1] xp = data[0]["main"] @@ -80,7 +80,7 @@ def pnj(data, stat, identifiant): return [0, "Hmm ?"] -def ennemi(data, stat, identifiant): +def ennemi(data, stat, entites, identifiant): carte_actuelle = data[1] coords = data[2], data[3] xp = data[0]["main"] diff --git a/samples/sample_4.py b/samples/sample_4.py index 1c80186..6f83245 100644 --- a/samples/sample_4.py +++ b/samples/sample_4.py @@ -12,9 +12,9 @@ cartes = ( |==|==|==|==|==|==|==|""", -{ - "pnj": ["?", 2, 5] -}, +[ + ("pnj", "?", 2, 5, "stand by") +], (1, 3, 1, 5, 7)), (r""" @@ -26,15 +26,15 @@ cartes = ( +--/ \--------/ \--+ | | +---|^|--------------+""", -{ - "boulanger": ["?", 9, 1], - "kiosque": ["?", 20, 1], -}, +[ + ("boulanger", "?", 9, 1, "stand by"), + ("kiosque", "?", 20, 1, "stand by") +], (5, 7, 0, 1, 3)) ) -def pnj(data, stat, identifiant): +def pnj(data, stat, entites, identifiant): if identifiant == "pnj": # Si les deux quêtes annexes sont terminées, on incrémente la quête principale if "pain" in data[0] and "journal" in data[0] and data[0]["pain"] == 3 and data[0]["journal"] == 3: @@ -57,7 +57,6 @@ def pnj(data, stat, identifiant): 4: [1, "Merci, pour ces commissions !"] # Si le joueur a terminé la quête } - elif identifiant == "boulanger": if "pain" in data[0] and data[0]["pain"] == 1: return "pain", [1, "Tient voila pour toi ! [+PAIN]"] else: return [0, "Je suis boulanger mon jeune ami !"] From b5dc8230789f79b532ae23680e164c0547544fac Mon Sep 17 00:00:00 2001 From: Shadow15510 Date: Fri, 28 Jan 2022 19:18:41 +0100 Subject: [PATCH 5/6] Add entities gestion and basic animations. Unify the coordinates system --- asci.py | 300 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 178 insertions(+), 122 deletions(-) diff --git a/asci.py b/asci.py index f7650f1..8caea0b 100644 --- a/asci.py +++ b/asci.py @@ -1,62 +1,19 @@ -# Asci (version 1.7.0) - -class Screen: - def __init__(self, screen_width=21, screen_height=6): - # Screen configuration - self.screen_width = screen_width - self.screen_height = screen_height - self._data = [[" " for _ in range(screen_width)] for _ in range(screen_height)] - - def clear(self): - print("\n" * self.screen_height) - - def set_world(self, world): - self._world = [[char for char in line] for line in world.split("\n")[1:]] - self.map_width = max([len(line) for line in self._world]) - self.map_height = len(self._world) - - def set_data(self, x, y): - for x_map in range(x, x + self.screen_width): - for y_map in range(y, y + self.screen_height): - self._data[y_map - y][x_map - x] = " " - if 0 <= x_map < self.map_width and 0 <= y_map < self.map_height: - try: self._data[y_map - y][x_map - x] = self._world[y_map][x_map] - except: pass - - def set_cell(self, x, y, value): - self._data[y][x] = value - - def display(self, return_input=True): - for line in self._data: - print("".join(line)) - - if return_input: return input(">") - - def display_text(self, string): - paragraphs = [i for i in text_formater(string) if i] - nb_par = len(paragraphs) - for index in range(nb_par): - self.clear() - print(paragraphs[index]) - if index + 1 == nb_par: return input(">") - else: input() - - def get_cell(self, x, y): - return self._data[y][x] - - def get_map_size(self): - return self.map_width, self.map_height - +# Asci (1.7.0) class Asci: - def __init__(self, maps, events_mapping, keys_mapping, screen_width=21, screen_height=6): + def __init__(self, maps, events_mapping, keys_mapping, behaviors=None, screen_width=21, screen_height=6): # Load maps self.maps = [Map(*i) for i in maps] # Custom functions - self.legend = list(events_mapping.keys()) - self._game_events_mapping = [events_mapping[i] for i in self.legend] + self._legend = list(events_mapping.keys()) + self._game_events_mapping = [events_mapping[i] for i in self._legend] self._game_keys_mapping = {key: keys_mapping[key] for key in keys_mapping if not key in (1, 2, 3, 5)} + + # Custom entities behavior + self._behaviors = {"stand by": stand_by, "follow": follow, "walk": walk} + if behaviors: + for i in behaviors: self._behaviors[i] = behaviors[i] # Screen initialisation self.screen = Screen(screen_width, screen_height) @@ -64,39 +21,36 @@ class Asci: self.visible_entities = [] def _looked_case(self, direction): - # Left - if direction == 1: - return self.data[2] + 9, self.data[3] + 3 + if direction == 1: # Left + return self.data[2] - 1, self.data[3] - # Right - elif direction == 3: - return self.data[2] + 11, self.data[3] + 3 + elif direction == 3: # Right + return self.data[2] + 1, self.data[3] - # Up - elif direction == 5: - return self.data[2] + 10, self.data[3] + 2 + elif direction == 5: # Up + return self.data[2], self.data[3] - 1 - # Down - elif direction == 2: - return self.data[2] + 10, self.data[3] + 4 + elif direction == 2: # Down + return self.data[2], self.data[3] + 1 - return self.data[2] + 10, self.data[3] + 3 + return self.data[2], self.data[3] def _cell_test(self, direction): if direction == 1: - if self.data[2] + 9 < 0: return -1 - else: cell = self.screen.get_cell(9, 3) + if self.data[2] - 1 < 0: return -1 + else: cell = self.screen.get_cell(self.data[2] - 10, self.data[3] - 3, self.data[2] - 1, self.data[3]) if direction == 3: - if self.data[2] + 11 >= self.map_width: return -1 - else: cell = self.screen.get_cell(11, 3) + if self.data[2] + 1 >= self.map_width: return -1 + else: cell = self.screen.get_cell(self.data[2] - 10, self.data[3] - 3, self.data[2] + 1, self.data[3]) if direction == 5: - if self.data[3] + 2 < 0: return -1 - else: cell = self.screen.get_cell(10, 2) + if self.data[3] - 1 < 0: return -1 + else: cell = self.screen.get_cell(self.data[2] - 10, self.data[3] - 3, self.data[2], self.data[3] - 1) if direction == 2: - if self.data[3] + 4 >= self.map_height: return -1 - else: cell = self.screen.get_cell(10, 4) + if self.data[3] + 1 >= self.map_height: return -1 + else: cell = self.screen.get_cell(self.data[2] - 10, self.data[3] - 3, self.data[2], self.data[3] + 1) - cell_patterns = self.legend + print(f"'{cell}'") + cell_patterns = self._legend for pattern_index in range(len(cell_patterns)): if cell in cell_patterns[pattern_index]: return pattern_index @@ -108,20 +62,16 @@ class Asci: cell_test = self._cell_test(key) # Move - if cell_test == len(self.legend) - 1: + if cell_test == len(self._legend) - 1: if key == 1: self.data[2] -= 1 if key == 3: self.data[2] += 1 if key == 5: self.data[3] -= 1 if key == 2: self.data[3] += 1 # Change map - elif interaction and cell_test == len(self.legend) - 2: # or (self.data[1] and cell_test < 0): - self.data[1], self.data[2], self.data[3] = self._get_map(key) - self.current_map = self.maps[self.data[1]] - self.screen.set_world(self.current_map.map_data) - self._get_visible_entities() - self.map_width, self.map_height = self.screen.get_map_size() - + elif interaction and cell_test == len(self._legend) - 2: + new_map, self.data[2], self.data[3] = self._get_map(key) + self._change_map(new_map) # Interaction elif interaction and cell_test >= 0: self._interaction(key, cell_test) @@ -131,12 +81,39 @@ class Asci: self.screen.clear() self._game_keys_mapping[key](self.data, self.stat) + def _get_map(self, direction): + current_coords = self._looked_case(direction) + + for coords in self.current_map.coords: + if coords[:2] == current_coords: + return coords[2], coords[3], coords[4] + + return self.data[1], self.data[2], self.data[3] + + def _change_map(self, new_map): + # Update entities + if self.current_map: + for i in range(len(self.current_map.entities)): + entity = self.current_map.entities[i] + if entity.behavior == "follow": + self.maps[new_map].entities.append(entity) + self.maps[self.data[1]].entities.pop(i) + + # Update current map + self.data[1] = new_map + self.current_map = self.maps[self.data[1]] + + # Update screen configuration + self.screen.set_world(self.current_map.map_data) + self.map_width, self.map_height = self.screen.get_map_size() + self._get_visible_entities() + def _interaction(self, direction, cell_content): x, y = self._looked_case(direction) data_copy = [self.data[0], self.data[1], x, y, self.data[4]] # Get the event - event = self._game_events_mapping[cell_content](data_copy, self.stat, self._get_entity_id(x, y)) + event = self._game_events_mapping[cell_content](data_copy, self.stat, self.visible_entities, self._get_entity_id(x, y)) if type(event) == tuple: quest, event = event else: @@ -145,12 +122,10 @@ class Asci: # data modification self.data[0] = data_copy[0] if self.data[1] != data_copy[1]: - self.data[1] = data_copy[1] - self.screen.set_world(self.maps[self.data[1]].map_data) - self.map_width, self.map_height = self.screen.get_map_size() + self._change_map(data_copy[1]) - if data_copy[2] != x: self.data[2] = data_copy[2] - 10 - if data_copy[3] != y: self.data[3] = data_copy[3] - 3 + if data_copy[2] != x: self.data[2] = data_copy[2] + if data_copy[3] != y: self.data[3] = data_copy[3] if not event: return event = read_event(self.data, event, quest) @@ -167,28 +142,25 @@ class Asci: self.data[0][quest] += answer_selected self._interaction(direction, cell_content) - def _get_map(self, direction): - current_coords = self._looked_case(direction) - - for coords in self.current_map.coords: - if coords[:2] == current_coords: - return coords[2], coords[3] - 10, coords[4] - 3 - - return self.data[1], self.data[2], self.data[3] - - # Entites gestion + # Entities gestion def _get_visible_entities(self): - self.visible_entities = [] + self.visible_entities = {} for entity in self.current_map.entities: - symbol, x, y = self.current_map.entities[entity] - if (0 <= x - self.data[2] < self.screen.screen_width) and (0 <= y - self.data[3] < self.screen.screen_height): - self.visible_entities.append((symbol, x, y, entity)) + if (0 <= entity.pos_x - self.data[2] + 10 < self.screen.screen_width) and (0 <= entity.pos_y - self.data[3] + 3 < self.screen.screen_height): + self.visible_entities[entity.entity_id] = entity def _get_entity_id(self, x, y): - for _, entity_x, entity_y, entity_id in self.visible_entities: - if entity_x == x and entity_y == y: + for entity_id in self.visible_entities: + entity = self.visible_entities[entity_id] + if entity.pos_x == x and entity.pos_y == y: return entity_id + def _run_entities_behaviors(self): + for entity in self.current_map.entities: + data_copy = get_data_copy(self.data) + self._behaviors[entity.behavior](entity, data_copy, self.stat, self.screen, self.walkable) + self._get_visible_entities() + # Mainloop def mainloop(self, end_game, stat=None, data=None, routine=None, player="@", door="^", walkable=" ", exit_key=9, multi_move="."): if exit_key in self._game_keys_mapping: @@ -199,27 +171,25 @@ class Asci: else: self.stat = stat if not data: self.data = [{"main": 0}, 0, 0, 0, 0] - else: self.data = [data[0], data[1], data[2] - 10, data[3] - 3, 0] + else: self.data = [data[0], data[1], data[2], data[3], 0] - self.legend.append(door) - self.legend.append(walkable) - - # Screen and map configuration - self.current_map = self.maps[self.data[1]] - self.screen.set_world(self.current_map.map_data) - self.map_width, self.map_height = self.screen.get_map_size() + # Configuration + self.walkable = walkable + self._legend.append(door) + self._legend.append(walkable) + self._change_map(data[1]) key = 0 while key != exit_key and self.stat[0] > 0 and self.data[0]["main"] < end_game: # Update the map - self.screen.set_data(self.data[2], self.data[3]) + self.screen.set_screen(self.data[2], self.data[3]) - # Display the player and entites over it - self.screen.set_cell(10, 3, player) - self._get_visible_entities() - for symbol, x, y, _ in self.visible_entities: - self.screen.set_cell(x - self.data[2], y - self.data[3], symbol) + # Compute the player's and entities' positions + self.screen.set_cell(self.data[2] - 10, self.data[3] - 3, self.data[2], self.data[3], player) + self._run_entities_behaviors() + for entity in self.visible_entities.values(): + self.screen.set_cell(self.data[2] - 10, self.data[3] - 3, entity.pos_x, entity.pos_y, entity.symbol) # Display map and get the key key = convert(self.screen.display()) @@ -236,15 +206,64 @@ class Asci: # Launching the game routine if routine: - data_copy = [self.data[0], self.data[1], self.data[2] + 10, self.data[3] + 3, self.data[4]] + data_copy = get_data_copy(self.data) routine(data_copy, self.stat) if self.stat[0] <= 0: self.stat[0] = 100 - self.data[2] += 10 - self.data[3] += 3 return self.stat, self.data[:-1] +# Classes used by Asci +class Screen: + def __init__(self, screen_width=21, screen_height=6): + # Screen configuration + self.screen_width = screen_width + self.screen_height = screen_height + self._on_screen = [[" " for _ in range(screen_width)] for _ in range(screen_height)] + + def get_map_size(self): + return self.map_width, self.map_height + + def set_world(self, world): + self._world = [[char for char in line] for line in world.split("\n")[1:]] + self.map_width = max([len(line) for line in self._world]) + self.map_height = len(self._world) + + def set_screen(self, x, y): + x -= 10 ; y -= 3 + 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] + except: pass + + def display(self, return_input=True): + for line in self._on_screen: + print("".join(line)) + + if return_input: return input(">") + + def clear(self): + print("\n" * self.screen_height) + + def display_text(self, string): + paragraphs = [i for i in text_formater(string) if i] + nb_par = len(paragraphs) + for index in range(nb_par): + self.clear() + print(paragraphs[index]) + if index + 1 == nb_par: return input(">") + else: input() + + def set_cell(self, x_offset, y_offset, x, y, value): + self._on_screen[y - y_offset][x - x_offset] = value + + def get_cell(self, x_offset, y_offset, x, y): + if 0 <= x - x_offset < self.screen_width and 0 <= y - y_offset <= self.screen_height: + return self._on_screen[y - y_offset][x - x_offset] + else: return " " + class Event: def __init__(self, xp, text, answer=0, *stat): self.xp = xp @@ -256,10 +275,24 @@ class Event: class Map: def __init__(self, map_data, entities, *coords): self.map_data = map_data - self.entities = entities + if entities: self.entities = [Entity(*i) for i in entities] + else: self.entities = [] self.coords = coords +class Entity: + def __init__(self, entity_id, symbol, x, y, behavior, *args): + self.entity_id = entity_id + self.symbol = symbol + self.pos_x = x + self.pos_y = y + self.behavior = behavior + self.args = list(args) + def change_behavior(self, new_behavior): + self.behavior = new_behavior + + +# Functions used by Asci def convert(string, force_int=False): try: return int(string) except: @@ -305,6 +338,11 @@ def read_event(data, event, quest): return Event(*event) +def get_data_copy(data): + return [data[0], data[1], data[2], data[3], data[4]] + + +# 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] nb = len(paragraphs) @@ -321,3 +359,21 @@ def print_text(text, min_value=0, max_value=0, default_value=0): return result else: input() + + +def stand_by(entity, data, stat, screen, walkable): + pass + +def follow(entity, data, stat, screen, walkable): + if data[4] == 1 and screen.get_cell(data[2] - 10, data[3] - 3, data[2] + 1, data[3]) in walkable: entity.pos_x, entity.pos_y = data[2] + 1, data[3] + elif data[4] == 2 and screen.get_cell(data[2] - 10, data[3] - 3, data[2], data[3] - 1) in walkable: entity.pos_x, entity.pos_y = data[2], data[3] - 1 + elif data[4] == 3 and screen.get_cell(data[2] - 10, data[3] - 3, data[2] - 1, data[3]) in walkable: entity.pos_x, entity.pos_y = data[2] - 1, data[3] + elif data[4] == 5 and screen.get_cell(data[2] - 10, data[3] - 3, data[2], data[3] + 1) in walkable: entity.pos_x, entity.pos_y = data[2], data[3] + 1 + +def walk(entity, data, stat, screen, walkable): + frame = (entity.args[0] + 1) % len(entity.args[1]) + new_x, new_y = entity.args[1][frame] + print(new_x, new_y) + if screen.get_cell(data[2] - 10, data[3] - 3, new_x, new_y) in walkable: + entity.pos_x, entity.pos_y = new_x, new_y + entity.args[0] = frame From faa5ffa23e77f1e02718c48bed05b9185445861b Mon Sep 17 00:00:00 2001 From: Shadow15510 Date: Fri, 28 Jan 2022 23:12:59 +0100 Subject: [PATCH 6/6] Minor fixes --- asci.py | 28 +++++++++++++++------------- samples/sample_1.py | 28 +++++++--------------------- samples/sample_2.py | 2 +- samples/sample_3.py | 10 ---------- 4 files changed, 23 insertions(+), 45 deletions(-) diff --git a/asci.py b/asci.py index 8caea0b..14cd5bc 100644 --- a/asci.py +++ b/asci.py @@ -38,16 +38,16 @@ class Asci: def _cell_test(self, direction): if direction == 1: if self.data[2] - 1 < 0: return -1 - else: cell = self.screen.get_cell(self.data[2] - 10, self.data[3] - 3, self.data[2] - 1, self.data[3]) + else: cell = self.screen.get_cell(self.data[2: 4], self.data[2] - 1, self.data[3]) if direction == 3: if self.data[2] + 1 >= self.map_width: return -1 - else: cell = self.screen.get_cell(self.data[2] - 10, self.data[3] - 3, self.data[2] + 1, self.data[3]) + else: cell = self.screen.get_cell(self.data[2: 4], self.data[2] + 1, self.data[3]) if direction == 5: if self.data[3] - 1 < 0: return -1 - else: cell = self.screen.get_cell(self.data[2] - 10, self.data[3] - 3, self.data[2], self.data[3] - 1) + else: cell = self.screen.get_cell(self.data[2: 4], self.data[2], self.data[3] - 1) if direction == 2: if self.data[3] + 1 >= self.map_height: return -1 - else: cell = self.screen.get_cell(self.data[2] - 10, self.data[3] - 3, self.data[2], self.data[3] + 1) + else: cell = self.screen.get_cell(self.data[2: 4], self.data[2], self.data[3] + 1) print(f"'{cell}'") cell_patterns = self._legend @@ -113,7 +113,7 @@ class Asci: data_copy = [self.data[0], self.data[1], x, y, self.data[4]] # Get the event - event = self._game_events_mapping[cell_content](data_copy, self.stat, self.visible_entities, self._get_entity_id(x, y)) + event = self._game_events_mapping[cell_content](data_copy, self.stat, self.current_map.entities, self._get_entity_id(x, y)) if type(event) == tuple: quest, event = event else: @@ -259,9 +259,11 @@ class Screen: def set_cell(self, x_offset, y_offset, x, y, value): self._on_screen[y - y_offset][x - x_offset] = value - def get_cell(self, x_offset, y_offset, x, y): - if 0 <= x - x_offset < self.screen_width and 0 <= y - y_offset <= self.screen_height: - return self._on_screen[y - y_offset][x - x_offset] + def get_cell(self, offsets, x, y): + x = x - (offsets[0] - 10) + y = y - (offsets[1] - 3) + if 0 <= x < self.screen_width and 0 <= y <= self.screen_height: + return self._on_screen[y][x] else: return " " class Event: @@ -365,15 +367,15 @@ def stand_by(entity, data, stat, screen, walkable): pass def follow(entity, data, stat, screen, walkable): - if data[4] == 1 and screen.get_cell(data[2] - 10, data[3] - 3, data[2] + 1, data[3]) in walkable: entity.pos_x, entity.pos_y = data[2] + 1, data[3] - elif data[4] == 2 and screen.get_cell(data[2] - 10, data[3] - 3, data[2], data[3] - 1) in walkable: entity.pos_x, entity.pos_y = data[2], data[3] - 1 - elif data[4] == 3 and screen.get_cell(data[2] - 10, data[3] - 3, data[2] - 1, data[3]) in walkable: entity.pos_x, entity.pos_y = data[2] - 1, data[3] - elif data[4] == 5 and screen.get_cell(data[2] - 10, data[3] - 3, data[2], data[3] + 1) in walkable: entity.pos_x, entity.pos_y = data[2], data[3] + 1 + if data[4] == 1 and screen.get_cell(data[2: 4], data[2] + 1, data[3]) in walkable: entity.pos_x, entity.pos_y = data[2] + 1, data[3] + elif data[4] == 2 and screen.get_cell(data[2: 4], data[2], data[3] - 1) in walkable: entity.pos_x, entity.pos_y = data[2], data[3] - 1 + elif data[4] == 3 and screen.get_cell(data[2: 4], data[2] - 1, data[3]) in walkable: entity.pos_x, entity.pos_y = data[2] - 1, data[3] + elif data[4] == 5 and screen.get_cell(data[2: 4], data[2], data[3] + 1) in walkable: entity.pos_x, entity.pos_y = data[2], data[3] + 1 def walk(entity, data, stat, screen, walkable): frame = (entity.args[0] + 1) % len(entity.args[1]) new_x, new_y = entity.args[1][frame] print(new_x, new_y) - if screen.get_cell(data[2] - 10, data[3] - 3, new_x, new_y) in walkable: + if screen.get_cell(data[2: 4], new_x, new_y) in walkable: entity.pos_x, entity.pos_y = new_x, new_y entity.args[0] = frame diff --git a/samples/sample_1.py b/samples/sample_1.py index d7d2655..30beed7 100644 --- a/samples/sample_1.py +++ b/samples/sample_1.py @@ -1,10 +1,10 @@ -from asci_v2 import * +from asci import * -monde = (r""" +maison = (r""" _ ### /o\__ ##### -|_ <>\ ### -|^|__| /_\ +| <>\ ### +|____| /_\ @@ -13,24 +13,10 @@ monde = (r""" # Entités [ ("sdf", "*", 2, 5, "stand by") -], -# Portes -(1, 3, 1, 5, 7)) +]) -maison = (r""" -+--+--+--------+--+--+ -| | | | | | -| + + + + | -| | -| + + + + | -+--/ \--------/ \--+ -| | -+---|^|--------------+""", -[], -(5, 7, 0, 1, 3)) - -carte_monde = (monde, maison) +carte_monde = (maison,) @@ -71,4 +57,4 @@ touche = {6: affichage_stat} def mon_jeu(): rpg_python = Asci(carte_monde, evenements, touche) - rpg_python.mainloop(7, stat=[100, 5], data=[{"main": 0}, 0, 10, 3]) \ No newline at end of file + rpg_python.mainloop(7, stat=[100, 5], data=[{"main": 0}, 0, 10, 3]) diff --git a/samples/sample_2.py b/samples/sample_2.py index 71ff75e..d016af0 100644 --- a/samples/sample_2.py +++ b/samples/sample_2.py @@ -84,4 +84,4 @@ touche = {8: affichage_statistique} def mon_jeu(): rpg_python = Asci(cartes, evenements, touche) - rpg_python.mainloop(13, [100]) \ No newline at end of file + rpg_python.mainloop(11, [100], [{"main": 0}, 0, 10, 3]) \ No newline at end of file diff --git a/samples/sample_3.py b/samples/sample_3.py index e171dd5..84f4a5e 100644 --- a/samples/sample_3.py +++ b/samples/sample_3.py @@ -67,16 +67,6 @@ def pnj(data, stat, entites, identifiant): 4: [1, "Merci, tu as rendu un grand service a mon ami !"] } - elif identifiant == "bandit": - # Si le bandit vient d'être tué - if xp == 3: return [1, "Vous avez reussi la quete !"] - - # Si le bandit est encore vivant - elif xp < 3: return [0, "Qu'est-ce que tu regardes toi ? Casses-toi !"] - - # Si le bandit est déjà mort - else: return [0, "Vous regardez le cadavre froid du bandit."] - return [0, "Hmm ?"]