如何将嵌套词典中的值添加到玩家的统计数据中?



我刚刚开始做我的第一个python项目,现在我正在寻求一些python智慧!

你将如何添加这些值(来自武器(。我知道要将模块导入到另一个模块中,但在我的类的值中添加"attach_power"(etc(值怎么样?

所以,只是为了把事情弄清楚。我想+=玩家装备的字典武器的值到玩家值的统计数据?

设备.py模块:

weapons = {
'starter_dagger':   {'attack_power': 4,
'agility': 10,
'luck': 1,
'description': 'Assassins starter weapon, used by only the newest members.'},
'starter_staff':    {'spell_power': 8,
'attack_power': 4,
'luck': 1,
'description': 'For the novice wizards.'},
'starter_longsword':    {'attack_power': 12,
'agility': -2,
'luck': 2,
'description': 'Unnecessarily heavy longsword, used to train the novice warriors.'},
'starter_torch':    {'attack_power': 6,
'agility': 6,
'spell_power': 4,
'description': 'To wander and not get lost, this torch will guide you through the darkest of '
'places'},

classes.py模块:

class HeroCharacter:
def __init__(self, name, health_points, attack_power, spell_power, agility, inventory,
level, experience, luck):
self.name = name
self.health_points = health_points
self.attack_power = attack_power
self.spell_power = spell_power
self.agility = agility
self.inventory = inventory
self.level = level
self.experience = experience
self.luck = luck
class Adventurer(HeroCharacter):
def __init__(self):
super().__init__(name=input("Enter your character name: "), health_points=225, attack_power=30,
spell_power=0, agility=30, inventory={}, level=1, experience=0, luck=10)
profession = "Adventurer"
class Wizard(HeroCharacter):
def __init__(self):
super().__init__(name=input("Enter your character name: "), health_points=175, attack_power=10,
spell_power=75, agility=0, inventory={}, level=1, experience=0, luck=4)
profession = "Wizard"
class Warrior(HeroCharacter):
def __init__(self):
super().__init__(name=input("Enter your character name: "), health_points=250, attack_power=60,
spell_power=5, agility=15, inventory={}, level=1, experience=0, luck=4)
profession = "Warrior"
class Assassin(HeroCharacter):
def __init__(self):
super().__init__(name=input("Enter your character name: "), health_points=200, attack_power=35,
spell_power=15, agility=40, inventory={}, level=1, experience=0, luck=4)
profession = "Assassin"```

以下是我如何构建它:

from typing import NamedTuple

class Weapon(NamedTuple):
name: str
attack_power: int
spell_power: int
agility: int
luck: int
description: str

class HeroCharacter:
def __init__(
self,
name: str,
health_points: int,
attack_power: int,
spell_power: int,
agility: int,
luck: int,
):
self._name = name
self._health_points = health_points
self._attack_power = attack_power
self._spell_power = spell_power
self._agility = agility
self._luck = luck
self._level = 1
self._experience = 0
self._weapon = Weapon(
"unarmed", 0, 0, 0, 0, "You are unarmed."
)
self._inventory = []  # What kinds of things go in here?
def equip(self, weapon: Weapon) -> None:
# Should the old weapon go into the inventory?
self._weapon = weapon
@property
def name(self) -> str:
return self._name
@property
def profession(self) -> str:
return self.__class__.__name__
@property
def attack_power(self) -> int:
return self._attack_power + self._weapon.attack_power
@property
def spell_power(self) -> int:
return self._spell_power + self._weapon.spell_power
@property
def agility(self) -> int:
return self._agility + self._weapon.agility
@property
def luck(self) -> int:
return self._luck + self._weapon.luck

class Adventurer(HeroCharacter):
def __init__(self, name: str):
super().__init__(
name=name,
health_points=225,
attack_power=30,
spell_power=0,
agility=30,
luck=10,
)

class Wizard(HeroCharacter):
def __init__(self, name: str):
super().__init__(
name=name,
health_points=175,
attack_power=10,
spell_power=75,
agility=0,
luck=4
)

class Warrior(HeroCharacter):
def __init__(self, name: str):
super().__init__(
name=name,
health_points=250,
attack_power=60,
spell_power=5,
agility=15,
luck=4
)

class Assassin(HeroCharacter):
def __init__(self, name: str):
super().__init__(
name=name,
health_points=200,
attack_power=35,
spell_power=15,
agility=40,
luck=4
)

我将武器通用的属性定义为NamedTuple,因为定义不同属性的名称(而不是让它们成为字典中的字符串文字(意味着你不太可能拼写错误——如果你使用IDE,这些名称甚至会自动完成,因为你提前定义了它们!:(

为了表示装备武器的概念,我在HeroCharacter类中添加了_weapon属性;为了使逻辑更简单;非武装的";国家是一种武器,所以我们总是可以检查Weapon物体,即使武器是徒手的。

然后可以计算角色的有效统计数据@propertys——每次你说类似player.attack_power的话,你都会得到武器统计数据中的答案。

我还简化了字符类层次结构的其他一些方面。初始化中所有字符共有的部分(如level和XP(都已移动到HeroCharacter基类中。";职业;类变量是不需要的,因为您可以从类的名称中获取它。我还将input()调用移出了构造函数,因为让构造函数做一些阻塞性的事情,比如提示输入,在某些时候可能会很烦人(比如,当你想把进度保存到文件中,然后加载它时,在这种情况下,你不想重新提示用户输入他们的字符名(。

以下是使用这些属性的示例,以及equipping武器如何更改其值:

>>> player = Assassin("Bob")
>>> player.name
'Bob'
>>> player.profession
'Assassin'
>>> player.equip(Weapon("starter_dagger", 4, 0, 10, 1, "Assassin starter weapon"))
>>> player.attack_power
39
>>> player.agility
50
>>> player.equip(Weapon("starter_longsword", 12, 0, -2, 2, "Unnecessarily heavy longsword"))
>>> player.attack_power
47
>>> player.agility
38

很酷的问题!这是一个非常开放的过程,在游戏中如何处理武器需要做出很多决定。

不过,你的问题确实太开放了,因为堆栈溢出更适合回答你面临的特定问题。我建议你试着自己想出一个解决方案,如果遇到问题,再提出一个新问题。

话虽如此,我建议从简单开始,在角色类中创建一个存储武器的值,并创建一个";攻击;作用下面只是伪代码,给你一个可能的想法

class HeroCharacter:
def __init__(self, name, health_points, attack_power, spell_power, agility, inventory,
level, experience, luck):
self.name = name
self.health_points = health_points
self.attack_power = attack_power
self.spell_power = spell_power
self.agility = agility
self.inventory = inventory
self.level = level
self.experience = experience
self.luck = luck
self.weapon = None

def get_attack_value(self):
val = self.attack_power
if self.weapon != None:
val += self.weapon['attack_power']
return val

在类HeroCharacter中添加两个函数。但在每件武器中,你都需要指定所有属性(攻击力、拼写力、敏捷性、运气。如果武器不使用,只需将其设置为0(。我还想添加武器的名称作为统计数据,以便在add_quipment中更容易访问

class HeroCharacter:
def __init__(self, name, health_points, attack_power, spell_power, agility, inventory,
level, experience, luck):
self.name = name
self.health_points = health_points
self.attack_power = attack_power
self.spell_power = spell_power
self.agility = agility
self.inventory = inventory
self.level = level
self.experience = experience
self.luck = luck
def add_equipment(self, eq):
self.attack_power += eq["attack_power"]
self.spell_power += eq["spell_power"]
self.agility += eq["agility"]
self.luck += eq["luck"]

def del_equipment(self, eq):
self.attack_power -= eq["attack_power"]
self.spell_power -= eq["spell_power"]
self.agility -= eq["agility"]
self.luck -= eq["luck"]

要使用这些功能,你需要这样做:

hero = HeroCharacter("Bob", 10, 10, 10, 10, "", 10, 10, 10) #Create your hero 
hero.add_equipment(weapons['starter_dagger']) #Select and add equ to hero
hero.del_equipment(weapons['starter_dagger']) # And use this to delete item from hero

最新更新