CollegeWork/OOP/main.py

82 lines
2.3 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"]
ACTION = ["ATTACK", "DEFEND", "REST"]
2023-04-20 13:56:46 +01:00
def __init__(self):
2023-04-21 20:58:48 +01:00
for i in range(3):
self.ACTION.append("ATTACK")
self.name = f"{choice(Knight.FIRST)} The {choice(Knight.NICK)}"
self.state = ""
self.health = 100
def action(self, player):
action = choice(Knight.ACTION)
self.state = action
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:
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:
system("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"{player.name} attacks!")
case "DEFEND":
print(f"{player.name} defends from any attacks.")
case "REST":
print(f"{player.name} is tired and will not do anything this round")
sleep(2)
for player in self.PLAYERS:
player.update_health(player)
sleep(2)
self.end_game()
2023-04-20 13:56:46 +01:00
2023-04-21 20:58:48 +01:00
def end_game(self):
print("Game End")
2023-04-20 13:56:46 +01:00
2023-04-21 20:58:48 +01:00
if __name__ == "__main__":
game = Game()
game.gameloop()