【问题标题】:Is there a way to shorten the damage calculation for my Pokemon game? I am constantly repeating lines of code有没有办法缩短我的口袋妖怪游戏的伤害计算?我不断重复代码行
【发布时间】:2022-01-06 21:39:15
【问题描述】:

如您所见,当我计算造成的伤害时,我会重复输入口袋妖怪。只是想知道是否有更简单的方法来编写此代码。我想知道您是否可以使用某种表格,但不确定如何实现。 为了以防万一,我提供了大部分课程。

class Pokemon:
    def __init__(self, name, types, max_health, health, attack, defense):
        self.name = name
        self.types = types
        self.max_health = max_health
        self.health = health
        self.attack = attack
        self.defense = defense

    #Calculates the damage output depending on their attack and defense attributes
    def move(self, opponent):
        raw_dmg = 0
        choice = int(input("Choose your move: "))
        if choice == 1:
            raw_dmg = int((self.attack / opponent.defense) * 25)
        elif choice == 2:
            if accuracy(70):
                raw_dmg = int((self.attack / opponent.defense) * 40)
            else:
                raw_dmg = 0
        elif choice == 3:
            if accuracy(20):
                raw_dmg = int((self.attack / opponent.defense) * 200)
            else:
                raw_dmg = 0

        #Increases or decereases the damage depending on the type of both pokemon
        if self.types == "Fire":
            if opponent.types == "Water":
                raw_dmg = raw_dmg * 0.5
            elif opponent.types == "Grass":
                raw_dmg = raw_dmg * 2
        elif self.types == "Water":
            if opponent.types == "Grass":
                raw_dmg = raw_dmg * 0.5
            elif opponent.types == "Fire":
                raw_dmg = raw_dmg * 2
        elif self.types == "Grass":
            if opponent.types == "Fire":
                raw_dmg = raw_dmg * 0.5
            elif opponent.types == "Water":
                raw_dmg = raw_dmg * 2
        elif self.types == "Light":
            if opponent.types == "Dark":
                raw_dmg = raw_dmg * 2
        elif self.types == "Dark":
            if opponent.types == "Light":
                raw_dmg = raw_dmg * 2

        #The final damage is randomised by multiplying it by a value between 0.8 and 1.2
        final_dmg = round(raw_dmg * (random.randint(80, 120)) / 100)
        print(final_dmg)
        opponent.health -= final_dmg
        if opponent.health <= 0:
            opponent.health = 0
        return final_dmg

【问题讨论】:

  • 看起来像是代码审查的问题,而不是这样
  • 这能回答你的问题吗? Shorten repetitive Python code
  • @Matt 不是。 OP 只寻求一部分的帮助,而不是对整个事情的审查。他们错过了这件事的背景。 More info

标签: python oop


【解决方案1】:

只要有可能,最好将代码转换为数据:

之前:

        if self.types == "Fire":
            if opponent.types == "Water":
                raw_dmg = raw_dmg * 0.5
            elif opponent.types == "Grass":
                raw_dmg = raw_dmg * 2
        elif self.types == "Water":
            if opponent.types == "Grass":
                raw_dmg = raw_dmg * 0.5
            elif opponent.types == "Fire":
                raw_dmg = raw_dmg * 2
        elif self.types == "Grass":
            if opponent.types == "Fire":
                raw_dmg = raw_dmg * 0.5
            elif opponent.types == "Water":
                raw_dmg = raw_dmg * 2
        elif self.types == "Light":
            if opponent.types == "Dark":
                raw_dmg = raw_dmg * 2
        elif self.types == "Dark":
            if opponent.types == "Light":
                raw_dmg = raw_dmg * 2

之后:

# ahead of time in the global namespace
SUPER_EFFECTIVE = {
    ("Fire", "Grass"),
    ("Water", "Fire"),
    ...
}
NOT_VERY_EFFECTIVE = {
    ("Fire", "Water"),
    ("Water", "Grass"),
    ...
}

...

        # in the move function
        if (self.types, opponent.types) in SUPER_EFFECTIVE:
            raw_dmg *= 2.0
        elif (self.types, opponent.types) in NOT_VERY_EFFECTIVE:
            raw_dmg *= 0.5

【讨论】:

    【解决方案2】:

    Dennis's answer 开始,我会更进一步,制作一个字典,您可以在其中根据类型组合查找乘数。然后使用 .get(),您可以使用默认值 1。这使得类中的代码 flat instead of nested

    TYPE_MULTIPLIERS = {
        0.5: [
            ("Fire", "Water"),
            ("Water", "Grass"),
            ("Grass", "Fire"),
            ],
        2: [
            ("Fire", "Grass"),
            ("Water", "Fire"),
            ("Grass", "Water"),
            ("Light", "Dark"),
            ("Dark", "Light"),
            ],
        }
    
    _MULTIPLIER_LOOKUP = {
        types: multiplier
        for multiplier, type_combos in TYPE_MULTIPLIERS.items()
        for types in type_combos}
    
    # ... later
    
        raw_dmg *= _MULTIPLIER_LOOKUP.get((self.types, opponent.types), 1)
    

    【讨论】:

      猜你喜欢
      • 2019-08-30
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多