CollegeWork/OOP/Knights/main.py

91 lines
3.1 KiB
Python

from random import choice, randint
from time import sleep
from os import system, name as os_name
class Color:
red = "\033[91m"
green = "\033[92m"
yellow = "\033[93m"
blue = "\033[94m"
purple = "\033[95m"
cyan = "\033[96m"
norm = "\033[0m"
class Knight:
FIRST = ["Lancelot", "Charles", "Henry", "William"]
NICK = ["Brave", "Horrific", "Courageous", "Terrific"]
def __init__(self):
self.actions = ["ATTACK", "ATTACK", "ATTACK", "DEFEND", "REST"]
self.name = f"{choice(Knight.FIRST)} The {choice(Knight.NICK)}"
self.state = None
self.health = 100
def action(self, player):
self.state = choice(self.actions)
def update_health(self, player):
if player == Game.PLAYERS[0]:
if player.state in ["ATTACK", "REST"] and Game.PLAYERS[1].state == "ATTACK":
player.health -= 5
else:
if player.state in ["ATTACK", "REST"] and Game.PLAYERS[0].state == "ATTACK":
player.health -= 5
class Game:
# Do not like this being outside of init, figure out a way to have it inside
PLAYERS = []
def __init__(self):
for i in range(2):
self.PLAYERS.append(Knight())
def gameloop(self):
while self.PLAYERS[0].health > 0 and self.PLAYERS[1].health > 0:
system("cls" if os_name == "nt" else "clear")
print(f"{Color.cyan}-----------------------------------------------{Color.norm}")
for player in self.PLAYERS:
print(f"{Color.green}{player.name}{Color.norm}: {player.health}")
print(f"{Color.cyan}-----------------------------------------------{Color.norm}")
sleep(1)
for player in self.PLAYERS:
player.action(player)
match player.state:
case "ATTACK":
print(f"{Color.green}{player.name} {Color.red}attacks{Color.norm} the enemy!")
case "DEFEND":
print(f"{Color.green}{player.name} {Color.red}defends{Color.norm} from any attacks.")
case "REST":
print(f"{Color.green}{player.name}{Color.norm} is tired and will {Color.red}rest{Color.norm} for this round")
sleep(1)
for player in self.PLAYERS:
player.update_health(player)
sleep(1)
system("cls" if os_name == "nt" else "clear")
self.game_end()
def game_end(self):
print(f"{Color.cyan}-----------------------------------------------{Color.norm}")
for player in self.PLAYERS:
print(f"{Color.green}{player.name}{Color.norm}: {player.health}")
print(f"{Color.cyan}-----------------------------------------------{Color.norm}")
if self.PLAYERS[0].health == self.PLAYERS[1].health:
print(f"Both brave knights sadly die from their battle wounds!")
elif self.PLAYERS[0].health == 0:
print(f"{self.PLAYERS[1].name} won this mighty battle!")
else:
print(f"{self.PLAYERS[0].name} has conquered his enemy!")
if __name__ == "__main__":
Game().gameloop()