Generation

generate functionFri, 07 Jul 2023

import random class Part(): def __init__(self, name, attack_level=0, defense_level=0, energy_consumption=0): self.name = name self.attack_level = attack_level self.defense_level = defense_level self.energy_consumption = energy_consumption def get_status_dict(self): formatted_name = self.name.replace(" ","_").lower() return { "{}_name".format(formatted_name): self.name.upper(), "{}_status".format(formatted_name): self.is_available(), "{}_attack".format(formatted_name): self.attack_level, "{}_defense".format(formatted_name): self.defense_level, "{}_energy_consump".format(formatted_name): self.energy_consumption, } def is_available(self): return not self.defense_level <= 0 colors = { "Black": '\x1b[90m', "Blue": '\x1b[94m', "Cyan": '\x1b[96m', "Green": '\x1b[92m', "Magenta": '\x1b[95m', "Red": '\x1b[91m', "White": '\x1b[97m', "Yellow": '\x1b[93m', } class Robot: def __init__(self, name, color_code): self.name = name self.color_code = color_code self.energy = 100 self.parts = [ Part("Head", attack_level=5, defense_level=10, energy_consumption=5), ##cabeza Part("Weapon", attack_level=15, defense_level=0, energy_consumption=10), ##arma Part("Left Arm", attack_level=3, defense_level=20, energy_consumption=10), ##brazo derecho Part("Right Arm", attack_level=6, defense_level=20, energy_consumption=10), ##brazo izquierdo Part("Left Leg", attack_level=4, defense_level=20, energy_consumption=15), ##pierna derecha Part("Right Leg", attack_level=8, defense_level=20, energy_consumption=15), ##pierna izquierda ] def greet(self): print("Hello, my name is", self.name) def print_energy(self): print("We have", self.energy, "percent energy left") def attack(self, enemy_robot, part_to_use, part_to_attack): enemy_robot.parts[part_to_attack].defense_level -= self.parts[part_to_use].attack_level self.energy -= self.parts[part_to_use].energy_consumption def is_on(self): return self.energy >= 0 def is_there_available_parts(self): for part in self.parts: if part.is_available(): return True return False def print_status(self): print(self.color_code) str_robot = "" # Define el contenido de robot_art print(str_robot) self.greet() self.print_energy() print(str_robot) print(colors["Black"]) def get_part_status(self): part_status = {} for part in self.parts: status_dict = part.get_status_dict() part_status.update(status_dict) return part_status def build_robot(): robot_name = input("Robot Name: ") color_code = choose_color() robot = Robot(robot_name, color_code) comprar_dado(robot) # Comprar dados antes de imprimir el estado del robot robot.print_status() return robot def choose_color(): for color in colors: print(colors[color], color) aux_color = input("") return colors[aux_color] class Dado: @staticmethod def tirar_dado(): resultado = random.randint(1, 6) return resultado @staticmethod def aplicar_efecto(resultado, robot, part_to_use): if resultado % 2 == 0: # Número par print("¡Efecto par activado!") print("Multiplicando ataque por", resultado) robot.parts[part_to_use].attack_level *= resultado else: # Número impar print("¡Efecto impar activado!") print("Incrementando defensa por", resultado) robot.parts[part_to_use].defense_level += resultado def play(): playing = True print("Welcome to the game") robot_one = build_robot() robot_two = build_robot() round_num = 0 last_dice_roll = None while playing: if round_num % 2 == 0: current_robot = robot_one enemy_robot = robot_two else: current_robot = robot_two enemy_robot = robot_one current_robot.print_status() print("What part should I use to attack?") part_to_use = int(input("Choose a number part: ")) enemy_robot.print_status() print("What part of the enemy should we attack?") part_to_attack = int(input("Choose an enemy part to attack: ")) dice_roll = Dado.tirar_dado() print("Dice roll:", dice_roll) Dado.aplicar_efecto(dice_roll, current_robot, part_to_use) current_robot.attack(enemy_robot, part_to_use, part_to_attack) round_num += 1 if dice_roll == last_dice_roll: playing = False print("You lost the game due to rolling the same number twice.") print("Better luck next time!") last_dice_roll = dice_roll if not enemy_robot.is_on() or not enemy_robot.is_there_available_parts(): playing = False print("Congratulations! You won the game!") print("Winner:", current_robot.name) play() corrigelo

# -*- coding: utf-8 -*- """ Created on Thu Apr 2 10:07:40 2020 @author: Usuario """ import random class Part(): def __init__(self, name, attack_level=0, defense_level=0, energy_consumption=0): self.name = name self.attack_level = attack_level self.defense_level = defense_level self.energy_consumption = energy_consumption def get_status_dict(self): formatted_name = self.name.replace(" ","_").lower() return { "{}_name".format(formatted_name): self.name.upper(), "{}_status".format(formatted_name): self.is_available(), "{}_attack".format(formatted_name): self.attack_level, "{}_defense".format(formatted_name): self.defense_level, "{}_energy_consump".format(formatted_name):

Questions about programming?Chat with your personal AI assistant