Basically I want to return a value from the healthcheck function so I can stop the pokemons attacking eachother. You will see I have left out the rest of the "if" line, as I am unsure what to put here. I need a way to return a function from the healthcheck function, but I can't get the return statement to work.
class enemy():
global enemylives
global dead
dead=0
enemylives=10
def healthcheck():
if enemylives<=0:
print("You defeated the enemy!")
while True:
time.sleep(1)
enemy1.attacked()
enemy1.healthcheck()
if
break;
time.sleep(1)
squirtle.attacked()
squirtle.healthcheck()
(This isn't my whole code)
Not sure what the logic of your game is but you should create attributes and avoid the use of global, use the instances attributes and methods. You can exit the loop with sys.exit if your enemys life gets to 0.
import time
import sys
from random import random
class Enemy():
def __init__(self):
self.dead = 0
self.life = 100
def attack(self):
if random() > .20:
self.life -= 10
print("Attack successful, enemy life decreased by 10 to {}".format(self.life))
elif self.life <= 90:
self.life += 10
print("Attack unsuccessful, enemy increases life to {}".format(self.life))
def health_check(self):
if self.life <= 0:
print("You defeated the enemy!")
sys.exit()
enemy1 = Enemy()
while True:
time.sleep(1)
enemy1.attack()
enemy1.health_check()
time.sleep(1)
Example output:
Attack successful, enemy life decreased by 10 to 90
Attack unsuccessful, enemy increases life to 100
Attack successful, enemy life decreased by 10 to 90
Attack successful, enemy life decreased by 10 to 80
Attack successful, enemy life decreased by 10 to 70
Attack successful, enemy life decreased by 10 to 60
Attack successful, enemy life decreased by 10 to 50
Attack unsuccessful, enemy increases life to 60
Attack unsuccessful, enemy increases life to 70
Attack successful, enemy life decreased by 10 to 60
Attack successful, enemy life decreased by 10 to 50
Attack successful, enemy life decreased by 10 to 40
Attack successful, enemy life decreased by 10 to 30
Attack successful, enemy life decreased by 10 to 20
Attack unsuccessful, enemy increases life to 30
Attack successful, enemy life decreased by 10 to 20
Attack successful, enemy life decreased by 10 to 10
Attack successful, enemy life decreased by 10 to 0
You defeated the enemy!
You could also make a difficulty attribute which set the chance of successful attacks by increasing or decreasing what you compare random() to:
class Enemy():
def __init__(self,diff):
self.dead = 0
self.life = 100
self.diff = diff
def attack(self):
if random() > self.diff:
self.life -= 10
print("Attack successful, enemy life decreased by 10 to {}".format(self.life))
elif self.life <= 90:
self.life += 10
print("Attack unsuccessful, enemy increases life to {}".format(self.life))
def health_check(self):
if self.life <= 0:
print("You defeated the enemy!")
sys.exit()
enemy1 = Enemy(.20)
If you wanted to break in the while, return in the method and check with an if:
class Enemy():
def __init__(self,diff):
self.dead = 0
self.life = 100
self.diff = diff
def attack(self):
if random() > self.diff:
self.life -= 10
print("Attack successful, enemy life decreased by 10 to {}".format(self.life))
elif self.life <= 90:
self.life += 10
print("Attack unsuccessful, enemy increases life to {}".format(self.life))
def health_check(self):
if self.life <= 0:
print("You defeated the enemy!")
return True
return False
enemy1 = Enemy(.3)
while True:
time.sleep(1)
enemy1.attack()
if enemy1.health_check():
break
time.sleep(1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.