Class Enemy Has Function combat_roll() but can't seem to access it Python 2.7 -


i never bothered learn classes in python because docs find bit technical me. so, yet again have sat down try conquer them. love able use classes text based games make, how try every time.

my class code (its messy because have been trying can think of) is

class enemy:     def __init__(self, name, weapon, hp, attack, defense, xp):         self.self = self         self.name = name         self.hp = hp         self.attack = attack         self.defense = defense         self.xp = xp          #self.attack_text1 = attack_text1         self.attack_text1 = name, " attacks ", weapon, "."         #self.attack_damage = attack_damage         self.attack_damage = random.randint(0,2) + attack         #self.total_damage = total_damage         self.total_damage = self.attack_damage - player.defense         if self.total_damage < 1:             self.total_damage = 0          #self.attack_text2 = attack_text2         self.attack_text2 = name, " deals ", self.total_damage, " you."           @staticmethod #tried solution found on stack overflow         def combat_roll(self):             self.roll = random.randrange(0,20) + attack          combat_roll(self) 

i have function called combat() pits player against instance of enemy class.

the instance called in function:

def combat():     goblin_weapon = weapon("goblin dagger", 1, 5)     goblin = enemy("ugly goblin", goblin_weapon, 3,2,1,150)      goblin_damage = goblin.attack - player.defense     print "a ", goblin.name, " attacks ", goblin_weapon, "."     print goblin.combat_roll     if goblin.roll > player.defense:         print goblin.name, "does ", goblin_damage, " damage."     else:         print goblin.name, "misses." 

my goal of d20 feel combat. attack bonus + d20 rolls vs armor class. hoping enemy class have own function handle own dice rolls.

i've lost original state of code. have checked google , searched here solutions none of them worked. sure because haven't seen solution in way works brain, because can't complicated. why can't call function reference class variables?

enemy.combat_roll() 

you know?

anyway, help!

you're declaring method within __init__, instead of within enemy. understanding, available if declare within enemy class itself.

class enemy:     def __init__(self, name, weapon, hp, attack, defense, xp):         self.self = self         self.name = name         self.hp = hp         self.attack = attack         self.defense = defense         self.xp = xp          #self.attack_text1 = attack_text1         self.attack_text1 = name, " attacks ", weapon, "."         #self.attack_damage = attack_damage         self.attack_damage = random.randint(0,2) + attack         #self.total_damage = total_damage         self.total_damage = self.attack_damage - player.defense         if self.total_damage < 1:             self.total_damage = 0          #self.attack_text2 = attack_text2         self.attack_text2 = name, " deals ", self.total_damage, " you."          combat_roll(self)      def combat_roll(self):         self.roll = random.randrange(0,20) + attack 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -