Certainly! Below are the modified versions of your existing Python files with the added functionality to enchant items using magic scrolls. Players can now purchase magic scrolls from the store and use them to enchant their equipment, enhancing their attack or defense bonuses.
items.py
class Item:
def __init__(self, name, description):
self.name = name
self.description = description
def __str__(self):
return f"{self.name}: {self.description}"
class Consumable(Item):
def __init__(self, name, description, healing=0, cost={"gold": 0, "silver": 0, "copper": 0}):
super().__init__(name, description)
self.healing = healing
self.cost = cost # Cost in coins
class Equipment(Item):
def __init__(self, name, description, slot, attack_bonus=0, defense_bonus=0, cost={"gold": 0, "silver": 0, "copper": 0}, enchantment_level=0):
super().__init__(name, description)
self.slot = slot # e.g., 'Weapon', 'Armor'
self.attack_bonus = attack_bonus
self.defense_bonus = defense_bonus
self.cost = cost # Cost in coins
self.enchantment_level = enchantment_level # New attribute for enchantments
def __str__(self):
bonuses = []
if self.attack_bonus:
bonuses.append(f"Attack +{self.attack_bonus}")
if self.defense_bonus:
bonuses.append(f"Defense +{self.defense_bonus}")
if self.enchantment_level > 0:
bonuses.append(f"Enchanted (+{self.enchantment_level})")
bonus_str = ", ".join(bonuses) if bonuses else "No bonuses"
return f"{self.name} ({self.slot}): {self.description} [{bonus_str}]"
def enchant(self, scroll):
"""
Apply an enchantment to the equipment using a MagicScroll.
"""
if isinstance(scroll, MagicScroll):
self.enchantment_level += scroll.enchantment_power
# You can define how enchantments affect attack and defense
self.attack_bonus += scroll.enchantment_power
self.defense_bonus += scroll.enchantment_power
return True
return False
class MagicScroll(Consumable):
def __init__(self, name, description, enchantment_power=1, cost={"gold": 0, "silver": 0, "copper": 0}):
super().__init__(name, description, healing=0, cost=cost)
self.enchantment_power = enchantment_power
def __str__(self):
return f"{self.name}: {self.description} (Enchantment Power: +{self.enchantment_power})"
Changes Made:
- Added
enchantment_level attribute to the Equipment class to track the number of enchantments applied.
- Modified the
__str__ method in Equipment to display enchantment information.
- Added an
enchant method to the Equipment class to apply enchantments using a MagicScroll.
- Introduced a new
MagicScroll class inheriting from Consumable, representing the magic scrolls used for enchanting.
store.py
from items import *
from utils import *
class Store:
def __init__(self):
# Define items available for purchase
self.items = [
Consumable("Small Potion", "Heals 20 HP", healing=20, cost={"gold": 0, "silver": 10, "copper": 50}),
Consumable("Large Potion", "Heals 50 HP", healing=50, cost={"gold": 0, "silver": 25, "copper": 0}),
Consumable("Mega Potion", "Heals 100 HP", healing=100, cost={"gold": 1, "silver": 0, "copper": 0}),
Equipment("Iron Sword", "A sturdy iron sword.", slot="Weapon", attack_bonus=10, cost={"gold": 2, "silver": 0, "copper": 0}),
Equipment("Steel Shield", "A strong steel shield.", slot="Armor", defense_bonus=10, cost={"gold": 2, "silver": 50, "copper": 0}),
MagicScroll("Basic Enchantment Scroll", "Enchants an equipment by +1.", enchantment_power=1, cost={"gold": 1, "silver": 50, "copper": 0}),
MagicScroll("Advanced Enchantment Scroll", "Enchants an equipment by +2.", enchantment_power=2, cost={"gold": 3, "silver": 0, "copper": 0}),
]
def show_items(self):
print("\n--- Store Items ---")
for idx, item in enumerate(self.items, 1):
if isinstance(item, Consumable) and not isinstance(item, MagicScroll):
healing_info = f"Heals {item.healing} HP" if item.healing > 0 else "No healing"
cost_str = format_coins(item.cost)
print(f"{idx}. {item.name} - {item.description} | {healing_info} | Cost: {cost_str}")
elif isinstance(item, Equipment):
bonuses = []
if item.attack_bonus:
bonuses.append(f"Attack +{item.attack_bonus}")
if item.defense_bonus:
bonuses.append(f"Defense +{item.defense_bonus}")
if item.enchantment_level > 0:
bonuses.append(f"Enchanted (+{item.enchantment_level})")
bonus_str = ", ".join(bonuses) if bonuses else "No bonuses"
cost_str = format_coins(item.cost)
print(f"{idx}. {item.name} ({item.slot}) - {item.description} | {bonus_str} | Cost: {cost_str}")
elif isinstance(item, MagicScroll):
cost_str = format_coins(item.cost)
print(f"{idx}. {item.name} - {item.description} | Enchantment Power: +{item.enchantment_power} | Cost: {cost_str}")
print("---------------------\n")
def buy_item(self, player):
self.show_items()
try:
choice = int(input("Select the number of the item you want to buy (0 to cancel): "))
if choice == 0:
return
if 1 <= choice <= len(self.items):
item = self.items[choice - 1]
if player.spend_coins(item.cost):
# Deep copy to add to player's inventory
if isinstance(item, Consumable) and not isinstance(item, MagicScroll):
bought_item = Consumable(item.name, item.description, item.healing, item.cost)
elif isinstance(item, Equipment):
bought_item = Equipment(
item.name,
item.description,
item.slot,
item.attack_bonus,
item.defense_bonus,
item.cost,
item.enchantment_level
)
elif isinstance(item, MagicScroll):
bought_item = MagicScroll(
item.name,
item.description,
item.enchantment_power,
item.cost
)
player.add_item(bought_item)
print(f"You purchased {item.name}.")
else:
print("Purchase failed due to insufficient funds.")
else:
print("Invalid selection.")
except ValueError:
print("Invalid input.")
Changes Made:
- Added
MagicScroll items to the store with different enchantment powers and costs.
- Modified the
show_items method to display magic scrolls with their specific enchantment powers.
- Updated the
buy_item method to handle purchasing MagicScroll instances.
player.py
class Player:
def __init__(self, name):
self.name = name
self.level = 1
self.exp = 0
self.exp_to_level = 100
self.base_max_health = 100
self.max_health = self.base_max_health
self.base_attack = 20
self.attack = self.base_attack
self.base_defense = 10
self.defense = self.base_defense
self.health = self.max_health
self.inventory = []
# Equipment slots: 'Weapon', 'Armor'
self.equipment = {
'Weapon': None,
'Armor': None
}
# Currency
self.coins = {"gold": 10, "silver": 50, "copper": 100} # Starting coins
def is_alive(self):
return self.health > 0
def gain_exp(self, amount):
self.exp += amount
print(f"\nYou gained {amount} XP.")
while self.exp >= self.exp_to_level:
self.level_up()
def level_up(self):
self.level += 1
self.exp -= self.exp_to_level
self.exp_to_level = int(self.exp_to_level * 1.5)
self.base_max_health += 20
self.base_attack += 5
self.base_defense += 3
self.max_health = self.base_max_health
self.health = self.max_health
self.update_stats()
print(f"\n*** Congratulations! You reached level {self.level}! ***")
print(f"Stats increased: Base Health: {self.base_max_health}, Base Attack: {self.base_attack}, Base Defense: {self.base_defense}\n")
def add_item(self, item):
self.inventory.append(item)
print(f"Added {item.name} to your inventory.")
def show_inventory(self):
if not self.inventory:
print("Your inventory is empty.")
else:
print("\n--- Inventory ---")
for idx, item in enumerate(self.inventory, 1):
print(f"{idx}. {item}")
print("-----------------\n")
def use_item(self):
consumables = [item for item in self.inventory if isinstance(item, Consumable) and not isinstance(item, MagicScroll)]
if not consumables:
print("You have no consumable items to use.")
return
print("\n--- Consumables ---")
for idx, item in enumerate(consumables, 1):
print(f"{idx}. {item}")
print("-------------------")
try:
choice = int(input("Select the number of the consumable you want to use (0 to cancel): "))
if choice == 0:
return
consumable = consumables[choice - 1]
if consumable.healing > 0:
self.health += consumable.healing
if self.health > self.max_health:
self.health = self.max_health
print(f"You used {consumable.name} and healed for {consumable.healing} HP.")
self.inventory.remove(consumable)
else:
print(f"{consumable.name} has no use.")
except (IndexError, ValueError):
print("Invalid choice.")
def equip_item(self):
equipables = [item for item in self.inventory if isinstance(item, Equipment)]
if not equipables:
print("You have no equipment items to equip.")
return
print("\n--- Equipable Items ---")
for idx, item in enumerate(equipables, 1):
print(f"{idx}. {item}")
print("------------------------")
try:
choice = int(input("Select the number of the item you want to equip (0 to cancel): "))
if choice == 0:
return
equipment = equipables[choice - 1]
slot = equipment.slot
currently_equipped = self.equipment.get(slot)
if currently_equipped:
print(f"Unequipping {currently_equipped.name} from {slot} slot.")
self.inventory.append(currently_equipped)
self.remove_equipment_bonus(currently_equipped)
self.equipment[slot] = equipment
self.inventory.remove(equipment)
self.apply_equipment_bonus(equipment)
print(f"Equipped {equipment.name} to {slot} slot.")
except (IndexError, ValueError):
print("Invalid choice.")
def unequip_item(self):
equipped = {slot: item for slot, item in self.equipment.items() if item is not None}
if not equipped:
print("You have no equipment equipped.")
return
print("\n--- Equipped Items ---")
for idx, (slot, item) in enumerate(equipped.items(), 1):
print(f"{idx}. {item} (Slot: {slot})")
print("----------------------")
try:
choice = int(input("Select the number of the item you want to unequip (0 to cancel): "))
if choice == 0:
return
slot = list(equipped.keys())[choice - 1]
item = self.equipment[slot]
self.equipment[slot] = None
self.inventory.append(item)
self.remove_equipment_bonus(item)
print(f"Unequipped {item.name} from {slot} slot.")
except (IndexError, ValueError):
print("Invalid choice.")
def show_equipment(self):
print("\n--- Equipped Items ---")
for slot, item in self.equipment.items():
if item:
print(f"{slot}: {item.name}")
else:
print(f"{slot}: None")
print("----------------------\n")
def show_stats(self):
print(f"\n--- {self.name}'s Stats ---")
print(f"Level: {self.level}")
print(f"EXP: {self.exp}/{self.exp_to_level}")
self.display_health_bar()
print(f"Attack: {self.attack}")
print(f"Defense: {self.defense}")
self.display_coins()
print("---------------------------\n")
def apply_equipment_bonus(self, equipment):
self.attack += equipment.attack_bonus
self.defense += equipment.defense_bonus
print(f"Stats updated: +{equipment.attack_bonus} Attack, +{equipment.defense_bonus} Defense.")
def remove_equipment_bonus(self, equipment):
self.attack -= equipment.attack_bonus
self.defense -= equipment.defense_bonus
print(f"Stats updated: -{equipment.attack_bonus} Attack, -{equipment.defense_bonus} Defense.")
def update_stats(self):
# Reset stats to base plus equipment bonuses
self.attack = self.base_attack
self.defense = self.base_defense
for equipment in self.equipment.values():
if equipment:
self.attack += equipment.attack_bonus
self.defense += equipment.defense_bonus
def display_health_bar(self):
# Display a visual health bar
bar_length = 20
health_ratio = self.health / self.max_health
filled_length = int(bar_length * health_ratio)
bar = '█' * filled_length + '-' * (bar_length - filled_length)
print(f"Health: |{bar}| {self.health}/{self.max_health} HP")
def display_coins(self):
# Display coins in a readable format
gold = self.coins["gold"]
silver = self.coins["silver"]
copper = self.coins["copper"]
print(f"Coins: {gold} Gold, {silver} Silver, {copper} Copper")
def add_coins(self, coin_dict):
# Add coins, handling the conversion if necessary
self.coins["copper"] += coin_dict.get("copper", 0)
self.coins["silver"] += coin_dict.get("silver", 0)
self.coins["gold"] += coin_dict.get("gold", 0)
# Convert excess copper to silver and silver to gold
self.convert_coins()
def convert_coins(self):
# 100 copper = 1 silver, 100 silver = 1 gold
# Convert copper to silver
if self.coins["copper"] >= 100:
self.coins["silver"] += self.coins["copper"] // 100
self.coins["copper"] = self.coins["copper"] % 100
# Convert silver to gold
if self.coins["silver"] >= 100:
self.coins["gold"] += self.coins["silver"] // 100
self.coins["silver"] = self.coins["silver"] % 100
def spend_coins(self, cost):
"""
Attempt to spend coins. Cost is a dict with 'gold', 'silver', 'copper'.
Returns True if successful, False otherwise.
"""
# First, calculate total copper
total_player = self.coins["gold"] * 10000 + self.coins["silver"] * 100 + self.coins["copper"]
total_cost = cost.get("gold", 0) * 10000 + cost.get("silver", 0) * 100 + cost.get("copper", 0)
if total_player < total_cost:
print("You don't have enough coins for this purchase.")
return False
# Deduct copper, then silver, then gold
remaining = total_cost
# Deduct gold
gold_needed = remaining // 10000
if gold_needed > self.coins["gold"]:
gold_needed = self.coins["gold"]
self.coins["gold"] -= gold_needed
remaining -= gold_needed * 10000
# Deduct silver
silver_needed = remaining // 100
if silver_needed > self.coins["silver"]:
silver_needed = self.coins["silver"]
self.coins["silver"] -= silver_needed
remaining -= silver_needed * 100
# Deduct copper
if remaining > self.coins["copper"]:
# Not enough copper, need to adjust
# Here, we'll assume coins have already been converted
print("Error in coin deduction.")
return False
self.coins["copper"] -= remaining
return True
def enchant_item(self):
"""
Enchant an equipped item using a MagicScroll from the inventory.
"""
magic_scrolls = [item for item in self.inventory if isinstance(item, MagicScroll)]
equipped_items = {slot: item for slot, item in self.equipment.items() if item is not None}
if not magic_scrolls:
print("You have no Magic Scrolls to use for enchanting.")
return
if not equipped_items:
print("You have no equipped items to enchant.")
return
print("\n--- Magic Scrolls ---")
for idx, scroll in enumerate(magic_scrolls, 1):
print(f"{idx}. {scroll}")
print("---------------------")
try:
scroll_choice = int(input("Select the Magic Scroll to use (0 to cancel): "))
if scroll_choice == 0:
return
if 1 <= scroll_choice <= len(magic_scrolls):
scroll = magic_scrolls[scroll_choice - 1]
else:
print("Invalid selection.")
return
except ValueError:
print("Invalid input.")
return
print("\n--- Equipped Items ---")
equipped_slots = list(equipped_items.keys())
for idx, slot in enumerate(equipped_slots, 1):
item = equipped_items[slot]
print(f"{idx}. {item} (Slot: {slot})")
print("----------------------")
try:
item_choice = int(input("Select the equipped item to enchant (0 to cancel): "))
if item_choice == 0:
return
if 1 <= item_choice <= len(equipped_slots):
slot = equipped_slots[item_choice - 1]
item = self.equipment[slot]
success = item.enchant(scroll)
if success:
print(f"Successfully enchanted {item.name} with {scroll.name}!")
self.inventory.remove(scroll)
else:
print("Enchantment failed.")
else:
print("Invalid selection.")
except ValueError:
print("Invalid input.")
Changes Made:
- Added an
enchant_item method to the Player class, allowing players to enchant their equipped items using MagicScrolls.
- Excluded
MagicScrolls from the regular use_item method, ensuring they are only used for enchanting.
- Updated the
add_item and show_inventory methods to handle MagicScrolls appropriately.
main.py
import random
from player import Player
from monster import Monster
from store import Store
from items import *
from utils import *
# Function to create random monsters
def generate_monsters():
# Decide the number of monsters in the encounter (e.g., 2-4)
num_monsters = random.randint(2, 4)
available_monsters = [
Monster("Goblin", 50, 10, 5, 50, [
Consumable("Small Potion", "Heals 20 HP", healing=20),
Equipment("Wooden Shield", "A basic shield made of wood.", slot="Armor", defense_bonus=5)
], coin_drop={"gold": 0, "silver": 5, "copper": 50}),
Monster("Skeleton", 60, 12, 8, 60, [
Equipment("Bone Shield", "A shield crafted from bones.", slot="Armor", defense_bonus=8),
Equipment("Short Sword", "A short but sharp sword.", slot="Weapon", attack_bonus=7)
], coin_drop={"gold": 0, "silver": 10, "copper": 70}),
Monster("Orc", 80, 15, 10, 80, [
Equipment("Orcish Axe", "A mighty axe that increases attack.", slot="Weapon", attack_bonus=10),
Consumable("Large Potion", "Heals 50 HP", healing=50)
], coin_drop={"gold": 0, "silver": 15, "copper": 100}),
Monster("Troll", 120, 20, 15, 120, [
Equipment("Troll Hide Armor", "Heavy armor made from troll hide.", slot="Armor", defense_bonus=15),
Consumable("Mega Potion", "Heals 100 HP", healing=100)
], coin_drop={"gold": 1, "silver": 0, "copper": 200}),
Monster("Dragon", 200, 30, 20, 300, [
Equipment("Dragon Scale Shield", "A very rare and powerful shield.", slot="Armor", defense_bonus=25),
Equipment("Dragon Slayer Axe", "An axe capable of slaying dragons.", slot="Weapon", attack_bonus=30)
], coin_drop={"gold": 5, "silver": 0, "copper": 500})
]
# Randomly select monsters, allowing repetition
monsters = []
for _ in range(num_monsters):
monster_template = random.choice(available_monsters)
# Create a copy to have independent health
monsters.append(Monster(monster_template.name, monster_template.max_health, monster_template.attack,
monster_template.defense, monster_template.exp, monster_template.drop_items,
monster_template.coin_drop))
return monsters
# Combat system
def combat(player, monsters):
print("\n=== Combat Start ===")
print(f"A group of {len(monsters)} monsters approaches!")
active_monsters = monsters.copy()
while active_monsters and player.is_alive():
print("\n--- Current Status ---")
player.display_health_bar()
print("\nMonsters:")
for idx, monster in enumerate(active_monsters, 1):
monster.display_health_bar()
print("----------------------")
print("\nChoose your action:")
print("1. Attack")
print("2. Use Consumable")
print("3. Equip Item")
print("4. Enchant Equipment")
print("5. View Stats")
print("6. Run")
choice = input("Enter your choice: ")
if choice == '1':
# Attack action
# Let player choose which monster to attack
try:
target = int(input(f"Select the monster to attack (1-{len(active_monsters)}): "))
if 1 <= target <= len(active_monsters):
monster = active_monsters[target - 1]
damage = max(player.attack - monster.defense, 1)
monster.health -= damage
print(f"\nYou attack the {monster.name} for {damage} damage.")
if not monster.is_alive():
print(f"You have defeated the {monster.name}!")
player.gain_exp(monster.exp)
player.add_coins(monster.coin_drop)
# Handle item drops
if monster.drop_items:
drop_chance = random.random()
if drop_chance < 0.7: # 70% chance to drop an item
dropped_item = random.choice(monster.drop_items)
print(f"The {monster.name} dropped a {dropped_item.name}!")
player.add_item(dropped_item)
active_monsters.remove(monster)
else:
print("Invalid target.")
continue
except ValueError:
print("Invalid input.")
continue
elif choice == '2':
player.use_item()
elif choice == '3':
player.equip_item()
elif choice == '4':
player.enchant_item()
elif choice == '5':
player.show_stats()
continue # Allow player to continue without monsters attacking
elif choice == '6':
# Attempt to run away; success chance depends on number of monsters
success_chance = 0.3 + (0.1 * len(active_monsters)) # Base 30% + 10% per monster
if random.random() < success_chance:
print("You successfully ran away from the battle!")
return
else:
print("Failed to run away!")
else:
print("Invalid choice.")
continue
# Check if all monsters are defeated
if not active_monsters:
print("\n*** You have defeated all the monsters! ***")
break
# Monsters' turn to attack
print("\n--- Monsters' Turn ---")
for monster in active_monsters:
if monster.is_alive():
damage = max(monster.attack - player.defense, 1)
player.health -= damage
print(f"The {monster.name} attacks you for {damage} damage.")
if not player.is_alive():
print("\nYou have been defeated. Game Over.")
exit()
print("-----------------------")
if not player.is_alive():
print("\nYou have been defeated. Game Over.")
exit()
print("=== Combat End ===\n")
# Main game loop
def main():
store = Store()
print("=== Welcome to the Text RPG Game ===")
player_name = input("Enter your character's name: ")
player = Player(player_name)
print(f"\nWelcome, {player.name}! Your adventure begins...\n")
while player.is_alive():
print("Choose an action:")
print("1. Explore (Fight monsters)")
print("2. View Inventory")
print("3. View Equipment")
print("4. View Stats")
print("5. Use Consumable")
print("6. Equip Item")
print("7. Unequip Item")
print("8. Enchant Equipment")
print("9. Visit Store")
print("10. Quit Game")
choice = input("Enter your choice: ")
if choice == '1':
monsters = generate_monsters()
combat(player, monsters)
elif choice == '2':
player.show_inventory()
elif choice == '3':
player.show_equipment()
elif choice == '4':
player.show_stats()
elif choice == '5':
player.use_item()
elif choice == '6':
player.equip_item()
elif choice == '7':
player.unequip_item()
elif choice == '8':
player.enchant_item()
elif choice == '9':
store.buy_item(player)
elif choice == '10':
print("Thank you for playing! Goodbye.")
break
else:
print("Invalid choice. Please select a valid action.")
if not player.is_alive():
print("You have perished on your journey. Game Over.")
def add_monster_health_bar():
def display_health_bar(self):
bar_length = 20
health_ratio = self.health / self.max_health
filled_length = int(bar_length * health_ratio)
bar = '█' * filled_length + '-' * (bar_length - filled_length)
print(f"{self.name}: |{bar}| {self.health}/{self.max_health} HP")
Monster.display_health_bar = display_health_bar
# Run the game
if __name__ == "__main__":
add_monster_health_bar()
main()
Changes Made:
- Added an option to enchant equipment in both combat (
choice == '4') and the main game loop (print("8. Enchant Equipment") and corresponding elif choice == '8':).
- Ensured that enchantments can be performed both inside and outside combat.
utils.py
def format_coins(coin_dict):
parts = []
if coin_dict.get("gold", 0) > 0:
parts.append(f"{coin_dict['gold']}G")
if coin_dict.get("silver", 0) > 0:
parts.append(f"{coin_dict['silver']}S")
if coin_dict.get("copper", 0) > 0:
parts.append(f"{coin_dict['copper']}C")
return ' '.join(parts) if parts else "Free"
Changes Made:
- No changes were necessary in this file for the enchantment feature.
Additional Notes:
-
Enchantment Workflow:
- Purchasing Magic Scrolls: Players can buy
MagicScroll items from the store.
- Enchanting Equipment: Players can use the
enchant_item method from the Player class to apply a MagicScroll to an equipped Equipment item, enhancing its attack_bonus and/or defense_bonus based on the scroll's enchantment_power.
-
UI Enhancements:
- Display Enchantments: The
__str__ methods are updated to show enchantment levels on equipment.
- Store Display: Magic Scrolls are clearly listed with their enchantment capabilities.
-
Balance Considerations:
- Enchantment Power vs. Cost: Ensure that the cost of enchantment scrolls appropriately reflects their power to maintain game balance.
-
Error Handling:
- The
enchant_item method includes checks to ensure that only MagicScroll items are used for enchanting and that there are equipped items to enchant.
-
Future Enhancements:
- Limit Enchantments: You might want to set a maximum enchantment level to prevent equipment from becoming overpowered.
- Different Scroll Types: Introduce scrolls that enhance specific attributes or provide special abilities.
Feel free to test these changes and adjust the enchantment mechanics to best fit your game's design!