I am just learning python and I have been following a book for making a text RPG. I've made my way through the book and now I'm attempting to add experience and levelling starting with something really small. Right now I have enemies providing a random amount of experience using the random.randint() function and I can get that to display just fine, but I can't seem to get that to translate to the player class and then into the level up function.
def level_up(self):
self.xp = self.xp + enemy.xp
while self.xp >= self.lvlNext:
self.lvl += 1
self.xp = self.xp - self.lvlNext
self.lvlNext = round(self.lvlNext * 1.5)
print("Congatulations your level {}".format(self.lvl))
This is the function I'm attempting to use. It doesn't seem to be getting the input from enemy.xp I'm also new to stack overflow - if I can post the whole code file I will.
github link: https://github.com/GusRobins60/pythonTextRPG
thanks for any help
Here is the signature you're currently using:
def level_up(self):
You complain that
It doesn't seem to be getting the input from enemy.xp.
You want to pass in enemy
as a parameter:
def level_up(self, enemy):
As an entirely separate matter, it is fine to say self.xp = self.xp + enemy.xp
, but it would be preferable to state it more concisely:
self.xp += enemy.xp
and similarly for
self.xp -= self.lvlNext
EDIT
Perhaps you'd care to derive enemy
in this way:
room = world.tile_at(self.x, self.y)
enemy = room.enemy
It would be worth packaging that up as a helper function:
def get_enemy(x, y):
room = world.tile_at(x, y)
return room.enemy
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.