From b42e460fcd098eb7f563720b5bf0cf96b4554e55 Mon Sep 17 00:00:00 2001 From: Swagata Jana Date: Fri, 23 Sep 2022 11:02:07 +0530 Subject: [PATCH] added currency to the game. --- builtin.py | 28 ++++++++++++++++++++-------- crpg.py | 40 ++++++++++++++++++++++++---------------- game-1.py | 13 +++++++------ game-2-layout.dl | 23 ++++++++++++----------- setup.py | 7 +++++-- 5 files changed, 68 insertions(+), 43 deletions(-) diff --git a/builtin.py b/builtin.py index ab35f8e..083ec9c 100644 --- a/builtin.py +++ b/builtin.py @@ -1,13 +1,16 @@ -from __future__ import annotations #needed for Node type hints +from __future__ import annotations # needed for Node type hints + + # Base player classes class Player: def __init__(self): self.hp = 100 self.xp = 0 + self.coins_earned = 0 def summary(self): - return f'hp: {self.hp} xp: {self.xp}' + return f'hp: {self.hp} xp: {self.xp} coins: {self.coins_earned}' # Base connection classes @@ -23,28 +26,33 @@ def on_select(self): def __str__(self): return str(self.from_node) + " -> " + str(self.to_node) + # Breaking connections can only be used once class BreakingConnection(Connection): def __init__(self, from_node: Node, to_node: Node): super().__init__(from_node, to_node) - + def on_select(self): self.from_node.connections.remove(self) def __str__(self): - return str(self.from_node) + " \-> " + str(self.to_node) + return str(self.from_node) + " \\-> " + str(self.to_node) # Base node classes class Node: - def __init__(self, title: str, desc: str): + def __init__(self, title: str, desc: str, coins: str): + super().__init__() self.title = title self.desc = desc + self.coins = int(coins) + # self.player = player self.connections: list[Connection] = [] def on_select(self): print(self.desc) + # self.player.coins_earned += self.coins def add_connection(self, other: Node, type: Connection = Connection): self.connections += [type(self, other)] @@ -66,24 +74,28 @@ class Location(Node): # Actions include the player for reference (augment player attributes) class Action(Node): - def __init__(self, title: str, desc: str, player: Player): + def __init__(self, title: str, desc: str, coins: str, player: Player): self.player = player - super().__init__(title, desc) + super().__init__(title, desc, coins) def on_select(self): print(self.player.summary()) + self.player.coins_earned += self.coins + super(Action, self).on_select() class Fight(Action): def on_select(self): self.player.hp -= 25 self.player.xp += 10 + self.player.coins_earned += 50 super().on_select() class Run(Action): def on_select(self): + self.player.coins_earned += 10 self.player.xp = max(self.player.xp - 10, 0) - super().on_select() \ No newline at end of file + super().on_select() diff --git a/crpg.py b/crpg.py index 2fa46fe..2a8e294 100644 --- a/crpg.py +++ b/crpg.py @@ -1,18 +1,20 @@ -from typing import Callable # for connection function type hints +from typing import Callable # for connection function type hints from setup import start import re -from builtin import Connection, BreakingConnection, Node, Location, Fight, Run, Player +from builtin import Connection, BreakingConnection, Node, Location, Fight, Run, Player, Action + # the base crpg game class Game: def __init__(self, player: Player = Player(), starting_location: Location = None): + self.name = start() self.current: Node = starting_location self.player = player - self.name: str = None + self.name: str self.nodes: set[Node] = set() - + self.add_node(self.current) # establish nodes and connections @@ -25,7 +27,8 @@ def add_connection(self, from_node: Node, to_node: Node, connection_type: Connec from_node.add_connection(to_node, connection_type) - def add_twoway_connection(self, node_a: Node, node_b: Node, connection_type_a: Connection = Connection, connection_type_b: Connection = Connection): + def add_twoway_connection(self, node_a: Node, node_b: Node, connection_type_a: Connection = Connection, + connection_type_b: Connection = Connection): assert node_a in self.nodes assert node_b in self.nodes @@ -36,7 +39,7 @@ def add_twoway_connection(self, node_a: Node, node_b: Node, connection_type_a: C def list_next(self): _next = self.current.connections for i, connection in enumerate(_next): - print(f'{i+1}) {connection.to_node}') + print(f'{i + 1}) {connection.to_node}') # Iterate through graph, ask for choices at each node def ask(self, query: str): @@ -51,10 +54,10 @@ def ask(self, query: str): choice = int(input(f'{query}')) except ValueError: print('Please enter a number. Try again.') - continue + continue - # check for valid choice - if (choice <= len(_next) and choice > 0): + # check for valid choice + if len(_next) >= choice > 0: break else: print('Please enter a valid number. Try again.') @@ -62,7 +65,6 @@ def ask(self, query: str): self.current = self.current.advance(choice - 1) def start(self): - self.name = start() print(f'Welcome, {self.name}') while self.player.hp > 0: @@ -81,7 +83,7 @@ def generate(filename): node_mapping: dict[str, Node] = {} n_types = { "starting": Location, - "node": Node, + "node": Action, "location": Location, "fight": Fight, "run": Run @@ -89,11 +91,13 @@ def generate(filename): c_funcs: dict[str, Callable[[Game, Node, Node], None]] = { "->": lambda game, node_a, node_b: game.add_connection(node_a, node_b), - "\->": lambda game, node_a, node_b: game.add_connection(node_a, node_b, BreakingConnection), + "\\->": lambda game, node_a, node_b: game.add_connection(node_a, node_b, BreakingConnection), "<->": lambda game, node_a, node_b: game.add_twoway_connection(node_a, node_b), - "<-\->": lambda game, node_a, node_b: game.add_twoway_connection(node_a, node_b, BreakingConnection), - "<-/->": lambda game, node_a, node_b: game.add_twoway_connection(node_a, node_b, Connection, BreakingConnection), - "<-/\->": lambda game, node_a, node_b: game.add_twoway_connection(node_a, node_b, BreakingConnection, BreakingConnection), + "<-\\->": lambda game, node_a, node_b: game.add_twoway_connection(node_a, node_b, BreakingConnection), + "<-/->": lambda game, node_a, node_b: game.add_twoway_connection(node_a, node_b, Connection, + BreakingConnection), + "<-/\\->": lambda game, node_a, node_b: game.add_twoway_connection(node_a, node_b, BreakingConnection, + BreakingConnection), } for node in nodes.split("\n"): @@ -102,7 +106,11 @@ def generate(filename): if n_type == "fight" or n_type == "run": args.append(game.player) + if n_type == "node": + args.append(game.player) + obj = n_types[n_type](*args) + # print(obj) if n_type == "starting": game.current = obj @@ -124,4 +132,4 @@ def generate(filename): else: raise ConnectionError("Invalid .dl connection input") - return game \ No newline at end of file + return game diff --git a/game-1.py b/game-1.py index 13d2a5a..e2fbbaf 100644 --- a/game-1.py +++ b/game-1.py @@ -4,25 +4,26 @@ player = Player() starting_location = Location( "castle", - "You find yourself in the prison cell of an abandoned castle" + "You find yourself in the prison cell of an abandoned castle", + 0 ) game = Game(player, starting_location) # nodes -examine_skeleton = Node("examine skeleton", "You examine the skeleton of a corpse.") +examine_skeleton = Node("examine skeleton", "You examine the skeleton of a corpse.", 10) game.add_node(examine_skeleton) -open_cell_gate = Node("open cell gate", "The cell gate is unlocked. You push it open.") +open_cell_gate = Node("open cell gate", "The cell gate is unlocked. You push it open.", 1) game.add_node(open_cell_gate) -dungeon = Location("left corridor", "The corridor leads to a large flight of stairs to the dungeon") +dungeon = Location("left corridor", "The corridor leads to a large flight of stairs to the dungeon", 5) game.add_node(dungeon) -courtyard = Location("right corridor", "The corridor leads to an open door to the castle's courtyard") +courtyard = Location("right corridor", "The corridor leads to an open door to the castle's courtyard", 1) game.add_node(courtyard) -dragon_event = Node("scale stairs", "A dragon swoops down from the sky, blocking your path") +dragon_event = Node("scale stairs", "A dragon swoops down from the sky, blocking your path", 10) game.add_node(dragon_event) fight_dragon = Fight("fight dragon", "You vanquish the dragon", player) diff --git a/game-2-layout.dl b/game-2-layout.dl index 4aee0e5..34ab026 100644 --- a/game-2-layout.dl +++ b/game-2-layout.dl @@ -1,13 +1,14 @@ -1 | starting | castle | You wake up in the prison cell of an abandoned castle. -2 | node | examine skeleton | You examine the skeleton of a corpse. -3 | node | open cell gate | The cell gate is unlocked. You push it open. -4 | node | left corridor | The corridor leads to an open door to the castle's courtyard. -9 | location | courtyard | You enter the castle's courtyard. -5 | node | right corridor | The corridor leads to a large flight of stairs to the dungeon. -10 | location | dungeon | You enter the castle's dungeon. -6 | node | scale stairs | A dragon swoops down from the sky, blocking your path. -7 | fight | fight dragon | You vanquish the dragon. -8 | run | run from dragon | You run from the dragon. +1 | starting | castle | You wake up in the prison cell of an abandoned castle. | 0 +2 | node | examine skeleton | You examine the skeleton of a corpse. | 10 +3 | node | open cell gate | The cell gate is unlocked. You push it open. | 10 +4 | node | left corridor | The corridor leads to an open door to the castle's courtyard. | 10 +9 | location | courtyard | You enter the castle's courtyard. | 10 +5 | node | right corridor | The corridor leads to a large flight of stairs to the dungeon. | 10 +10 | location | dungeon | You enter the castle's dungeon. | 10 +6 | node | scale stairs | A dragon swoops down from the sky, blocking your path. | 10 +7 | fight | fight dragon | You vanquish the dragon. | 50 +8 | run | run from dragon | You run from the dragon. | 10 +11 | node | winner | You have won the game! | 10 --- 1 <-> 2 1 -> 3 @@ -18,5 +19,5 @@ 9 -> 6 6 -> 7 6 -> 8 -7 -> 4 +7 -> 11 8 -> 4 diff --git a/setup.py b/setup.py index 7a1508d..83b09fa 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,10 @@ import random from pyfiglet import Figlet -fonts = ["6x9", "banner", "big", "block", "bubble", "chartr", "chartri", "cour", "digital", "helv", "ivrit", "lean", "mini", "mnemonic", "sbook", "script", "shadow", "slant", "small", "smscript", "smshadow", "smslant", "standard", "term", "times", "xchartr"] + +fonts = ["6x9", "banner", "big", "block", "bubble", "chartr", "chartri", "cour", "digital", "helv", "ivrit", "lean", + "mini", "mnemonic", "sbook", "script", "shadow", "slant", "small", "smscript", "smshadow", "smslant", + "standard", "term", "times", "xchartr"] + def start(): font_idx = random.randint(0, len(fonts) - 1) @@ -15,4 +19,3 @@ def start(): ready = input("> ") if ready.lower() == "y" or ready.lower() == "yes": return name -