2

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
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
ka1rn
  • 21
  • 1
  • [Pygame Zero](https://pygame-zero.readthedocs.io/en/stable/) is not [PyGame](https://www.pygame.org/news). You have to use the [tag:pgzero] tag instead of the [tag:pygame] tag. – Rabbid76 Oct 16 '21 at 16:06

1 Answers1

1

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()
r0the
  • 617
  • 4
  • 13