【问题标题】:Python+OOP - Adding attributes and creating Game ObjectPython+OOP - 添加属性和创建游戏对象
【发布时间】:2021-08-15 14:02:06
【问题描述】:

您好,我想知道我是否正确地将剑和斧头添加到我的对象中? 我想让我的角色在攻击时向前移动+2,在防守时向后移动-2。我的 def move 代码是否正确?

Image1 Image2

【问题讨论】:

    标签: python oop console-application


    【解决方案1】:

    您可以使用继承并编写类似的内容:

    class Item:
    
        def __init__(self, name: str) -> None:
            self.name = name
    
    
    class Character:
        def __init__(self, weapon: Item) -> None:
            self.weapon = weapon
    
    
    class Warrior(Character):
        def __init__(self, weapon: Item) -> None:
            # some attributes
            super().__init__(weapon=weapon)
    
    
    class Tanker(Character):
        def __init__(self, weapon: Item) -> None:
            # some attributes
            super().__init__(weapon=weapon)
    
    
    sword = Item(name="Sword")
    axe = Item(name="Axe")
    warrior = Warrior(weapon=sword)
    tanker = Tanker(weapon=axe)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-18
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 2022-07-27
      • 1970-01-01
      • 2018-05-16
      相关资源
      最近更新 更多