Guest User

Untitled

a guest
Sep 27th, 2026
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.27 KB | None | 0 0
  1. from pathlib import Path
  2. import json
  3.  
  4. path = Path("data.json")
  5. list_of_characters = []
  6.  
  7. # List of jobs
  8. warrior_class = {'strength': 5, 'dexterity': 4, 'agility': 3, 'endurance': 5, 'intelligence': 3, 'wisdom': 4,
  9. 'health': 50, 'mana': 30, 'growth': [3, 2, 1, 3, 1, 2, 10, 4], 'inventory': []}
  10. rogue_class = {'strength': 4, 'dexterity': 5, 'agility': 5, 'endurance': 3, 'intelligence': 4, 'wisdom': 3,
  11. 'health': 40, 'mana': 40, 'growth': [2, 3, 3, 1, 2, 1, 7, 7], 'inventory': []}
  12. mage_class = {'strength': 3, 'dexterity': 3, 'agility': 4, 'endurance': 4, 'intelligence': 5, 'wisdom': 5,
  13. 'health': 30, 'mana': 50, 'growth': [1, 1, 2, 2, 3, 3, 4, 10], 'inventory': []}
  14.  
  15. class RPG_Character:
  16. def __init__(self, name, **stats_dict):
  17. for stat in stats_dict:
  18. setattr(self, key, kwargs[key])
  19. self.name = name
  20. self.job = job
  21. self.gold = 50
  22. self.level = 1
  23.  
  24. def __str__(self):
  25. stat_page = f"""==========Stats Page==========
  26. Name: {self.name}
  27. Class: {self.job}
  28. Level: {self.level}
  29. -Health: {self.health}
  30. -Mana: {self.mana}
  31. -Strength: {self.strength}
  32. -Dexterity: {self.dexterity}
  33. -Agility: {self.agility}
  34. -Endurance: {self.endurance}
  35. -Intelligence: {self.intelligence}
  36. -Wisdom: {self.wisdom}
  37.  
  38. Gold: {self.gold}
  39. ==========Stats Page=========="""
  40. return stat_page
  41.  
  42. def add_gold(self, amount):
  43. self.gold += amount
  44.  
  45. def remove_gold(self, amount):
  46. self.gold -= amount
  47. if self.gold < 0:
  48. print("Gold amount can't be below zero.")
  49. self.gold += amount
  50.  
  51. def increase_level(self):
  52. self.level += 1
  53. self.strength += self.growth[0]
  54. self.dexterity += self.growth[1]
  55. self.agility += self.growth[2]
  56. self.endurance += self.growth[3]
  57. self.intelligence += self.growth[4]
  58. self.wisdom += self.growth[5]
  59. self.health += self.growth[6]
  60. self.mana += self.growth[7]
  61.  
  62. def add_item(self, item):
  63. self.inventory.append(item)
  64.  
  65. def show_inventory(self):
  66. print("=====Inventory=====")
  67. for item in self.inventory:
  68. print(item)
  69. print("=====Inventory=====")
  70.  
  71. def to_dict(self):
  72. saved_inventory = []
  73. for item in self.inventory:
  74. saved_inventory.append(item.to_dict())
  75. saved_dict = {
  76. 'name': self.name,
  77. 'class': self.job,
  78. 'level': self.level,
  79. 'health': self.health,
  80. 'mana': self.mana,
  81. 'strength': self.strength,
  82. 'dexterity': self.dexterity,
  83. 'agility': self.agility,
  84. 'endurance': self.endurance,
  85. 'intelligence': self.intelligence,
  86. 'wisdom': self.wisdom,
  87. 'gold': self.gold,
  88. 'inventory': saved_inventory
  89. }
  90. return saved_dict
  91.  
  92. class Item:
  93. def __init__(self, name, item_type, buy_price):
  94. self.name = name
  95. self.item_type = item_type
  96. self.buy_price = buy_price
  97. self.sell_price = buy_price // 4
  98.  
  99. def __str__(self):
  100. item_info = f"""Name: {self.name}
  101. Type: {self.item_type}
  102. """
  103. return item_info
  104.  
  105. def to_dict(self):
  106. saved_dict = {
  107. 'name': self.name,
  108. 'type': self.item_type,
  109. 'buy_price': self.buy_price,
  110. 'sell_price': self.sell_price
  111. }
  112. return saved_dict
  113.  
  114. # List of items
  115. basic_health_potion = Item("Basic Health Potion", "Consumable", 16)
  116. basic_mana_potion = Item("Basic Mana Potion", "Consumable", 20)
  117. wooden_shield = Item("Wooden Shield", "Equipment", 40)
  118.  
  119. def convert_dict_to_obj_inv(list_of_dicts):
  120. list_of_items = []
  121. for temp_dict in list_of_dicts:
  122. current_item = Item(temp_dict['name'], temp_dict['type'], temp_dict['buy_price'])
  123. list_of_items.append(current_item)
  124. return list_of_items
  125.  
  126. if path.exists():
  127. contents = path.read_text()
  128. temp_list_of_characters = json.loads(contents)
  129. print("File successfully retrieved")
  130. for character in temp_list_of_characters:
  131. new_character = convert_back_to_obj(character)
  132. list_of_characters.append(new_character)
  133.  
  134. for character in list_of_characters:
  135. print(character)
  136. print(character.show_inventory())
  137. else:
  138. # Adds new characters
  139. new_warrior = RPG_Character('John', warrior_class)
  140. list_of_characters.append(new_warrior)
  141. print(new_warrior)
  142. new_warrior.add_gold(100)
  143. print(new_warrior)
  144. new_warrior.remove_gold(80)
  145. print(new_warrior)
  146. new_rogue = RPG_Character('Toby', rogue_class)
  147. new_mage = RPG_Character('Harris', mage_class)
  148. list_of_characters.append(new_rogue)
  149. list_of_characters.append(new_mage)
  150. print(new_rogue)
  151. print(new_mage)
  152. new_mage.increase_level()
  153. print(new_mage)
  154.  
  155. new_rogue.add_item(basic_health_potion)
  156. new_rogue.add_item(basic_mana_potion)
  157. new_warrior.add_item(wooden_shield)
  158. new_rogue.show_inventory()
  159. new_warrior.show_inventory()
  160.  
  161. print(list_of_characters)
  162.  
  163. contents = json.dumps([ob.to_dict() for ob in list_of_characters])
  164. path.write_text(contents)
  165. print("File saved")
  166.  
Advertisement
Add Comment
Please, Sign In to add comment