Skip to main content
Tweeted twitter.com/StackCodeReview/status/1523815103543525376
Became Hot Network Question
edited tags; edited tags
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Heyy, 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 eelegant of a codeelegant? I would appreciate some tips and tricks the Gods in here can share me, thank you! :D

Heyy, 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 eelegant of a code? I would appreciate some tips and tricks the Gods in here can share me, thank you! :D

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?

edited tags
Link
Reinderien
  • 65.3k
  • 5
  • 69
  • 187

Trying to optimize a python program Battle terminal-based game

Source Link

Trying to optimize a python program

Heyy, 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 eelegant of a code? I would appreciate some tips and tricks the Gods in here can share me, thank you! :D

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")