class - I have a issue with python classes -
ok i'm making text based rpg game have issue python classes i'm not sure how fix i've tried looking problem haven't been able find posts pertain maybe i'm looking wrong things? don't know. anyways here's issue have class in file called unit.py in folder named subfiles
class enemy(object): def __init__(self,name,hp,atk): self.name = name self.hp = hp self.atk = atk
in main file have
from subfiles.unit import * wolf = enemy("wolf", 100, 10) cow = enemy("cow", 50, 0) nmelist = [wolf,cow] def gennme(): global nme ##choose enemy fight nme = random.choice(nmelist) def attack(): nme.hp -= 10 print nme.hp attack()
when run attack definition subtracts wolf.hp should, that's not i'm trying achieve, because next time battle comes against wolf dead how can have wolf = enemy("wolf", 100, 10) set out default values , after battle on , wolf killed, next time fight wolf loads defaults. hope i'm making sense here i'm sure there's simple way achieve evading me perhaps understanding of how classes work flawed? anyways forward responses ans suggestions oh way same thing applies cow wolf.
yeah, maybe it's practice create new wolf after 1 killed. don't have alive flag, alive?
you have enemy deafult deafult hp if hp 0. in manner lets check if wolf killed. way this:
class enemy(object): def __init__(self,name,hp,atk): self.name = name self._hp = hp self.atk = atk self._deafult_hp = hp @property def hp(self): return self._hp @hp.setter def hp(self, value): if not self._hp: self._hp = self._deafult_hp self._hp = value
now in battle can check if wolfs hp 0, , in next battle wolfs hp deafult 100 hp or ever choose. ^^
Comments
Post a Comment