Make A Gunpla

Video referencia de la clase a modelar


In [1]:
from IPython.display import HTML

# Youtube
HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/FTd8xr9y5ig?rel=0&amp;controls=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>')


Out[1]:

Crear instancias


In [2]:
from gundam import GunplaPart, Gun, MeleeWeapon

Gunpla Parts


In [3]:
turn_a_r_arm = GunplaPart("Turn A Gundam R Arm",GunplaPart.R_ARM,5,9,60)
turn_a_l_arm = GunplaPart("Turn A Gundam L Arm",GunplaPart.L_ARM,5,9,60)
turn_a_waist = GunplaPart("Turn A Gundam Waist",GunplaPart.WAIST,10,10,20)
turn_a_backpack = GunplaPart("Turn A Gundam Backpack",GunplaPart.BACKPACK,1,1,700)
turn_a_head = GunplaPart("Turn A Gundam Head",GunplaPart.HEAD,1,1,20)
turn_a_chest = GunplaPart("Turn A Gundam Chest",GunplaPart.CHEST,5,20,200)

In [4]:
print(turn_a_l_arm)


Gunpla Part: Turn A Gundam L Arm <(Left Arm)>

In [5]:
turn_a_l_arm


Out[5]:
Gunpla Part: Turn A Gundam L Arm <(Left Arm)>
	 Size: 5
	 Armor: 60
	 Hitpoints: 225.0
	 Weight: 9

In [6]:
strike_noir_r_arm = GunplaPart("Strike Gundam E R Arm",GunplaPart.R_ARM,5,11,50)
strike_noir_l_arm = GunplaPart("Strike Gundam E L Arm",GunplaPart.L_ARM,5,11,50)
strike_noir_waist = GunplaPart("Strike Gundam E Waist",GunplaPart.WAIST,10,10,20)
strike_noir_backpack = GunplaPart("Noir Strike Backpack",GunplaPart.BACKPACK,4,4,500)
strike_noir_head = GunplaPart("Strike Gundam E Head",GunplaPart.HEAD,2,1,25)
strike_noir_chest = GunplaPart("Strike Gundam E Chest",GunplaPart.CHEST,5,20,150)

barbatos_r_arm = GunplaPart("Gundam Barbatos R Arm",GunplaPart.R_ARM,5,3,90)
barbatos_l_arm = GunplaPart("Gundam Barbatos L Arm",GunplaPart.L_ARM,5,3,90)
barbatos_waist = GunplaPart("Gundam Barbatos Waist",GunplaPart.WAIST,10,9,200)
barbatos_backpack = GunplaPart("Gundam Barbatos Backpack",GunplaPart.BACKPACK,1,0,100)
barbatos_head = GunplaPart("Gundam Barbatos Head",GunplaPart.HEAD,1,1,15)
barbatos_chest = GunplaPart("Gundam Barbatos Chest",GunplaPart.CHEST,6,35,700)

Weapons


In [7]:
beam_rifle = Gun(name="57mm High-energy Beam Rifle",rounds=8,damage_per_shot=50)
grenade_launcher = Gun(name="175mm Grenade Launcher",rounds=1,damage_per_shot=500)
beam_cannon = Gun(name="Beam Cannon",rounds=2,damage_per_shot=100)

beam_blade = MeleeWeapon(name="MR-Q10 'Fragarach 3' Beam Blade",damage=200)
beam_saber = MeleeWeapon(name="Beam Saber",damage=150)
mace = MeleeWeapon(name="Pile Bunker",damage=400)

Clase Gundam


In [8]:
from random import random, shuffle

class Gundam:
    """Clase que representa un Gunpla (Gundam) 
        pensado para combatir entre si."""
    
    def __init__(self,nombre):
        """Construye una clase Gundam sin piezas.
            nombre: Es el identificador del Gundam, debe tener 
            representacion en string"""
        self.hitpoints = 0
        self.main_weapon = None
        self.sub_weapon = None
        self.frame = {}
        self.nombre = str(nombre)
             
    def attach(self,parte):
        """Adosa una parte al Gundam. 
            Si habia una parte del mismo tipo adosada anteriormetente la 
            reemplaza y la devuelve"""
        tipo_parte = parte.get_part_type()
        parte_anterior = self.frame.get(tipo_parte,None)
        self.frame[tipo_parte] = parte
        return parte_anterior
    
    def attack(self,otro_gundam):
        """Recibe otro Gundam y lo ataca"""
        
        random_number = random()
        if random_number > 0.95:
            self.main_weapon.atack(otro_gundam)
            self.sub_weapon.atack(otro_gundam)
        elif random_number > 0.20:
            self.main_weapon.atack(otro_gundam)
        else:
            self.sub_weapon.atack(otro_gundam)
    
    def set_main_weapon(self,weapon):
        
        self.main_weapon = weapon
        
    def set_sub_weapon(self,weapon):
        
        self.sub_weapon = weapon
    
    def get_hitpoints(self):
        
        return self.hitpoints
    
    def receive_damage(self,damage):
    
        efective_damage = (damage - self.get_armor()/10)
        
        if efective_damage < 0:
            # A lo sumo puede recibir 0, pero no curarse
            efective_damage = 0
        
        self.hitpoints -= efective_damage
        
    def __str__(self):
        return self.nombre
    
    def get_max_hitpoints(self):
        """Devuelve el maximo de puntos de vida que puede tener el Gundam
            dadas las partes adosadas"""
        hitpoints = 0
        for _,part in self.frame.items():
            hitpoints += part.get_hitpoints()
        return hitpoints
    
    def get_armor(self):
        
        armor_points = 0
        for _,part in self.frame.items():
            armor_points += part.get_armor()
        return armor_points
    
    def recharge(self):
        """Recarga la energia (hitpoints) completamente"""
        self.hitpoints = self.get_max_hitpoints()

In [9]:
strike_noir_gundam = Gundam("Strike Noir Gundam")

strike_noir_gundam.attach(strike_noir_head)
strike_noir_gundam.attach(strike_noir_backpack)
strike_noir_gundam.attach(strike_noir_chest)
strike_noir_gundam.attach(strike_noir_l_arm)
strike_noir_gundam.attach(strike_noir_r_arm)
strike_noir_gundam.attach(strike_noir_waist)

strike_noir_gundam.set_main_weapon(beam_rifle)
strike_noir_gundam.set_sub_weapon(beam_blade)
strike_noir_gundam.recharge()

In [10]:
turn_a_gundam = Gundam("Turn A Gundam")

turn_a_gundam.attach(turn_a_head)
turn_a_gundam.attach(turn_a_backpack)
turn_a_gundam.attach(turn_a_chest)
turn_a_gundam.attach(turn_a_l_arm)
turn_a_gundam.attach(turn_a_r_arm)
turn_a_gundam.attach(turn_a_waist)

turn_a_gundam.set_main_weapon(beam_cannon)
turn_a_gundam.set_sub_weapon(beam_saber)
turn_a_gundam.recharge()

In [11]:
barbatos = Gundam("Gundam Barbatos")

barbatos.attach(barbatos_head)
barbatos.attach(barbatos_backpack)
barbatos.attach(barbatos_chest)
barbatos.attach(barbatos_l_arm)
barbatos.attach(barbatos_r_arm)
barbatos.attach(barbatos_waist)

barbatos.set_main_weapon(mace)
barbatos.set_sub_weapon(grenade_launcher)
barbatos.recharge()

Combat simulation


In [12]:
my_gundams = [strike_noir_gundam,turn_a_gundam,barbatos]
shuffle(my_gundams)

red_side , blue_side = my_gundams[0] , my_gundams[-1]
gundams = [red_side,blue_side]

active_gundam = 0

while red_side.get_hitpoints() > 0 \
        and blue_side.get_hitpoints() > 0 :
    attacker = gundams[active_gundam % 2]
    defender = gundams[(active_gundam+1) % 2]

    print(f"{attacker} attacks {defender}")
    old_hp = defender.get_hitpoints()
    attacker.attack(defender)
    print(f"Damage dealt {old_hp - defender.get_hitpoints()}")
    
    active_gundam += 1
          
winner = red_side
if blue_side.get_hitpoints() > 0:
    winner = blue_side

print(f"Winner: {winner}")


Strike Noir Gundam attacks Turn A Gundam
Damage dealt 94.0
Turn A Gundam attacks Strike Noir Gundam
Damage dealt 20.5
Strike Noir Gundam attacks Turn A Gundam
Damage dealt 94.0
Turn A Gundam attacks Strike Noir Gundam
Damage dealt 20.5
Strike Noir Gundam attacks Turn A Gundam
Damage dealt 494.0
Turn A Gundam attacks Strike Noir Gundam
Damage dealt 220.5
Strike Noir Gundam attacks Turn A Gundam
Damage dealt 94.0
Turn A Gundam attacks Strike Noir Gundam
Damage dealt 170.5
Strike Noir Gundam attacks Turn A Gundam
Damage dealt 144.0
Turn A Gundam attacks Strike Noir Gundam
Damage dealt 370.5
Strike Noir Gundam attacks Turn A Gundam
Damage dealt 94.0
Turn A Gundam attacks Strike Noir Gundam
Damage dealt 0.0
Strike Noir Gundam attacks Turn A Gundam
Damage dealt 369.0
Turn A Gundam attacks Strike Noir Gundam
Damage dealt 0.0
Strike Noir Gundam attacks Turn A Gundam
Damage dealt 344.0
Turn A Gundam attacks Strike Noir Gundam
Damage dealt 170.5
Strike Noir Gundam attacks Turn A Gundam
Damage dealt 694.0
Turn A Gundam attacks Strike Noir Gundam
Damage dealt 595.5
Strike Noir Gundam attacks Turn A Gundam
Damage dealt 319.0
Winner: Strike Noir Gundam