mardi 18 avril 2017

Working on classes for a text based game in python

I'm like 3 weeks into coding and python. (since that's what i decided to start with) I'm not sure if i understand how classes work entirely and if i'm making these right. To my understanding everything i have put under player: (upperStrengthName) is unchangeable and permanent. Then as far as def init(self,x, y, z) everything here x, y, z are arguement (which i'm not sure what that means so to my understanding everything here is created as soon as the class is called. meaning when a player (class) is called it will create it's self, x, y, z. Then i would add under the init statement the self.x = x, self.y = y, self.z = z or if i wanted x to always be 10 i would put self.x = 10. putting in the self statements lets the program know i'm talking about player x not a global x variable. so doing this in the parent class lets all sub classes have the self statement resulting in less coding. i feel like this is the case but not sure if i'm 100% correct.

next you see i have some self.maxHp = maxHpCalc() and below that a def mxHpcalc(): and return (formula) i'm assuming anytime i wish to recalculate maxHp i could put in player.maxHpCalc() which would update max for anytime a stat is raised or something like that? i'm not sure if the (formula) needs to have self.x, or just have x.

the last is def attack and the action class attack(action) i think i have it right, but i haven't been able to check it "in game" since the maxHpCalac and what not seems to be failing.

i apolgize for the long code and probably could have removed some and wrote this easier to read, first post on here.

class player:
    upperStrengthName = "upper strength"
    upperStrengthSubStatsNames = ["might", "brawn"]
    lowerStrengthName = "lower strength"
    lowerStrengthSubStatsNames = ["endurance", "posture"]
    upperDexterityName = "upper dexterity"
    upperDexteritySubStatsNames = ["accracy", "agility"]
    lowerDexterityName = "lower dexterity"
    lowerDexteritySubStatsNames = ["reflex", "speed"]
    magicName = "magic"
    magicSubStatsNames = ["psychic", "elemental", "holy", "black"]
    physicalDefenseName = "physical defense"
    physicalDefenseSubStatsNames = ["blunt", "slash", "pierce"]
    wisdomName = "wisdom"
    wisdomSubStatsNames = ["psychic wisdom", "elemental wisdom"]
    willpowerName = "willpower"
    willpowerSubStatsNames = ["holy willpower", "black willpower"]
    knowledgeName = "knowledge"
    knowledgeSubStatsNames = ["combat", "learning", "insight"]

#assume everything is indented correctly class wasn't originally indented so had to
# shift upper section to show up.

def __init__(self, name, exp, hp, mp, upperStrength, lowerStrength, upperDexterity, lowerDexterity, magic , physicalDefense , wisdom, willpower, knowledge, lvl, \
             upperStrengthExp, lowerStrengthExp, upperDexterityExp, lowerDexterityExp, magicExp , physicalDefenseExp , wisdomExp, willpowerExp, knowledgeExp, \
             upperStrengthSubStatsGain, lowerStrengthSubStatsGain, upperDexteritySubStatsGain, lowerDexteritySubStatsGain, magicSubStatsGain, physicalDefenseSubStatsGain, wisdomSubStatsGain, willpowerSubStatsGain, knowledgeSubStatsGain, \
             upperStrengthSubStats, lowerStrengthSubStats, upperDexteritySubStats, lowerDexteritySubStats, magicSubStats, physicalDefenseSubStats, wisdomSubStats, willpowerSubStats, knowledgeSubStats, inventory):
    self.name = name
    self.exp = exp
    self.hp = hp
    self.mp = mp
    self.weaponType = weaponType
    self.weaponDamage = weaponDamage
    self.armor = armor
    self.upperStrengthExp = upperStrengthExp
    self.lowerStrengthExp = lowerStrengthExp
    self.upperDexterityExp = upperDexterityExp
    self.lowerDexterityExp = lowerDexterityExp
    self.magicExp = magicExp
    self.physicalDefenseExp = physicalDefenseExp
    self.wisdomExp = wisdomExp
    self.willpowerExp = willpowerExp
    self.knowledgeExp = knowledgeExp
    self.upperStrengthSubStatsGain = upperStrengthSubStatsGain
    self.lowerStrengthSubStatsGain = lowerStrengthSubStatsGain
    self.upperDexteritySubStatsGain = upperDexteritySubStatsGain
    self.lowerDexteritySubStatsGain = lowerDexteritySubStatsGain
    self.magicSubStatsGain = magicSubStatsGain
    self.physicalDefenseSubStatsGain = physicalDefenseSubStatsGain
    self.wisdomSubStatsGain = wisdomSubStatsGain
    self.willpowerSubStatsGain = willpowerSubStatsGain
    self.knowledgeSubStatsGain = knowledgeSubStatsGain
    self.upperStrength = upperStrength
    self.lowerStrength = lowerStrength
    self.upperDexterity = upperDexterity
    self.lowerDexterity = lowerDexterity
    self.magic = magic
    self.physicalDefense = physicalDefense
    self.wisdom = wisdom
    self.willpower = willpower
    self.knowledge = knowledge
    self.lvl = lvlCalc()
    self.upperStrengthSubStats = upperStrengthSubStats 
    self.lowerStrengthSubStats = lowerStrengthSubStats
    self.upperDexteritySubStats = upperDexteritySubStats 
    self.lowerDexteritySubStats = lowerDexteritySubStats 
    self.magicSubStats = magicSubStats 
    self.physicalDefenseSubStats = physicalDefenseSubStats
    self.wisdomSubStats = wisdomSubStats
    self.willpowerSubStats = willpowerSubStats 
    self.knowledgeSubStats = knowledgeSubStats
    self.maxHp = maxHpCalc()
    self.accuracy = accuracy()
    self.dodge = dodge()
    self.percentChanceToHit = percentChanceToHit()
    self.damage = damage()
    self.defenseBlunt = defenseCalcBlunt()
    self.defenseSlash = defenseCalcSlash()
    self.defensePierce = defenseCalcPierce()
    self.damageDelt = damageDelt()
    self.inventory = inventory

def isAlive(self):
    return self.hp > 0

def lvlCalc(self):
    return (self.upperStrength + self.lowerStrength + self.upperDexterity + self.lowerDexterity + self.magic + self.physicalDefense + self.wisdom + self.willpower + self.knowledge)/5

def maxHpCalc(self):
    return (self.upperStrength*2) + (self.upperStrengthSubStats[0]*1) + (self.upperStrengthSubStats[1]*1) + (self.lowerStrength*3) + (self.lowerStrengthSubStats[0]*5) + (self.lowerStrengthSubStats[1]*3) + (self.upperDexterity*1) + (self.upperDexteritySubStats[0]*1) + (self.upperDexteritySubStats[1]*1) + (self.lowerDexterity*1) + (self.lowerDexteritySubStats[0]*1) + (self.lowerDexteritySubStats[1]*1) + (self.physicalDefense*6) + (self.physicalDefenseSubStats[0]*2) + (self.physicalDefenseSubStats[1]*3) + (self.physicalDefenseSubStats[2]*4)

def accuracy(self):
    return (self.upperStrength*5) + (self.lowerStrength*5) + (self.upperDexterity*4) + (self.upperDexteritySubStats[0]*7) + (self.upperDexteritySubStats[1]*4) + (self.knowledge*2) + (self.knowledgeSubStats[0]*3) + (self.knowledgeSubStats[2]*2)

def dodge(self):
    return (self.lowerDexterity*4) + (self.lowerDexteritySubStats[0]*6) + (self.lowerDexteritySubStats[1]*4) + (self.knowledge*2) + (self.knowledgeSubStats[0]*3)

def percentChanceToHit(self):
    return self.accrucy/((random.randint((self.accracy*.9),(self.accracy))+(random.randint(enemy.dodge*.9),(enemy.dodge*1.1))))

def damage(self):
    return (self.upperStrength*5) + (self.upperStrengthSubStats[0]*4) + (self.upperStrengthSubStats[1]*3) + (lowerStrength*2) + (self.lowerStrengthSubStats[0]*1) + (self.lowerStrengthSubStats[1]*1) + (self.knowledgeSubStats[0]*2) + self.weaponDamage

def defenseCalcBlunt(self):
    return (self.physicalDefense*4) + (self.physicalDefenseSubStats[0]*7) + self.Armor

def defenseCalcSlash(self):
    return (self.physicalDefense*2) + (self.physicalDefenseSubStats[1]*6) + self.Armor

def defenseCalcPierce(self):
    return (self.physicalDefense*2) + (self.physicalDefenseSubStats[2]*5) + self.Armor

def damageDelt(self,enemy):
    return random.randint(self.damage*.8,self.damage*1.2) * (self.damage/(self.damage + enemy.defense))

def printInventory(self):
    for item in self.inventory:
        print(item,'\n')

def attack(self, enemy):

    if randint(1,100) <= self.percentChanceToHit
        damageDelt(self,enemy)
        print("You {} {} for {} damage.".format(self.weaponType, enemy.name, self.damageDelt))
        enemy.hp -= self.damageDelt

        if enemy.isAlive():
        pass

        else:
            print("You have Slain {} and gained {} exp!".format(enemy.name, enemy.expGiven))

    else:
        print("you miss {} with your {}.".format(enemy.name, self.equipedWeapon))

class attack(action): def init(self, enemy): super().init(method=PlayerAttack, name="Attack", hotkey='a', enemy=enemy)






Comments
0 Comments

0 commentaires:

Enregistrer un commentaire