CollegeWork/OOP/main.py

91 lines
3.1 KiB
Python
Raw Normal View History

2023-04-21 20:58:48 +01:00
from random import choice, randint
from time import sleep
from os import system
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"]
2023-04-20 13:56:46 +01:00
def __init__(self):
2023-04-25 11:23:25 +01:00
self.actions = ["ATTACK", "ATTACK", "ATTACK", "DEFEND", "REST"]
2023-04-21 20:58:48 +01:00
self.name = f"{choice(Knight.FIRST)} The {choice(Knight.NICK)}"
2023-04-25 11:23:25 +01:00
self.state = None
2023-04-21 20:58:48 +01:00
self.health = 100
def action(self, player):
2023-04-25 11:23:25 +01:00
self.state = choice(self.actions)
2023-04-21 20:58:48 +01:00
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
2023-04-20 13:56:46 +01:00
2023-04-21 20:58:48 +01:00
class Game:
2023-04-25 11:23:25 +01:00
# Do not like this being outside of init, figure out a way to have it inside
2023-04-21 20:58:48 +01:00
PLAYERS = []
2023-04-20 13:56:46 +01:00
def __init__(self):
2023-04-21 20:58:48 +01:00
for i in range(2):
self.PLAYERS.append(Knight())
def gameloop(self):
while self.PLAYERS[0].health > 0 and self.PLAYERS[1].health > 0:
2023-04-25 11:23:25 +01:00
system("cls" if name == "nt" else "clear")
2023-04-21 20:58:48 +01:00
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":
2023-04-25 11:23:25 +01:00
print(f"{Color.green}{player.name} {Color.red}attacks{Color.norm} the enemy!")
2023-04-21 20:58:48 +01:00
case "DEFEND":
2023-04-25 11:23:25 +01:00
print(f"{Color.green}{player.name} {Color.red}defends{Color.norm} from any attacks.")
2023-04-21 20:58:48 +01:00
case "REST":
2023-04-25 11:23:25 +01:00
print(f"{Color.green}{player.name}{Color.norm} is tired and will {Color.red}rest{Color.norm} for this round")
sleep(1)
2023-04-21 20:58:48 +01:00
for player in self.PLAYERS:
player.update_health(player)
2023-04-25 11:23:25 +01:00
sleep(1)
system("cls" if name == "nt" else "clear")
2023-04-20 13:56:46 +01:00
2023-04-25 11:23:25 +01:00
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!")
2023-04-20 13:56:46 +01:00
2023-04-21 20:58:48 +01:00
if __name__ == "__main__":
2023-04-25 11:23:25 +01:00
Game().gameloop()