我正在创建一个基于终端的游戏,在使用我last question的答案后,我意识到我遇到了另一个问题。我需要弄清楚如何根据set类正确更新角色的统计数据,并且他们的解决方案有效。但现在我需要弄清楚如果它不是数字就更新它。到目前为止,我为字符串生成了一个,但现在我需要一个用于数据列表。
例如,到目前为止,player
类包含一个具有默认统计数据和值的基本字符。
class BaseCharacter:
#define what to do when the object is created, or when you call player = BaseCharacter()
def __init__(self):
#generate all the stats. these are the default stats, not necessarily used by the final class when player starts to play.
#round(random.randint(25,215) * 2.5) creates a random number between 25 and 215, multiplies it by 2.5, then roudns it to the nearest whole number
self.gold = round(random.randint(25, 215) * 2.5)
self.currentHealth = 100
self.maxHealth = 100
self.stamina = 10
self.resil = 2
self.armor = 20
self.strength = 15
self.agility = 10
self.criticalChance = 25
self.spellPower = 15
self.speed = 5
self.first_name = 'New'
self.last_name = 'Player'
self.desc = "Base Description"
self.class_ = None
self.equipment = [None] * 6
我在上一个问题中使用的更新功能如下:
#define the function to update stats when the class is set
def updateStats(self, attrs, factors):
#try to do a function
try:
#iterate, or go through data
for attr, fac in zip(attrs, factors):
val = getattr(self, attr)
setattr(self, attr, val * fac)
#except an error with a value given or not existing values
except:
raise("Error updating stats.")
因此,当我根据WarriorCharacter
...
BaseCharacter
时
class WarriorCharacter(BaseCharacter):
#define data when initialized, or the Object is created
def __init__(self, first_name, last_name):
super().__init__()
#update the class value since its a copy of BaseCharacter
self.class_ = 'Warrior'
#update the first name
self.first_name = first_name
#update the last name
self.last_name = last_name
#update description value
self.desc = 'You were born a protector. You grew up to bear a one-handed weapon and shield, born to prevent harm to others. A warrior is great with health, armor, and defense.'
self.updateStats(['stamina', 'resil', 'armor', 'strength', 'speed'], [1.25, 1.25, 1.35, 0.75, 0.40])
...我可以在Warrior课程中运行self.updateStats(['stamina', 'resil', 'armor', 'strength', 'speed'], [1.25, 1.25, 1.35, 0.75, 0.40])
来正确更新已创建播放器的新统计数据。
然而我不得不稍微修改updateStats
类的shops
函数,因为它通过更新for循环主要使用字符串而不是数字。
for attr, fac in zip(attrs, factors):
setattr(self, attr, fac)
有了商店......
class BaseShop:
def __init__(self):
#generate shop information
self.name = "Base Shop"
self.gold = round(random.randint(325, 615) * 2)
self.desc = "Base Description"
self.stock = [None] * random.randint(3, 8)
self.vendor = "Base Vendor"
#set responses
#. . .
...这样我就可以致电shop.updateShopInfo(["vendor"], ["Test Vendor"])
将商店供应商名称从'Base Vendor'
更改为'Test Vendor'
现在我不知道如何为数组/列表数据类型修改此函数。正如您在示例中看到的,我有一些变量,例如self.equipment = [None] * 6
和self.stock = [None] * random.randint(3, 8)
。我希望能够update
这些统计数据与item
类中的项目一样,这些数据将如此构建:
class BaseItem:
def __init__(self):
self.name = "Base Item Name"
self.desc = "Base Item Description"
self.itemType = None
self.itemSize = None
self.stats = {
'Strength': 0,
'Agility': 0,
'Critical Chance': 0,
'Spell Power': 0,
'Speed': 0,
'Health': 0,
'Stamina': 0,
'Armor': 0,
'Resilience': 0
}
self.slot = None
#my attempt at it so far
def updateItemStats(self, attrs, factors):
try:
#iterate, or go through data
for attr, fac in zip(attrs, factors):
val = getattr(self, attr)
setattr(self, attr, val + fac)
#except an error with a value given or not existing values
except:
raise("Error updating stats.")
class OneHandedSword(BaseItem):
def __init__(self):
super().__init__()
self.itemType = "Sword"
self.itemSize = "One Hand"
self.name = "Jade Serpentblade"
self.desc = "Sharp cutlass."
self.slot = "Weapon"
#something like this? would update the stats value of the new item to have +5 strength and +2 stamina
self.update([self.stats["strength"], self.stats["stamina"]], [5, 2])
更新功能会抛出此错误:
Traceback (most recent call last):
File "/home/ubuntu/workspace/Python/game_Test/test.py", line 33, in <module>
item2 = OneHandedSword()
File "/home/ubuntu/workspace/Python/game_Test/assets/items.py", line 46, in __init__
self.updateItemStats([self.stats["Strength"], self.stats["Stamina"]], [5, 2])
File "/home/ubuntu/workspace/Python/game_Test/assets/items.py", line 33, in updateItemStats
raise("Error updating stats.")
TypeError: exceptions must derive from BaseException
当我取出try/except
时,会返回此错误:
Traceback (most recent call last):
File "/home/ubuntu/workspace/Python/game_Test/test.py", line 33, in <module>
item2 = OneHandedSword()
File "/home/ubuntu/workspace/Python/game_Test/assets/items.py", line 43, in __init__
self.updateItemStats([self.stats["Strength"], self.stats["Stamina"]], [5, 2])
File "/home/ubuntu/workspace/Python/game_Test/assets/items.py", line 28, in updateItemStats
val = getattr(self, attr)
TypeError: getattr(): attribute name must be string
然后我尝试将val = getattr(self, attr)
修改为val = gettattr(self, self.stats[attr]
然后返回KeyError: 0
所以我基本上需要两个更新函数,一个用于更新item.stats
字典,另一个用于更新stock
和equipment
列表{{1对象。我不能为我的生活弄清楚如何做到这一点。有什么想法吗?