我在写一个类似口袋妖怪的转身基地战斗模拟器。我已经很好了,但我有个问题。我试着把动作写成字典,这样我就可以为它的对象中的每个怪物指定动作集。这样我就有了一本字典,里面有他们所有的动作和伤害。然后我就把这些动作传给怪物。这使我可以给每个怪物唯一的移动集,而不必指定每次移动造成的伤害。我遇到的问题是在while循环中,当用户选择攻击时,我得到了错误
AttributeError: 'set' object has no attribute 'get'
我试着让用户输入攻击,这样它就可以从怪物1hp中获得伤害并减去它
如果我把字典放在对象中,它可以正常工作,但是每个对象都必须有自己的字典,我只想为所有移动设置一个字典,然后为移动集中的每个对象指定移动,如果这有意义的话。你知道吗
import random
class Monster:
def __init__(self, name, types, hp, region, disc, strength, weakness, moveset):
self.name = name
self.type = types
self.hp = hp
self.region = region
self.disc = disc
self.strength = strength
self.weakness = weakness
self.moveset = moveset
moves_dict = {
"hyperbeam": 14,
"flames": 34,
"water": 27,
"cent": 33,
}
Ariol = Monster("Ariol", "shadow", 1000, "Xevor", "A monster from hell", "psychic", "electricity", {"hyperbeam", "flames", "water", "cent"})
Xenos = Monster("Xenos", "shadow", 300, "Xevor", "A monster from hell", "psychic", "electricity", {"hyperbeam", "flames", "water", "cent"})
Siance = Monster("Siance", "shadow", 500, "Xevor", "A monster from hell", "psychic", "electricity", {"hyperbeam", "flames", "water", "cent"})
Mammy = Monster("Mammy", "shadow", 3000, "Xevor", "A monster from hell", "psychic", "electricity", {"hyperbeam", "flames", "water", "cent"})
def choose_monster():
monster_menu = {
"Ariol" : Ariol,
"Xenos" : Xenos,
"Siance" : Siance,
"Mammy" : Mammy,
}
#choosemonster
trainer_monster = input("What monster do you want to use?\n")
#variable is holding monster chosen
monster_chosen1 = monster_menu.get(trainer_monster)
#get monster 1 hp
computer_choose = random.choice(list(monster_menu))
computer_chosen1 = monster_menu[computer_choose]
print("You chose", monster_chosen1.name,"!")
print("Your opponenet chose", computer_chosen1.name, "!")
#this dict not used
moveset_dict = {
"1": 14,
"2": 34,
"3": 27,
"4": 33,
}
#how to get monster hp from above
#monster1hp = Ariol.hp
monster1hp = monster_chosen1.hp
monster2hp = computer_chosen1.hp
while monster1hp or monster2hp <= 0:
#monster1 moveset
for x in monster_chosen1.moveset:
print(x)
choose_attack = input("What move would you like to use?\n")
attack_chosen = monster_chosen1.moveset.get(choose_attack)
monster2hp = monster2hp - attack_chosen
print(monster_chosen1.name, "used ", attack_chosen, computer_chosen1.name, "has", monster2hp, "hp")
if monster2hp <= 0:
print(computer_chosen1.name, "was knocked out", monster_chosen1.name, "wins!")
break
attack_chosen = random.choice(list(computer_chosen1.moveset))
monster1hp -= computer_chosen1.moveset[attack_chosen]
print(computer_chosen1.name, "used", attack_chosen, monster_chosen1.name, "has", monster1hp, "hp")
if monster1hp <= 0:
print(monster_chosen1.name, "was knocked out", computer_chosen1.name, "wins!")
break
else:
print("Battle over")
choose_monster()
你的问题是这条线(我只举一个怪物为例)
在这里你把他的攻击定义为一系列的选择。集合没有get方法。你知道吗
所以你必须从移动中获取dmg值:
当然,如果你想让每个怪物有不同的dmg值,你可以用dict来替换这个集合,这样你就可以用dmg值来玩游戏,这会使dict的动作过时。你知道吗
类似的变化与计算机选择的攻击有关。但因为他总是选择有效的动作,所以他当然不需要一个while循环。但是dmg值必须从dict中取出来
问题最后一段的一个解决方案是:
您可以在类中设置字典而不使用self,当您为其中一个对象更新字典时,它也会为所有其他对象更新字典。这称为类变量。你知道吗
下面是我编写的一个示例,向您展示我的意思:
输出:
从这个输出可以看出,当您使用obj1更改字典中的某些内容时,它也会更改obj2的内容,反之亦然。希望这有帮助!你知道吗
如果您还有任何问题,请告诉我,我很乐意帮助您
相关问题 更多 >
编程相关推荐