file 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}, enchantments=None):
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.enchantments = enchantments if enchantments else []
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.enchantments:
enchants = ', '.join([f"{ench.type} +{ench.value}" for ench in self.enchantments])
bonuses.append(f"Enchantments: {enchants}")
bonus_str = ", ".join(bonuses) if bonuses else "No bonuses"
return f"{self.name} ({self.slot}): {self.description} [{bonus_str}]"
class MagicScroll(Item):
def __init__(self, name, description, cost={"gold":0, "silver":20, "copper":0}):
super().__init__(name, description)
self.cost = cost # Cost in coins
def __str__(self):
return f"{self.name}: {self.description} | Cost: {format_coins(self.cost)}"
file 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()
file monster.py:
class Monster:
def __init__(self, name, health, attack, defense, exp, drop_items, coin_drop):
self.name = name
self.max_health = health
self.health = health
self.attack = attack
self.defense = defense
self.exp = exp
self.drop_items = drop_items # List of Item objects
self.coin_drop = coin_drop # Dict with 'gold', 'silver', 'copper'
def is_alive(self):
return self.health > 0
def __str__(self):
return f"{self.name}: {self.health}/{self.max_health} HP"
file 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)]
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_scrolls = [item for item in self.inventory if isinstance(item, MagicScroll)]
equipables = [item for item in self.equipment.values() if isinstance(item, Equipment)]
if not enchant_scrolls:
print("You have no magic scrolls to use for enchanting.")
return
if not equipables:
print("You have no equipped items to enchant.")
return
print("\n--- Magic Scrolls ---")
for idx, scroll in enumerate(enchant_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
scroll = enchant_scrolls[scroll_choice - 1]
except (IndexError, ValueError):
print("Invalid choice.")
return
print("\n--- Equipped Equipment ---")
equipped_items = [item for item in self.equipment.values() if isinstance(item, Equipment)]
for idx, equipment in enumerate(equipped_items, 1):
print(f"{idx}. {equipment}")
print("----------------------------")
try:
equip_choice = int(input("Select the equipment to enchant (0 to cancel): "))
if equip_choice == 0:
return
equipment = equipped_items[equip_choice - 1]
except (IndexError, ValueError):
print("Invalid choice.")
return
# Define possible enchantments
possible_enchantments = [
{"type": "Fire", "attack_bonus": 5},
{"type": "Ice", "attack_bonus": 5},
{"type": "Lightning", "attack_bonus": 5},
{"type": "Sturdy", "defense_bonus": 5},
{"type": "Reinforced", "defense_bonus": 5},
]
enchantment = random.choice(possible_enchantments)
print(f"\nApplying {enchantment['type']} enchantment to {equipment.name}.")
# Apply enchantment
equipment.enchantments.append(Enchantment(enchantment['type'], enchantment.get("attack_bonus", 0), enchantment.get("defense_bonus", 0)))
self.apply_enchantment_bonus(equipment.enchantments[-1])
self.inventory.remove(scroll)
print(f"{equipment.name} has been enchanted with {enchantment['type']}.")
def apply_enchantment_bonus(self, enchantment):
if enchantment.attack_bonus:
self.attack += enchantment.attack_bonus
print(f"Enchantment added: +{enchantment.attack_bonus} Attack.")
if enchantment.defense_bonus:
self.defense += enchantment.defense_bonus
print(f"Enchantment added: +{enchantment.defense_bonus} Defense.")
class Enchantment:
def __init__(self, type, attack_bonus=0, defense_bonus=0):
self.type = type
self.attack_bonus = attack_bonus
self.defense_bonus = defense_bonus
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}")
bonus_str = ", ".join(bonuses)
return f"{self.type} Enchantment ({bonus_str})"
file 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("Fire Enchantment Scroll", "Imbues your equipment with Fire magic.", cost={"gold":1, "silver":20, "copper":0}),
MagicScroll("Ice Enchantment Scroll", "Imbues your equipment with Ice magic.", cost={"gold":1, "silver":20, "copper":0}),
MagicScroll("Lightning Enchantment Scroll", "Imbues your equipment with Lightning magic.", cost={"gold":1, "silver":20, "copper":0}),
MagicScroll("Sturdy Enchantment Scroll", "Imbues your equipment with increased defense.", cost={"gold":1, "silver":20, "copper":0}),
MagicScroll("Reinforced Enchantment Scroll", "Imbues your equipment with reinforced defense.", cost={"gold":1, "silver":20, "copper":0}),
]
def show_items(self):
print("\n--- Store Items ---")
for idx, item in enumerate(self.items, 1):
if isinstance(item, Consumable):
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} (Consumable) - {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}")
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} (Magic Scroll) - {item.description} | 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):
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)
elif isinstance(item, MagicScroll):
bought_item = MagicScroll(item.name, item.description, 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.")
file 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"