I have been teaching myself python for several months and have started working on an rpg project. Right now I am trying to code an armor class for a basic character. I am not sure if my approach is even a correct way to go about implementing this class. The armor list items consist of a name, armor location, it's stopping power, weight penalty, and its cost.I managed to get the code to keep track of things then I realized if I append a new armor it will of course add it to the end of the list and I really want 3 designated slots to assign to. I would really appreciate some help with the code or even a thought process to help me rework this code. Any help would be appreciated. Thanks in advance.

# Armor
# An armor class
# 12/10/09

import random

# Armor Lists

body_armor = [("Light Leather", "Limbs and Torso", 0, 0, 25),
              ("Heavy Leather", "Limbs and Torso", 4, 0, 50),
              ("Kevlar Vest", "Torso", 10, 0, 100),
              ("Steel Helmet", "Head", 14, 0, 25),
              ("Light Armor Jacket", "Limbs and Torso", 14, 0, 150),
              ("Medium Armor Jacket", "Limbs and Torso", 1, 1, 200),
              ("Flack Vest", "Torso", 20, 1, 200),
              ("Flack Pants", "Legs", 20, 1, 200),
              ("Nylon Helmet", "Head", 20, 0, 100),
              ("Heavy Armor Jacket", "Limbs and Torso", 20, 2, 250),
              ("Door Gunner's Vest", "Torso", 25, 3, 275),
              ("Full Metal", "Whole Body", 25, 2, 600)]

class Character(object):
    def __init__(self):
        self.armor = Armor()

    def Get_Armor_Report(self):
        print self.armor.armor_list
        print self.armor.current_armor
        print self.armor.EV_Total()

# NEEDS ---> 3 current armor places need to remain body in 0, body2 in 1, and helmet in 2
        
class Armor(object):
    def __init__(self):
        self.armor_list = []
        self.current_armor = []

    def Add_Armor(self, number):
        new_armor = body_armor[number]
        self.armor_list.append(new_armor)

    def Equip_Armor(self, number):
        max_check = self.Max_Armor()
        if max_check == 0:
            print "Max Armor - Cannot Equip New Armor"
        else:
            armor_name = self.armor_list[number][0]
            armor_sp = self.armor_list[number][2]
            armor_ev = self.armor_list[number][3]
            current_armor = (armor_name, armor_sp, armor_ev)
            self.current_armor.append(current_armor)

    def Remove_Armor(self, number):
        remove_armor = self.current_armor[number]
        self.current_armor.remove(remove_armor)

    def Get_SP(self, number):
        stop_power = self.current_armor[number][1]
        return stop_power

    def Get_EV(self, number):
        encumberance = self.current_armor[number][2]
        return encumberance
    
    def Get_Cover(self, number):
        cover = self.armor_list[number][1]
        return cover

    def EV_Total(self):
        ev_total = 0
        list_count = self.current_armor
        list_count = len(list_count)
        for i in range(list_count):
            number = i
            ev = self.current_armor[number][2]
            ev_total += ev
        return ev_total

    def Max_Armor(self):
        all_armor = self.current_armor
        armor_total = len(all_armor)
        if armor_total >= 3:
              return 0
        else:
              return 1

Recommended Answers

All 7 Replies

I'm a newbie programmer myself, so without playing around with your code for a while I have no idea how it works from just a glance, but from what I understand of your needs I would suggest creating a function that examines what the armor type is, and then replaces the desired armor in the character's current equip list. Random example:

if armor_list[1] == "Torso":
    character.armorTorso = armor_list[0,2:4]
elif armor_list[1] == "Head":
    character.armorHead = armor_list[0,2:4]

And so on. It is a little more work, but it is rather straightforward. I hope you understand the gist of what I am getting at. Adding or subtracting numbers works almost the same way, just a little more coding required.

So you want 3 slots on the body where you can have armor, and some armor can take up more than one of those slots, and you want to have armor equipped only in the slot it should go in if it can?

If that's the case, the character could have a self.armorList and a self.equipStatus . The armor list would obviously be a list of the armor the character has on, and the equip status would be a binary number with 3 digits, with 1s where armor is equipped and 0s where it isn't. Each piece of armor has a corresponding binary number, self.armorGoes , for where it should go, and it could be checked against the equip status whenever one tries to equip the armor. If armorGoes & equipStatus is anything other than 0, the player already has something that goes in one of the slots that the piece needs to go into. If it can be equipped, then simply add the two numbers and add the item to the armorList . Unequipping removes it from the list and subtracts armorGoes from equipStatus . The good thing about this method is that it allows you to make more than 3 slots and have armor that takes up more than one slot fairly easily.

That being said, this may not be what you're looking for at all. But maybe it'll inspire you.

Thanks for the responses. Mathhax0r, your response is exactly what I am trying to achieve. Is there anyway you can give me an example code for the armorGoes & equipStatus checks. I seem to be struggling with it.

This is where I am so far...

# Armor 
# An armor class
# 12/19/09

import random

# Armor Lists

bodyArmor = [("Light Leather", 1, "Limbs and Torso", 0, 0, 25)]

class Character(object):
    def __init__(self):
        self.armor = Armor()
        
class Armor(object):
    def __init__(self):
        self.armorList = []
        self.equipStatus = [0,0,0]

    def addArmor(self, number):
        new_armor = bodyArmor[number]
        self.armorList.append(new_armor)

    def armorGoes(self, number):
        armorLocation = self.armorList[number][1]
        

    def equipArmor(self, number):

Here's an example of an equip method.

def equip(self, argArmor):
    if self.equipStatus & argArmor.goes:
        print "You can't equip this armor right now"
    else:
        self.armorList.append(argArmor)
        self.equipStatus += argArmor.goes

And for the goes property of the armor, it would be something like 0b100 for a helmet or 0b111 for full body armor. You can add additional slots on the body by simply adding in more digits. However, binary literals are enabled in 2.6 or higher. I use version 2.5.4 of python, so I just used hexadecimal numbers. There'll be no difference aside from efficiency, though. Remember to initialize self.equipStatus to 0b000 when you make a new character.

EDIT: I just posted up my example and missed your update. I would make the armor class just have some properties and none of those functions. A character is the one who equips the armor.

class Character:
    def __init__(self):
        self.equipStatus = 0b000
        self.armorList = []
    def equip(self, argArmor):
        if self.equipStatus & argArmor.goes:
            print "You can't equip this armor right now"
        else:
            self.armorList.append(argArmor)
            self.equipStatus += argArmor.goes
class Armor:
    def __init__(self, name, whereGoes, weight, cost):
        self.name = name
        self.goes = whereGoes
        self.weight = weight
        self.cost = cost

Brilliant! Thank you so much for the insight. I am not sure if this is a stuipd question but with this arrangement how should I go about using the list and the new armor class?

You mean like when should you create the armor and in what data structures should you store it when it's not equipped? The armorList of the character is only necessary within the character. It's used in the equip function, the unequip function when you write one, and the function that calculates and updates the player's stats, which btw should probably be called inside of the equip and unequip functions.

I would create armor whenever you go into a shop for instance, or whenever you encounter a monster that has it, and the purchasing or looting of the armor would move it from one of their lists to the character's inventory list, a seperate list from armorList that contains all the armor, potions, weapons, or w/e the character picks up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.