import random import time from threading import Thread class Player: def __init__(self, name, hp=50, att=0, defense=13): self.name = name self.hp = hp self.att = att self.defense = defense self.inventory = [] def attack(self, enemy): # Implement the player's attack logic here print(f"{self.name} attacks the {enemy.name}...") time.sleep(1) # Simulate some action delay damage = max(0, self.att - enemy.defense) enemy.take_damage(damage) def defend(self): # Implement the player's defend logic here print(f"{self.name} defends...") time.sleep(1) # Simulate some action delay def take_damage(self, damage): # Implement the player taking damage logic here self.hp -= damage if self.hp <= 0: print("You have been defeated!") else: print(f"{self.name} takes {damage} damage. {self.name}'s HP: {self.hp}") time.sleep(1) # Simulate some action delay def collect_item(self, item): self.inventory.append(item) print(f"You collected {item}.") time.sleep(2) # Simulate some action delay def use_item(self, item): if item in self.inventory: if item == "Health Potion": self.hp += random.randint(10, 20) print(f"You used a Health Potion. Your HP is now {self.hp}") self.inventory.remove(item) elif item == "Strength Potion": self.att += 5 print(f"You used a Strength Potion. Your attack increased to {self.att}") self.inventory.remove(item) # Add more item effects as needed else: print("You don't have that item in your inventory.") class Room: def __init__(self, name, description, neighbors=None, monsters=None, items=None): self.name = name self.description = description self.neighbors = neighbors if neighbors else {} self.monsters = monsters if monsters else [] self.items = items if items else [] def add_neighbor(self, direction, room): self.neighbors[direction] = room def spawn_monster(self): if random.random() < 0.5: # 50% chance to spawn a monster return random.choice(self.monsters) else: return None class Monster: def __init__(self, name, hp, att, defense, attacks): self.name = name self.hp = hp self.att = att self.defense = defense self.attacks = attacks def attack(self, player): # Implement the monster's attack logic here print(f"The {self.name} attacks...") time.sleep(2) # Simulate some action delay damage = random.randint(1, 20) player.take_damage(damage) def take_damage(self, damage): # Implement the monster taking damage logic here self.hp -= damage if self.hp <= 0: print(f"The {self.name} has been defeated!") else: print(f"The {self.name} takes {damage} damage. {self.name}'s HP: {self.hp}") def print_map(current_room): print("Map:") print("--------------") print("| Hall |") print("|-----------|") print("| Foyer |") print("| |") print("| Bedroom |") print("| |") print("| Library |") print("|-----------|") print("| Garden |") print("| |") print("| Kitchen |") print("| |") print("| Cellar |") print("--------------") time.sleep(2) # Simulate some action delay print(f"\nYou are currently in the {current_room.name}.") print(current_room.description) print("Available actions:") for neighbor in current_room.neighbors: if current_room.neighbors[neighbor]: print(f"[{neighbor}] Go to {current_room.neighbors[neighbor].name}") print("[L] Look around") print("[C] Collect items") print("[U] Use item") print("[Q] Quit game") def fight_monster(current_room, player): while True: time.sleep(random.randint(10, 30)) # Simulate random time interval for monster encounters if current_room.monsters: monster = random.choice(current_room.monsters) print(f"You've encountered a {monster.name}!") while player.hp > 0 and monster.hp > 0: # Player attacks player.attack(monster) if monster.hp <= 0: break # Monster attacks monster.attack(player) if player.hp <= 0: break def main(): # Initialize player player_name = input("Enter your name: ") player = Player(player_name) # Initialize rooms hall = Room("Hall", "You find yourself in a dimly lit corridor, a musty smell fills your nostrils. There are no monsters or treasure in this area.") foyer = Room("Foyer", "You find yourself in a large foyer, there are four exits ... you hear a noise a monster attacks (Large Wolf).", items=["Health Potion"]) bedroom = Room("Bedroom", "A small room with a large bed and dusty furniture.", monsters=[Monster("Ghost", 40, 2, 8, ["Haunt"])], items=["Strength Potion"]) library = Room("Library", "A vast room with shelves full of old books and dusty tomes.", monsters=[Monster("Skeleton", 30, 1, 10, ["Strike"])], items=["Magic Scroll"]) # Define neighbors for each room hall.add_neighbor("N", foyer) foyer.add_neighbor("S", hall) foyer.add_neighbor("E", bedroom) bedroom.add_neighbor("W", foyer) foyer.add_neighbor("W", library) library.add_neighbor("E", foyer) current_room = hall # Start a thread for fighting monsters fight_thread = Thread(target=fight_monster, args=(current_room, player)) fight_thread.daemon = True fight_thread.start() while True: print_map(current_room) action = input("Choose your action: ").upper() if action in current_room.neighbors: # Move to the next room current_room = current_room.neighbors[action] elif action == "L": # Look around the room print(current_room.description) print("Items in the room:", current_room.items) elif action == "C": # Collect items in the room for item in current_room.items: player.collect_item(item) current_room.items = [] # Remove collected items from the room elif action == "U": # Use item from inventory print("Inventory:", player.inventory) item_to_use = input("Enter item to use: ") player.use_item(item_to_use) elif action == "Q": # Quit the game print("Thanks for playing!") break else: print("Invalid action!") time.sleep(1) # Add a delay after each action for readability if __name__ == "__main__": main()