常にhealth
を確認する必要はありません。健康を低下させる機能(例:attack(character, damage)
)を呼び出す場合は、いつでもhealth > 0
かどうかを確認することができます。そうでない場合は、game_over()
に電話する必要があります。ここで
は、関連する質問からいくつかのコードです:
class Character:
def __init__(self, name, hp_max):
self.name = name
self.xp = 0
self.hp_max = hp_max
self.hp = hp_max
# TODO: define hp_bar here
def is_dead(self):
return self.hp <= 0
def attack(self, opponent, damage):
opponent.hp -= damage
self.xp += damage
def __str__(self):
return '%s (%d/%d)' % (self.name, self.hp, self.hp_max)
hero = Character('Mario', 1000)
enemy = Character('Goomba', 100)
print(enemy)
# Goomba (100/100)
hero.attack(enemy, 50)
print(enemy)
# Goomba (50/100)
hero.attack(enemy, 50)
print(enemy)
# Goomba (0/100)
print(enemy.is_dead())
# True
print(hero.xp)
# 100
方法 - 健康は健康=健康から離れて離れて取られたとき
その後、私は何も入力する必要はありません:健康 - =損失;ヘルス<0:raise PanicHealthNegative() ' – tripleee