def perform_actions(attacker, target): # Do not remove vars target if attacker.human_mode: # human's turn while True: action_key = input(f"{Action.text}") if not_number(action_key): continue action_key = int(action_key) if action_key > len(Action.collection) or action_key < 1: print(f"Please enter only 1-{len(Action.collection)}") continue action_success = eval( Action.collection[action_key][1]) # Return True or False if not action_success: # if the action cannot be carried out continue time.sleep(Database.sleep) break else: # computer's turn while True: if not attacker.human_team: # if computer not in human team. cprint( f"{attacker.name.upper()}: ", color=Color.name_bot, attrs=["bold"], ) action_key = random.randint(1, len(Action.collection)) action_success = eval(Action.collection[action_key][1]) if not action_success: continue time.sleep(Database.sleep) break
def choose_victim(attacker, targets): if attacker.human_mode: # human's mode while True: Game.show_computers() index = input( colored(f"Select enemy to attack: ", color=Color.command)) if not_number(index): continue index = int(index) if index > Database.num_player or index < 1: print(f"Please enter only 1-{Database.num_player}\n") continue victim = targets[index - 1] if victim.dead: print( f"\n{victim.name.upper()} is {colored('DEAD', color=Color.dead_bot)}, pick another target\n" ) continue break else: # computer's turn while True: victim = targets[random.randint(0, Database.num_player - 1)] if victim.dead: continue break return victim
def choose_mode(): print("""Available play mode 1: You + computer vs computers 2: You vs computer(s)""") while True: choice = input(colored("Choose play mode: ", Color.command)) if not_number(choice): continue choice = int(choice) if choice not in [1, 2]: print("Please enter only 1 or 2\n") continue break if choice == 1: man_vs_bot = False else: man_vs_bot = True return man_vs_bot
def choose(attacker): num_magics = len(Magic.collection) magic = None if attacker.human_mode: # human's turn while True: Magic.show(attacker) index = input(colored("Select magic: ", color=Color.command)) if index == "*": return None if not_number(index): continue index = int(index) if index > num_magics or index < 1: print(f"Please enter only 1-{num_magics}") continue magic = Magic.collection[index] break else: # computer's turn magic = Magic.collection[random.randint(1, num_magics)] return magic
def choose(user): num_items = len(Item.collection) item = None if user.human_mode: # human's turn while True: Item.show() index = input(colored("Select item: ", color=Color.command)) if index == "*": return None if not_number(index): continue index = int(index) if index > num_items or index < 1: print(f"Please enter only 1-{num_items}") continue item = Item.collection[index] break else: # computer's turn item = Item.collection[random.randint(1, num_items)] return item
def create_players(enemy_names): """Create players - humans and computers""" while True: man_vs_bot = Game.choose_mode( ) # True if in mode players vs computers num_player = input( colored("Number of players (1-5, or * to go back): ", color=Color.command)) if num_player == '*' or not_number(num_player): continue Database.num_player = int(num_player) if Database.num_player > 5 or Database.num_player < 1: print( "The game can only have 1-5 players. Please enter the number of players again.\n" ) continue break print("Please enter the name(s) of player(s)") for i in range(Database.num_player): text = colored(f"Player {i + 1}: ", Color.command) if man_vs_bot: # Players vs computers mode Database.players.append( Person(name=input(text).capitalize(), human_mode=True)) else: # Player + computer vs computers mode if i == 0: Database.players.append( Person(name=input(text).capitalize(), human_mode=True)) elif i > 0: Database.players.append( Person( name=input(text).capitalize(), human_mode=False, human_team=True, )) Database.computers.append( Person(name=enemy_names[i], human_mode=False, human_team=False))