Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pathlib import Path
- import json
- path = Path("data.json")
- list_of_characters = []
- # List of jobs
- warrior_class = {'job': 'Warrior', 'strength': 5, 'dexterity': 4, 'agility': 3, 'endurance': 5, 'intelligence': 3, 'wisdom': 4,
- 'health': 50, 'mana': 30, 'growth': [3, 2, 1, 3, 1, 2, 10, 4], 'inventory': []}
- rogue_class = {'job': 'Rogue', 'strength': 4, 'dexterity': 5, 'agility': 5, 'endurance': 3, 'intelligence': 4, 'wisdom': 3,
- 'health': 40, 'mana': 40, 'growth': [2, 3, 3, 1, 2, 1, 7, 7], 'inventory': []}
- mage_class = {'job': 'Mage', 'strength': 3, 'dexterity': 3, 'agility': 4, 'endurance': 4, 'intelligence': 5, 'wisdom': 5,
- 'health': 30, 'mana': 50, 'growth': [1, 1, 2, 2, 3, 3, 4, 10], 'inventory': []}
- class RPG_Character:
- def __init__(self, **stats_dict):
- for stat in stats_dict:
- setattr(self, stat, stats_dict[stat])
- def __str__(self):
- stat_page = f"""==========Stats Page==========
- Name: {self.name}
- Class: {self.job}
- Level: {self.level}
- -Health: {self.health}
- -Mana: {self.mana}
- -Strength: {self.strength}
- -Dexterity: {self.dexterity}
- -Agility: {self.agility}
- -Endurance: {self.endurance}
- -Intelligence: {self.intelligence}
- -Wisdom: {self.wisdom}
- Gold: {self.gold}
- ==========Stats Page=========="""
- return stat_page
- def add_gold(self, amount):
- self.gold += amount
- def remove_gold(self, amount):
- self.gold -= amount
- if self.gold < 0:
- print("Gold amount can't be below zero.")
- self.gold += amount
- def increase_level(self):
- self.level += 1
- self.strength += self.growth[0]
- self.dexterity += self.growth[1]
- self.agility += self.growth[2]
- self.endurance += self.growth[3]
- self.intelligence += self.growth[4]
- self.wisdom += self.growth[5]
- self.health += self.growth[6]
- self.mana += self.growth[7]
- def add_item(self, item):
- self.inventory.append(item)
- def show_inventory(self):
- print("=====Inventory=====")
- for item in self.inventory:
- print(item)
- print("=====Inventory=====")
- def to_dict(self):
- saved_inventory = []
- for item in self.inventory:
- saved_inventory.append(item.to_dict())
- saved_dict = {
- 'name': self.name,
- 'job': self.job,
- 'level': self.level,
- 'health': self.health,
- 'mana': self.mana,
- 'strength': self.strength,
- 'dexterity': self.dexterity,
- 'agility': self.agility,
- 'endurance': self.endurance,
- 'intelligence': self.intelligence,
- 'wisdom': self.wisdom,
- 'gold': self.gold,
- 'inventory': saved_inventory
- }
- return saved_dict
- class Item:
- def __init__(self, name, item_type, buy_price):
- self.name = name
- self.item_type = item_type
- self.buy_price = buy_price
- self.sell_price = buy_price // 4
- def __str__(self):
- item_info = f"""Name: {self.name}
- Type: {self.item_type}
- Buy Value: {self.buy_price}
- Sell Value: {self.sell_price}
- """
- return item_info
- def to_dict(self):
- saved_dict = {
- 'name': self.name,
- 'type': self.item_type,
- 'buy_price': self.buy_price,
- 'sell_price': self.sell_price
- }
- return saved_dict
- # List of items
- basic_health_potion = Item("Basic Health Potion", "Consumable", 16)
- basic_mana_potion = Item("Basic Mana Potion", "Consumable", 20)
- wooden_shield = Item("Wooden Shield", "Equipment", 40)
- def convert_dict_to_obj_inv(list_of_dicts):
- list_of_items = []
- for temp_dict in list_of_dicts:
- current_item = Item(temp_dict['name'], temp_dict['type'], temp_dict['buy_price'])
- list_of_items.append(current_item)
- return list_of_items
- if path.exists():
- contents = path.read_text()
- temp_list_of_characters = json.loads(contents)
- print("File successfully retrieved")
- for character in temp_list_of_characters:
- new_character = RPG_Character(**character)
- new_character.inventory = convert_dict_to_obj_inv(new_character.inventory)
- list_of_characters.append(new_character)
- for character in list_of_characters:
- print(character)
- print(character.show_inventory())
- else:
- # Adds new characters
- new_warrior = RPG_Character(**warrior_class)
- new_warrior.name = 'John'
- new_warrior.gold = 50
- new_warrior.level = 1
- list_of_characters.append(new_warrior)
- print(new_warrior)
- new_warrior.add_gold(100)
- print(new_warrior)
- new_warrior.remove_gold(80)
- print(new_warrior)
- new_rogue = RPG_Character(**rogue_class)
- new_rogue.name = 'Toby'
- new_rogue.gold = 50
- new_rogue.level = 1
- new_mage = RPG_Character(**mage_class)
- new_mage.name = 'Harris'
- new_mage.gold = 50
- new_mage.level = 1
- list_of_characters.append(new_rogue)
- list_of_characters.append(new_mage)
- print(new_rogue)
- print(new_mage)
- new_mage.increase_level()
- print(new_mage)
- new_rogue.add_item(basic_health_potion)
- new_rogue.add_item(basic_mana_potion)
- new_warrior.add_item(wooden_shield)
- new_rogue.show_inventory()
- new_warrior.show_inventory()
- print(list_of_characters)
- contents = json.dumps([ob.to_dict() for ob in list_of_characters])
- path.write_text(contents)
- print("File saved")
Advertisement
Add Comment
Please, Sign In to add comment