Skip to content
Snippets Groups Projects
Commit 35ce8b8f authored by Markus Opolka's avatar Markus Opolka
Browse files

Add example with super class

parent 2d593fd1
No related branches found
No related tags found
No related merge requests found
#!/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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment