How to inherit two different classes (python)

I am having some trouble putting in two seperate class handlers. I want to be able to handle two seperate classes, one for the game itself. and one to just put in a player.

** Edit: I am not looking to inherit from ShowBase, I am looking for a seperate object **

from direct.showbase.ShowBase import ShowBase
from panda3d.core import *



class MyGame(ShowBase):
    def __init__(self):
        super().__init__()
        p = player()

        self.box = self.loader.loadModel("box")
        self.box.reparentTo(self.render)
        self.box.setPos(-10,-10,-5)
        self.box.setScale(30,30,0)

        self.cam.setPos(0, -100, 0)
        #self.key = self.accept("a", self.move)
        #self.key2 = self.accept("d", self.move2)
        #keymanage = self.messenger.toggleVerbose()
        self.forw = KeyboardButton.ascii_key("w")
        self.bac = KeyboardButton.ascii_key("s")
        print(self.cam.getPos()[1])
        self.taskMgr.add(self.check, "test")

    def check(self, task):
        isdown = self.mouseWatcherNode.is_button_down
        if isdown(self.forw):
            self.move()
        if isdown(self.bac):
            self.move2()
        return task.cont

    def move(self):
        x_move = self.cam.getPos()[1] + 10
        self.cam.setPos(0, x_move, 0)
    def move2(self):
        x_move = self.cam.getPos()[1] - 10
        self.cam.setPos(0, x_move, 0)

class player(MyGame):
    def __init__(self):
        MyGame.__init__(self)
        self.panda = self.loader.loadModel("panda")
        self.panda.reparentTo(self.render)
game = MyGame()

game.run()

The error in question.

Exception: Attempt to spawn multiple ShowBase instances!

Simply put, ShowBase is intended to exist only once per program, and thus having two objects that inherit from ShowBase causes a problem.

To be clear, the problem here isn’t that you have two classes, simply that you’re ending up with two instances of (something that inherits from) the “ShowBase” class, specifically. This structure could potentially be functional if done with a class other than ShowBase.

However, I suppose that my question is this: Why does the “player” class inherit from the “MyGame” class? Why is a “player” considered to be a “MyGame”, or a “ShowBase”, for that matter?

I can see that is definitely the case with have two ShowBases. I am wondering how i can create seperate objects without even inheriting ShowBases.

In general, there’s not much reason (that I see, at least) to inherit from ShowBase in anything other than your primary “game” class. (And there are differing approaches even with regards to that!)

So, for example, you might have something like this:

class MyGame(ShowBase):
    def __init__(self):
        super().__init__()
        p = Player(self)
        # Note above that we pass in a reference to our
        # game-class, so that the player-class has access to
        # it for the purposes of model-loading, etc.
        # See below for the implementation of this...

        # etc., as in your code above

class Player():
    def __init__(self, base):
        self.panda = base.loader.loadModel("panda")
        self.panda.reparentTo(base.render)
# Note that the "Player" class isn't derived from anything!
# That said, we could derive it from some other
# (non-ShowBase) class if we wanted to do so. For example,
# we might define a class that represents objects in the game,
# and derive it from that.

game = MyGame()
game.run()

Now, personally I’m not a huge fan of passing a reference to the ShowBase-derived object around to everything–while perfectly fine in a small example like the above, I feel that it can get unwieldy in more-complex projects.

For such cases, I tend to prefer having the “game” class in its own file, separate from others, and a “common” file that stores a reference to it and which other files can then import in order to gain access to it.