Hi guys! Hope you all have a wonderful Thanksgiving!
I have run into 2 new problems while working on ex45:
The first one is an inheritance problem. I want to create a “user” instance at the start of my game, and create a “monster” instance every time I have to. So I have the following code (only part of my code that is relevant):
class Scene(object):
def __init__(self, user):
self.user = user
self.monster = MonGen()
# generate a Monster instance
class LaserWeaponArmory(Scene):
def print_func(self):
print("LaserWeaponArmory")
self.user.view_stat()
self.monster.view_stat()
# 2 view_stat() functions that I used to check stats of the user/monster,
# they are under my other python file that already being imported
Combat(self.user, self.monster)
# again, Combat is also being imported beforehand
class Map(object):
map_num = [
[ 0, 1, 2, 3],
[10, 11, 12, 13],
[20, 21, 22, 23],
[30, 31, 32, 33]
]
def __init__(self, a, b):
self.a = a
self.b = b
self.start_num = self.map_num[a][b]
self.user = UserGen()
# generate a User instance at the beginning
self.scene = Scene(self.user)
# pass self.user to parent class Scene
self.scenes = {
1: LaserWeaponArmory(self.scene),
}
However, I got the following error when it tries to call
self.user.view_stat()
AttributeError: ‘Scene’ object has no attribute ‘view_stat’
My thinking behind the code is that when I create a Map instance, the UserGen() will be called and generate a user only once at the beginning, then I can pass the user to my parent class Scene(object), and I call MonGen() under the parent Scene class so every time I enter a new child class scene(i.e LaserWeaponArmory), a new monster can be generated (MonGen() will be inherited and called), and the user will be inherited to the child scene to fight the monster.
My other parts of the code works perfectly, so I know there must be something wrong happened to my inheritance, but I don’t know which part goes wrong.
The second one is an init() problem.
I want to do something like this to make my init looks simple:
__init__(self):
self.a = a
b, c, d = function_b()
self.b = b
self.c = c
self.d = d
function_b(self):
blah blah
return b, c, d
However, error message tells me function_b is not defined. I’m wondering is it possible to call a function in init if the function is below init() function? I know I have to put init at the top of a class. Is there a way to achieve my goal here?