【发布时间】: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