You can use the class called BaseUnit/Player/Enemy and create a instance called dragonslayer/dragon1/dragon2 to store the values for each instance.
Then, you can define a function called battle to let them fight.
This path will be more difficult to understand. But if you match this, you can do easily to handle with mutli enemies.
import logging
import random
class BaseUnit(object):
def __init__(self, unit_name, base_health, base_attack, base_heal=None):
self.name = unit_name
self.max_health = base_health
self.current_health = base_health
self.current_attack = base_attack
self.current_heal = base_heal
def attack(self, target):
point = random.randint(self.current_attack[0], self.current_attack[1])
if target.current_health >= point:
target.current_health = target.current_health - point
else:
target.current_health = 0
print('{0} does {1} points damage to {2}.'.format(self.name, point, target.name))
def heal(self):
point = random.randint(self.current_heal[0], self.current_heal[1])
if self.current_health + point <= self.max_health:
self.current_health = self.current_health + point
else:
self.current_health = self.max_health
print('{0} heals himself {1} points of health.'.format(self.name, point))
def get_current_health(self):
print('The health of {0} is {1} now.'.format(self.name, self.current_health))
class Enemy(BaseUnit):
def heal(self):
print("The enemy don't know how to heal himself.")
pass
class Player(BaseUnit):
def upgrade(self, uppoint_min, uppoint_max):
max_health_up = random.randint(uppoint_min, uppoint_max) * 10
max_attack_up = random.randint(uppoint_min, uppoint_max)
min_attack_up = random.randint(uppoint_min, uppoint_max)
if min_attack_up > max_attack_up:
min_attack_up = max_attack_up
max_heal_up = random.randint(uppoint_min, uppoint_max)
min_heal_up = random.randint(uppoint_min, uppoint_max)
if min_heal_up > max_heal_up:
min_heal_up = max_heal_up
self.max_health = self.max_health + max_health_up
self.base_attack = (self.current_attack[0] + min_attack_up, self.current_attack[1] + max_attack_up)
self.base_heal = (self.current_heal[0] + min_heal_up, self.current_heal[1] + max_heal_up)
print("""You are upgraded!
Your max health is {0} now.
Your base_attack is {1}~{2} now.
Your base_heal is {3}~{4} now.
""".format(self.max_health, self.base_attack[0], self.base_attack[1], self.base_heal[0], self.base_heal[1]))
def battle(player, enemy):
while player.current_health > 0 and enemy.current_health > 0:
choice = input("Enter your choice of action: \n1 - Attack\n2 - Heal\nChoice: ")
choice = str(choice)
if choice == "1":
player.attack(enemy)
enemy.attack(player)
player.get_current_health()
enemy.get_current_health()
if choice == "2":
player.heal()
enemy.attack(player)
player.get_current_health()
enemy.get_current_health()
if player.current_health == 0 and enemy.current_health > 0:
print('You are defeated, but {0} still alive.'.format(enemy.name))
elif player.current_health > 0 and enemy.current_health == 0:
print('At last You defeat {0}.'.format(enemy.name))
dragonslayer.upgrade(1, 5)
elif player.current_health == 0 and enemy.current_health == 0:
print('You defeat {0}, but {0} also defeats you.'.format(enemy.name))
if __name__ == '__main__':
logging.basicConfig(
level=logging.INFO,
# filename=os.path.basename(__file__) + '_' + time.strftime('%Y%m%d', time.localtime()) + '.log',
# filemode='a',
format='%(asctime)s %(name)s %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
logging.debug('start DEBUG')
logging.debug('==========================================================')
dragonslayer = Player('shiratori', base_health=100, base_attack=(2, 15), base_heal=(5, 20))
dragon1 = Enemy('SmallDragon', base_health=50, base_attack=(5, 10))
dragon2 = Enemy('BigDragon', 200, (7, 15))
battle(dragonslayer, dragon1)
if dragonslayer.current_health > 0:
battle(dragonslayer, dragon2)
logging.debug('==========================================================')
logging.debug('end DEBUG')
health. When you say "Player health is now...", that's not true because you've not modifiedhealth.healthvalues, those values are only ever printed and never reassigned back to the original variables. See the answers on how this should be done.player_attackandplayer_healcompute the new health values in local variables, and return those new values. But the caller needs to assign the results toeHealth, which it doesn't. All it does is print them, then discard them. The calls need to look likeeHealth = player_attach(eHealth), after which you can print the new value ofeHealth.