I'm new to learning OOP in python and played around with it, making this quick battle terminal based game-ish, but I'm wondering how I can optimize this to maybe less lines of code and just overall being more elegant?
import random
class Enemy():
def __init__(self, species, enemy_health):
self.species = species
self.enemy_health = enemy_health
def heal(self):
self.enemy_health += random.randint(0, 5)
def lose_enemy_health(self):
self.enemy_health -= random.randint(0, 20)
class Player():
def __init__(self, player, player_health):
self.player = player
self.player_health = player_health
def heal(self):
self.player_health += random.randint(0, 5)
def lose_player_health(self):
self.player_health -= random.randint(0, 20)
enemies = ["Vampire", "Zombie", "Wassup", "1N4RR"]
enemy_fight = random.choice(enemies)
enemy = Enemy(enemy_fight, 50)
player = input("Enter your player name: ")
player = Player(player, 50)
print(">Welcome",player.player,"to text based RPG!!\n")
print(">A wild",enemy.species,"has appeared! You must fight it!!\n")
while enemy.enemy_health or player.player_health > 0:
player_move = input(">Attack or heal?: \n").lower()
if player_move == "attack":
enemy.lose_enemy_health()
if enemy.enemy_health < 1:
print(">The enemy died!\n")
break
else:
print(">The enemy now only has",enemy.enemy_health,"health!\n")
elif player_move == "heal":
player.heal()
print(">You healed and now have",player.player_health,"health\n")
enemy_moves = ["attack", "heal"]
enemy_move = random.choice(enemy_moves)
if enemy_move == "attack":
player.lose_player_health()
if player.player_health < 1:
print(">You died loser!!\n")
break
else:
print(">The enemy attacked!\n")
print(">You now only have",player.player_health,"health!\n")
elif enemy_move == "heal":
enemy.heal()
print(">The enemy healed and now has",enemy.enemy_health,"health!\n")