类文件夹:
在游戏.py:http://pastebin.com/P7Degwna
(我没有足够的声誉发布两个以上的链接。以下断开的链接是为了完整性,但我认为它们的代码完全正确)
在魔法.py:http://pastebin.com/wpwSCDe7在
在库存.py:http://pastebin.com/8kFXJne1在
我正在努力成为一个更好的程序员,并一直遵循一个简单的战斗系统教程。目前正在实施敌方作战战术。在
我正在尝试这样的程序,如果一个敌人选择治疗,并且有超过50%的剩余,他们会重新选择一个咒语。
每当发生这种情况时,我从代码中得到以下错误Traceback (most recent call last):
File "C:/Users/Kyle/PycharmProjects/battle/main.py", line 160, in
choose_attack, spell, magic_dmg = enemy.choose_enemy_spell()
TypeError: 'NoneType' object is not iterable
以下是在此之前输出到终端的转储:
^{pr2}$
我可以看到我的玩家轮到他们了,然后敌人选择了治疗咒语,因为有太多的生命,所以选择了另一个咒语,然后我得到了非类型错误。在
当另一个不是库拉索加的咒语第一次被选中时,这个代码工作得很好。在
相关代码部分:
这是我的主程序#Create Black Magic
fire = Spell("Fire", 25, 600, "black") #create object fire from class Spell
thunder = Spell("Thunder", 25, 600, "black")
blizzard = Spell("Blizzard", 25, 600, "black")
meteor = Spell("Meteor", 40, 1200, "black")
quake = Spell("Quake", 32, 900, "black")
#Create White Magic
cure = Spell("Cure", 25, 620, "white")
cura = Spell("cura", 32, 1500, "white")
curaga = Spell("Curaga", 50, 6000, "white")
#Create lists of magic and items associated with players
player_magic = [fire, thunder, blizzard, meteor, cure, cura] #Create list of magic spell objects for object player
player_items = [{"item": potion, "quantity": 15}, {"item": hipotion, "quantity": 5}, {"item": superpotion, "quantity": 5},
{"item": elixer, "quantity": 5}, {"item": hielixer, "quantity": 2}, {"item": grenade, "quantity": 5},] #Create list of item objects for object player
#Creat lists of magic and items associated with enemies
enemy_spells = [fire, meteor, curaga]
eneny_items = []
#Istantiate Player objects
player1 = Person("Zelda", 3260, 132, 300, 34, player_magic, player_items)
#Inistantiating object player using class Person
player2 = Person("Link ", 4160, 188, 311, 34, player_magic, player_items)
player3 = Person("Sheik", 3089, 174, 288, 34, player_magic, player_items)
#Instantiate Enemy objects
enemy1 = Person("Vaati", 1250, 130, 560, 325, enemy_spells, [])
enemy2 = Person("Ganon", 18200, 701, 535, 25, enemy_spells, [])
#Instantiating object enemy using class Person
enemy3 = Person("Vaati", 1250, 130, 560, 325, enemy_spells, [])
# Enemy chose to use magic
if enemy_choice == 1:
choose_attack, spell, magic_dmg = enemy.choose_enemy_spell()
enemy.reduce_mp(spell.cost)
if spell.type == "white": # Check type of spell of object spell to determine how to handle the chosen spell.
enemy.heal(magic_dmg) # White typer means heal
print(bcolours.OKBLUE + "\n" + spell.name, " heals", enemy.name + str(magic_dmg),
"HP." + bcolours.ENDC)
elif spell.type == "black": # black type means do damage
target = random.randrange(0, 3)
players[target].take_damage(magic_dmg)
print(bcolours.OKBLUE + "\n" + enemy.name.replace(" ", "") + "'s " + spell.name, "deals",
str(magic_dmg), "points of damage to " + players[target].name + bcolours.ENDC)
if players[target].get_hp() == 0:
print(players[target].name + " has died.")
del players[target]
这来自一个包含类的文件
#Determine the spell the enemy chooses to use
def choose_enemy_spell(self):
print("Begin method")
choose_attack = 0
magic_choice = random.randrange(0, len(self.magic))
spell = self.magic[magic_choice]
magic_dmg = spell.generate_damage()
print(spell.name, "INSIDE CLASS")
pct = (self.hp / self.maxhp)*100 #percentage of health remaining
if self.mp < 25:
print("This probably isnt happening")
choose_attack = 1
return choose_attack, spell, magic_dmg
if (spell.type == "white" and pct > 50) or self.mp < spell.cost:
print("This is happening.")
self.choose_enemy_spell()
else:
print(magic_dmg, "INSIDE CLASS INSIDE METHOD INSIDE FINAL ELSE")
return choose_attack, spell, magic_dmg