python a simple modular rpg engine example
import random import time # https://pythonprogrammingsnippets.com/ # Define the rooms in the world rooms = { "room1": { "description": "This is room 1", "exits": {"n": "room2", "e": "room3"} }, "room2": { "description": "This is room 2", "exits": {"s": "room1"} }, "room3": { "description": "This is room 3", "exits": {"n": "room4", "w": "room1"} }, "room4": { "description": "This is room 4", "exits": {"s": "room3"} } } # Define the Player class class Player: def __init__(self, current_room): self.current_room = current_room self.health = 100 self.is_player = True self.name = "Player" self.dam_min = 20 self.dam_max = 30 self.health = 100 def fight(self, monster): print("You are fighting the monster.") while True: if self.check_health(): break if monster.check_health(): break self.one_hit(monster) monster.one_hit(self) time.sleep(1) def attack_message(self, monster): print(f"{self.name} hit {monster.name} for {self.damage} damage.") def one_hit(self, monster): self.damage = random.randint(self.dam_min, self.dam_max) monster.health -= self.damage self.attack_message(monster) time.sleep(0.3) def check_health(self): if self.health <= 0: print("You died.") quit() return True else: return False # Define the Monster class class Monster(Player): def __init__(self, current_room, name="Monster", health=50, dam_min=5, dam_max=10): super().__init__(current_room) self.name = name self.health = health self.dam_min = dam_min self.dam_max = dam_max self.is_player = False # Place the player in room 1 player = Player("room1") # Place a monster in a random room monster_room = random.choice(list(rooms.keys())) possible_monsters = [{"name": "Goblin", "health": 50, "dam_min": 5, "dam_max": 10}, {"name": "Orc", "health": 100, "dam_min": 10, "dam_max": 20}, {"name": "Dragon", "health": 200, "dam_min": 20, "dam_max": 30}] class MonsterFactory: def __init__(self, possible_monsters): self.possible_monsters = possible_monsters self.monsters = [] def randomly_create_and_place_monster(self, room): monster = random.choice(self.possible_monsters) monster = Monster(room, monster["name"], monster["health"], monster["dam_min"], monster["dam_max"]) self.monsters.append(monster) return monster def get_monsters(self): return self.monsters def get_monsters_in_room(self, room): monsters_in_room = [] for monster in self.monsters: if monster.current_room == room: monsters_in_room.append(monster) return monsters_in_room monster_factory = MonsterFactory(possible_monsters) monster = monster_factory.randomly_create_and_place_monster(monster_room) # Define the look command def do_look(room): # get all npcs in room monsters_in_room = monster_factory.get_monsters_in_room(player.current_room) # print the room description with a nice ascii art border print("+" + "-" * 50 + "+") print("|" + " " * 50 + "|") print("|" + rooms[player.current_room]["description"].center(50) + "|") print("|" + " " * 50 + "|") print("+" + "-" * 50 + "+") # print the health print(f"Your health: {player.health}") # print the npcs if len(monsters_in_room) == 0: print("There are no monsters in this room.") else: print("Monsters in this room:") for monster in monsters_in_room: print(f"{monster.name} - Health: {monster.health}") # show the exits print("Exits:") for exit in rooms[player.current_room]["exits"]: print(exit, end=" ") print() # show the commands as a line print("Commands: look, quit, fight, spawn, n, s, e, w") def show_prompt(player): # show progress bar for players health [====================] health_bar = "" for i in range(0, player.health, 10): health_bar += "=" # print(f"Your health: [{health_bar}] {player.health} :", end="") return f"Your health: [{health_bar}] {player.health} : " # Main game loop while True: # do look command do_look(player.current_room) # Get the player's input command = input(show_prompt(player)) # Handle the command if command == "quit" or command == "exit" or command == "q": break elif command in rooms[player.current_room]["exits"]: player.current_room = rooms[player.current_room]["exits"][command] elif command == "fight": monsters_in_room = monster_factory.get_monsters_in_room(player.current_room) if len(monsters_in_room) == 0: print("There are no monsters in this room.") else: for monster in monsters_in_room: player.fight(monster) elif command == "spawn": monster = monster_factory.randomly_create_and_place_monster(player.current_room) print(f"A {monster.name} has spawned in this room.") # a really stylish ascii art version of the look command elif command == "look" or command == "l": do_look(player.current_room) elif command == "help" or command =="h" or command == "?": print("Commands: n, s, e, w, look, fight, spawn, quit") else: print("Unknown command.")
output:
+--------------------------------------------------+ | | | This is room 1 | | | +--------------------------------------------------+ Your health: 100 Monsters in this room: Orc - Health: 100 Exits: n e Commands: look, quit, fight, spawn, n, s, e, w Your health: [==========] 100 :

















