diff --git a/aufgabe9/Arena_inherit.py b/aufgabe9/Arena_inherit.py new file mode 100755 index 0000000000000000000000000000000000000000..e91abd83e4da8a591002f56c8ef23e49846b7d63 --- /dev/null +++ b/aufgabe9/Arena_inherit.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python3 + + +import random + + +class Colors: + + GREEN = '\033[92m' + BLUE = '\033[94m' + RED = '\033[91m' + RESET = '\033[0m' + + +class Player: + + def __init__(self, hp=20, strength=5): + + self.potion = True + self.hp = hp + self.strength = strength, + self.moves = {"tackle": int(strength), + "fireball": 8, + "kick": int(strength) + 2} + + def choice(self): + raise NotImplementedError + + def attack(self, target): + + choice = self.choice() + print("{0} uses {1}{2}{3}".format(self.name, Colors.RED, choice.title(), Colors.RESET)) + + try: + target.hp -= self.moves[choice] + except: + pass + + +class Human(Player): + + def __init__(self, name): + + super().__init__() + self.name = name + + def choice(self): + + moves = list(self.moves.keys()) + choice = str.lower(input("Your move {0}: ".format(moves))) + + return choice + + +class Enemy(Player): + + def __init__(self, name="CPU"): + + super().__init__() + self.name = name + + def choice(self): + + choice = random.choice(list(self.moves.keys())) + return choice + + +def battle(player, cpu): + + while player.hp > 0 and cpu.hp > 0: + + player.attack(cpu) + if cpu.hp <= 0: + print("Player wins") + break + + cpu.attack(player) + if player.hp <= 0: + print("CPU wins") + break + + print() + print("{0}'s HP at {1}{2}{3}".format(cpu.name, Colors.GREEN, cpu.hp, Colors.RESET)) + print("{0}'s HP at {1}{2}{3}".format(player.name, Colors.GREEN, player.hp, Colors.RESET)) + print() + + +def main(): + + player_name = str(input("Enter your Name: ")) + + player = Human(name=player_name) + cpu = Enemy() + + battle(player, cpu) + + +if __name__ == "__main__": + main()