Calling another pygame-zero program from a main pygame-zero window

Viewed 90

I want to create multiple small games in pygame zero, with a main window on which you have buttons to click - when clicking a button it would start a new window and a new subgame (coded on its own in another file). The secondary games should be able to return if the game was won or lost. Is this possible using pygame-zero? I am thinking the subgame should be encapsulated in some function to be able to give a return value, but I am not sure if this is doable in pygame-zero since it calls some functions itself... any idea?

Or am I better off adding some kind of game state in the main program and do everything there like this?

def update():
    if state == 'game1':
        #all of the game1 update code here
    elif state == 'game2':
        #all of the game2 update code here
    #etc
def draw():
    if state == 'game1':
        #all of the game1 draw code here
    elif state == 'game2':
        #all of the game2 draw code here
    #etc
1 Answers

I think you are definitely better off by implementing a game state. I'd use an object-oriented approach like:

class GameState:
    def update():
        pass
    def draw():
        pass

class Game1(GameState):
    def update():
        # ...
    def draw():
        # ...

class GameChooser(GameState):
    def update():
        # choose game here
        global state
        if ...:
            state = Game1()
    def draw():
        # ...

state = GameChooser()
def update():
    state.update()

def draw():
    state.draw()
Related