while jogador1_hp > 30:
enemy1_atk = random.randrange(1, 250)
enemy1 = Enemy1(enemy1_atk)
enemy1_atkk = enemy1.getAttk()
jogador1_hp = jogador1_hp - enemy1_atkk
if jogador1_hp <= 30:
jogador1_hp = 30
print("You got hit and you lost ", enemy1_atkk, " hp. Your HP its now", jogador1_hp)
if jogador1_hp > 30:
continue
print("Gods protected you by teleporting you to the nearest INN")
break
、私は「敵」を作成したいと私は彼の攻撃は、それが攻撃するたびにランダムになりたいです。私は、whileループ内の敵オブジェクトを作成した場合、それをループするたびに新しいオブジェクトを作成するか、それが再び同じオブジェクトを作成しますか?どのように私は新しいオブジェクトを作成することなく、オブジェクトの引数を変更できますか?この演習で
それが再びループするたびに、彼の「攻撃」を変更することができ、まだループの外でオブジェクトを作成してする方法はありますか?
EDITは:これは私の敵クラス
class Enemy1:
def __init__(self, atkp):
self.atkp = atkp
def getAttk(self):
return self.atkp
EDIT2である:あなたの回答のおかげで、私はそれを修正するために管理します。それは次のようになります:あなたは、ループ内の敵のクラスのインスタンスを作成する場合
class Enemy1:
def __init__(self, atkp):
self.atkp = atkp
jogador1 = Player1(300)
jogador1_hp = jogador1.getBHP()
enemy1_atk = random.randrange(1, 250)
nemy1 = Enemy1(enemy1_atk)
while jogador1_hp > 30:
enemy1_atk = random.randrange(1, 250)
jogador1_hp = jogador1_hp - enemy1_atk
私はあなたの元のコードであなたの印刷ステートメントの提案を持っています。文字列の書式設定を使うと、文字列を簡単に組み立てることができます: 'print("ヒットして{} hpを失った。 – RagingRoosevelt