Paster500

RPG_Character_Refactored

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