I'm making an RPG, and am trying to split things up into multiple files to organize things better. (I mean, character creation alone was over 2000 lines.) But this is something that I've never done, and I'm really not sure how to do it. So, if I could get some general advice on how to set things up efficiently, etc. I would really appreciate it. I really haven't been able to find much on the subject, other than the page about modules on the python website, which I did read, but I'm still a little unclear about it. This is definitely the most confusing, least talked about, thing I've come across so far in programming... and I do hate asking vague questions like this, but I'm definitely not making any progress on my own, so I'm hoping one of you can put me on the right path.
I'll also give one specific example.
I'm wanting to put all of the character's stats and skills into one or two lists, so I can more easily use them with functions, and I have no clue how to use a list from another file as an argument for a function. This is what I've tried, and it seems like it should work. I'm importing this file into CharacterCreation, and then just using "StatCalculations.LevelUp(PLAYERSTATLIST)", but it says "'module' object has no attribute 'LevelUp'".
What am I doing wrong?
from CharacterCreation import BaseHEALTH, BaseMANA, BaseSTAMINA, BaseCAPACITY, BaseDEFENSE, STRENGTH, ENDURANCE, AGILITY, INTELLIGENCE, PERSONALITY, BaseSTRENGTH, BaseENDURANCE, BaseAGILITY, BaseINTELLIGENCE, BasePERSONALITY, PLAYERLEVEL, PLAYERNAME
def LevelUp(*PLAYERSTATLIST):
HEALTH = BaseHEALTH + ( ENDURANCE / 2 ) + ( STRENGTH / 5 )
MANA = BaseMANA + INTELLIGENCE + ( ENDURANCE / 10 )
STAMINA = BaseSTAMINA + ( STRENGTH / 5 ) + ( ENDURANCE / 5 )
CAPACITY = BaseCAPACITY + ( STRENGTH / 5 ) + ( ENDURANCE / 10 )
DEFENSE = BaseDEFENSE + ( ENDURANCE / 10 ) + ( AGILITY / 10 ) + ( STRENGTH / 10 )
PLAYERSTATLIST = [HEALTH, MANA, STAMINA, CAPACITY, DEFENSE, BaseHEALTH, BaseMANA, BaseSTAMINA, BaseCAPACITY, BaseDEFENSE, STRENGTH, ENDURANCE, AGILITY, INTELLIGENCE, PERSONALITY, BaseSTRENGTH, BaseENDURANCE, BaseAGILITY, BaseINTELLIGENCE, BasePERSONALITY, PLAYERLEVEL, PLAYERNAME]
return PLAYERSTATLIST
2 Answers 2
here is a mini example to get you going.. you can initialize base values with something like randrange and so on. each of these classes can be put into a file and imported as a module, whatever keeps you organized. good luck
import random
class PlayerAttributes(object):
def __init__(self):
self.health = 0
self.mana = 0
self.stamina = 0
self.capacity = 0
self.defense = 0
self.reset_attributes()
def level_up(self):
self.level_health()
self.level_mana()
#etc
def reset_attributes(self):
self.reset_health()
self.reset_mana()
self.reset_stamina()
self.reset_capacity()
self.reset_defense()
def get_health(self):
return self.health
def reset_health(self):
self.health = base_health
def level_health(self):
self.health += (self.endurance/2) + (self.strength/2)
class Equipment(object):
def __init__(self):
self.equipment_dict = {}
self.currently_equipped = None
def set_equipped_weapon(self,weapon_name)
self.currently_equipped = weapon_name
def add_item(self,item):
self.equipment_dict[item.name] = item
class Item(object):
def __init__(self,name,hp,attack,defense,weight):
pass #put stuff in here, etc
class Player(object):
def __init__(self):
self.attributes = PlayerAttributes()
self.backpack = Equipment()
self.backpack.add_item(Item("dagger",5,10,0,2))
def level_up(self):
self.attributes.level_up()
Comments
So, if I could get some general advice on how to set things up efficiently, etc.
Step 1. Read the Python libraries. Pick a dozen or so that are "related". If you look at the internet protocols section, you'll find many things that are broken up until many libraries.
Step 2. Think about what you learned from reading the libraries. The more you read, the more principles will emerge.
Hint. A "module" is a sigle, reusable, file largely composed of class and function definitions.
BaseHEALTHandBaseMANAin your gigantic list of player stats. Make each player an object of thePlayerclass and store the traits as class members, so you can refer to them by name and not care about ordering variables like this.]character at the end.