Fred James
Fred James

Reputation: 65

Text Based Python RPG - Storing Weapons

I am new to programming and I am teaching myself Python and as a hobby I am making a text-based RPG.

My question is this; I want to store a list of weapons, armours etc that can be easily edited without screwing up my whole program. My thinking is that I make a separate module for them equipment.py and store them in a list like;

equipment = [ 
#Longsword
w1 = 15
#Axe
w2 = 17
]

I would just import this and use the values assigned to them would be used in whatever combat engine I decide to create. Is this even correct? Terrible coding conventions? I have much to learn and any wisdom you have to share is much appreciated.

EDIT: From the excellent answer below I have started it code it, how does my code look?

equipment = {

'Longsword':     {  'cost': 50,
                    'damage': 19,
                    'element': 'physical',
                    'description': 'Long steel blade used by the knights of Coamrin. The weight allows it to cleave foes with relative ease.'}

'Estoc':        {   'cost': 45,
                    'damage': 17,
                    'element': 'physical',
                    'description': 'Expertly crafted thin, steel blade designed for thrust and swiping motions.'}

}

Any tips? I have read through PEP 8 but my knowledge is not good enough or there is nothing specific enough to help in this particular case

Upvotes: 0

Views: 2883

Answers (2)

bernard paulus
bernard paulus

Reputation: 1664

In my opinion, you will be even better off storing those values outside python, in configuration files.

For instance, ConfigParser has a syntax that is readable even for users who don't know any programming languages.

This also enables the possibility of making an editor of the weapons inside your game, with permanent changes. Indeed, by using the dictionary approach, you would need to change a file your program is running, which is something I try to avoid.

Upvotes: 1

Andy Hayden
Andy Hayden

Reputation: 375575

I would suggest that using a python dictionary (rather than a list) would be preferable.

equipment = {
              'Longsword': 15,
              'Axe':       17
            }

That way you can refer to the longswords value as equipment['Longsword'] rather than by its position in the list equipment[0].

Putting it in a separate module could be a good idea, perhaps inside a module containing the attack/defence functionality, and other items effects.

.

One other suggestion you might be interested in (depending on the RPG's functionality) would be to have another dictionary for this and other attributes, so you could add other attributes in the future. That is,

equipment = {
             'Longsword': {'attack_str': 15, 'defence_str': 15, 'color': 'grey',... etc.}
             }

This allows additional functionality to be added later (without breaking previous mechanics).

Upvotes: 3

Related Questions