试图让游戏升级并使用新的敌人

  • 本文关键字:敌人 游戏 python
  • 更新时间 :
  • 英文 :


每次英雄升级时,我都会尝试使用一个新的敌人。代码本身工作起来没有问题。玩家升级,他使用魔法和法力,敌人反击,以及所有的爵士乐。我只是想不出每次玩家杀死敌人时都能加入新敌人的方法。有什么想法吗?

class Character:
def __init__(self, name, health, level):
self.name = name
self.health = health
self.level = level
self.levelup_counter = level + 1
def attack(self):
c2.health -= 2
c1 = Character("Hero", 10, 1)
c2 = Character("Monster 1", 10, 2)
c3 = Character("Monster 2", 20, 3)
c4 = Character("Monster 3", 40, 4)
# monster_list = [c2,c3,c4]
def stats():
print("_______________________________________")
print("Your stats ttEnemy Stats")
print("---------------------------------------")
print(f"Health: {c1.health}ttHealth: {c2.health}")
print(f"Level: {c1.level}ttLevel: {c2.level}")
while True:
attack = input("Enter 1 to attack: ")
c1.attack()
stats()
if c2.health == 0:
c1.level += 1
print("YOu killed the monster!nYou leveled up!")
continue

更新:

更新:

我做了一些更改,并添加了一个for循环来迭代怪物列表。现在我有两个问题。首先,怪物不断迭代(显然(,而不是一个怪物战斗到死。然后,一个新的怪物将会出现。第二,我不能在怪物死后将其从名单中删除。我得到错误";TypeError:"Character"对象不能被解释为整数">

class Character:
def __init__(self, name, health, level):
self.name = name
self.health = health
self.level = level
self.levelup_counter = level + 1
def attack(self):
m.health -= 2
def monster_death(self):
if m.health == 0:
monster_list.pop(m)

c1 = Character("Hero", 10, 1)
c2 = Character("Monster 1", 10, 2)
c3 = Character("Monster 2", 20, 3)
c4 = Character("Monster 3", 40, 4)
monster_list = [c2,c3,c4]
def stats():
print("_______________________________________")
print("Your stats ttEnemy Stats")
print("---------------------------------------")
print(f"Health: {c1.health}ttHealth: {m.health}")
print(f"Level: {c1.level}ttLevel: {m.level}")
i=0
while True:
i+=1
for m in monster_list:
attack = input("Enter 1 to attack: ")
c1.attack()
stats()
if m.health == 0:
c1.level += 1
print("YOu killed the monster!nYou leveled up!")
m.monster_death()
continue

要解决第一个问题,只需为每个怪物添加一个while循环,直到生命值小于或等于0。我已经在下面的代码中显示了这一点。还有你的";i〃;据我所知,柜台完全并没有用过。

while True:
for m in monster_list:
while m.health > 0:
attack = input("Enter 1 to attack: ")
c1.attack()
stats()
if m.health <= 0:
c1.level += 1
print("YOu killed the monster!nYou leveled up!")
m.monster_death()

至于从列表中删除怪物的第二个问题,我认为在遍历for循环时这是个坏主意。通过从列表中删除怪物,同时循环浏览它们,你会得到索引错误,它会跳过怪物编号3。不过,如果您想消除正在出现的错误,只需将这一行:monster_list.pop(m)更改为monster_list.remove(m)即可。

@furas上面说的是一个很好的建议。你可以这样做(伪代码,但你明白了(:

while len(monster_list) > 0:
while monster_list[0].health > 0:
# attack
if monster_list[0].health <= 0:
# kill monster and remove monster_list[0]

您可以将怪物保留在列表中,并始终与列表中的第一个怪物monster_list[0]战斗。当你杀死它时,remove()它——然后你可以在列表的末尾添加新的怪物。


最小工作代码

# --- classes ---
class Character:

def __init__(self, name, health, level):
self.name = name
self.health = health
self.level = level
self.levelup_counter = level + 1

def attack(self):
self.health -= 2

def monster_death(self):
if self.health <= 0:  # better check `<=` instead of `==`
monsters.remove(self)
def __str__(self):
return f'name: {self.name}, health: {self.health}, level: {self.level}'

# --- functions ---
def stats(player, monster):
print("_______________________________________")
print("Your stats ttEnemy Stats")
print("---------------------------------------")
print(f"Health: {player.health}ttHealth: {monster.health}")
print(f"Level: {player.level}ttLevel: {monster.level}")
# --- main ---
player = Character("Hero", 10, 1)
monsters = [
Character("Monster 1", 10, 2),
Character("Monster 2", 20, 3),
Character("Monster 3", 40, 4),
]    
#monsters = []
#for i in range(1, 4):
#    monsters.append( Character(f"Monster {i}", i*10, i+1),
i = 3  # used for monster's name
while monsters:
m = monsters[0]
print('New monster attacked:', m)  # it will use `__str__` to display `m`
while True: 
attack = input("Enter 1 to attack: ")
m.attack()
stats(player, m)
if m.health <= 0:  # better check `<=` instead of `==`
player.level += 1
print("You killed the monster!nYou leveled up!")
m.monster_death()
i += 1
monsters.append( Character(f"Monster {i}", i*10, i+1) )
break  # exit nearest `while`

最新更新