如您所見,當我計算造成的傷害時,我重復輸入口袋妖怪。只是想知道是否有更簡單的方法來撰寫此代碼。我想知道您是否可以使用某種表格,但不確定如何實作。為了以防萬一,我提供了大部分課程。
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
uj5u.com熱心網友回復:
只要有可能,最好將代碼轉換為資料:
前:
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
uj5u.com熱心網友回復:
根據丹尼斯的回答,我會更進一步,制作一本字典,您可以在其中根據型別組合查找乘數。然后使用.get(),您可以使用默認值 1。這使得類中的代碼扁平而不是嵌套。
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/404783.html
標籤:
