From a529038ee275326d4e12446a67b1258a2f1bc56e Mon Sep 17 00:00:00 2001 From: Shadow15510 Date: Fri, 28 Jan 2022 12:33:28 +0100 Subject: [PATCH] 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