python中的类和字典有问题

2024-09-30 02:25:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将我的旧游戏引擎转换成一个使用类而不仅仅是字典的新版本

我的目标是从两个不同的类中添加相似的键值,以便修改玩家的统计信息

class player:
    def __init__(self, name, level, xp, nextlvlxp, stats, inventory):
        self.name = ''
        self.level = 1
        self.xp = 0
        self.nextlvlxp = 25
        self.stats = {
            'str': [],
            'dex': [],
            'con': [],
            'int': [],
            'wis': [],
            'cha': [],
            'modstr': [],
            'moddex': [],
            'modcon': [],
            'modint': [],
            'modwis': [],
            'modcha': []
        }
        self.combat_stats = {
            'totalhp': [],
            'currenthp':[],
            'totalmp': [],
            'currentmp': [],
            'baseatk': [],
            'attack': [],
            'speed': [],
            'perception':[]

        }
        self.noncombat_stats = {
            'evasion': [],
        }
        self.inventory = {}


class Item():
    def __init__(self, name, description, stats, value):
        self.name = name
        self.description = description
        self.stats = {}
        self.value = value
class Weapon(Item):
    def __init__(self, name, description, stats, value):
        self.stats = stats
        super().__init__(name, description, value)

    def __str__(self):
        return "{}\n=====\n{}\nValue: {}\nStats: {}".format(self.name, self.description, self.value, self.stats)

class Sword(Weapon):
    def __init__(self):
        super().__init__(name="Sword",
                         description="A common shortsword",
                         stats = {
                             'str': 2,
                             'dex': 1
                         },
                         value = 10)

电子xample:items ‘剑’’str’+玩家‘str’=玩家‘modstr’

我想把这些加在一起,还有下一个dex的值,我想用一种非竞争性的方式。我真的不知道从哪里开始,我只是刚刚学会如何写今天的课。有没有什么方法可以通过字典将具有相同键的值添加到一起


Tags: nameself字典initvaluedefstats玩家
1条回答
网友
1楼 · 发布于 2024-09-30 02:25:44

这基本上就是您要做的,但是我不确定您的情况,因为您的默认属性是空列表

a = {1: 2, 3: 4, 5: 6}
b = {1: 8, 3: 2, 7: 8}

for key in a:
    if key in b:
        a[key] += b[key]

例如:

class Pool:
    def __init__(self, value):
        self.value = value
        self.max_value = value

class Stats:
    def __init__(self, strength=0, dexterity=0, constitution=0, intelligence=0, wisdom=0, charisma=0):
        self.strength = strength
        self.dexterity = dexterity
        self.constitution = constitution
        self.intelligence = intelligence
        self.wisdom = wisdom
        self.charisma = charisma

    def __add__(self, stats):
        return Stats(
            self.strength + stats.strength,
            self.dexterity + stats.dexterity,
            self.constitution + stats.constitution,
            self.intelligence + stats.intelligence,
            self.wisdom + stats.wisdom,
            self.charisma + stats.charisma
        )

    def copy(self):
        return Stats(
            self.strength,
            self.dexterity,
            self.constitution,
            self.intelligence,
            self.wisdom,
            self.charisma
        )

    def __repr__(self):
        return str(vars(self))

class Equipment:
    def __init__(self):
        self.weapon = Weapon('None', 'None', 0, Stats())
        self.chest = Weapon('None', 'None', 0, Stats())
        self.hands = Weapon('None', 'None', 0, Stats())
        self.head = Weapon('None', 'None', 0, Stats())
        self.legs = Weapon('None', 'None', 0, Stats())

    def __repr__(self):
        return str(vars(self))

class Player:
    def __init__(self, name, next_xp, stats, inventory):
        self.xp = 0
        self.name = name
        self.stats = stats
        self.next_xp = next_xp
        self.inventory = inventory
        self.equipment = Equipment()

    def get_stats(self):
        stats = self.stats.copy()
        elist = vars(self.equipment)
        for v in elist:
            stats += elist[v].stats

        return stats

class Item:
    def __init__(self, name, description, value):
        self.name = name
        self.description = description
        self.value = value

class Weapon(Item):
    def __init__(self, name, description, value, stats):
        Item.__init__(self, name, description, value)
        self.stats = stats

    def __repr__(self):
        return str(vars(self))

def main():
    weapons = {}
    weapons['sword'] = Weapon('sword', 'common sword', 10, Stats(2, 1))
    weapons['intelligence sword'] = Weapon('intelligence sword', 'sword of intelligence', 10, Stats(2, 1, intelligence=1))

    player = Player('Me', 25, Stats(5, 4, 6, 3, 3, 4), {})
    print(player.get_stats())
    player.equipment.weapon = weapons['sword']
    print(player.get_stats())
    player.equipment.weapon = weapons['intelligence sword']
    print(player.get_stats())

main()

相关问题 更多 >

    热门问题