Multiple Displays in Pygame

Do you really need multiple windows? I mean, do you really need them?

If yes, then you should probably use pyglet/cocos2d instead.

To have multiple windows in pygame, you need multiple processes (one for each window). While this is doable, it’s not worth the efford. You’ll need IPC to exchange data between the windows, and I guess your code will become error-prone and ugly.

Go with pyglet when you need more than one window.

The better solution is probably to divide your game into scenes. Create multiple scenes so that each one represent one stage of the game, something like MenuScene, MainScene, BattleScene, GameOverScene, OptionScene etc.

Then let each of those scenes handle input/drawing of that very part of the game.

  • MenuScene handles drawing and input etc. of the game’s menu
  • MainScene handles drawing and input etc. of the running game
  • BattleScene handles drawing and input etc. of whatever you do in run_ani

In your mainloop, just pass control over to the current scene by implementing the methods draw(), handle_event(), and update().

Some example code to get the idea:

scenes = {'Main': MainScene(),
          'Battle': BattleScene()} #etc

scene = scenes['Main']

class MainScene():
  ...
  def handle_event(self, event):
    if event.type == KEYUP:
      if event.key == K_a:
        scene = scenes['Battle']
  ...

class BattleScene():
  ...
  def draw(self):
    # draw your animation

  def update(self):
    # if animation is over:
    scene = scenes['Main']

...

def main_game():
  ending=False
  While Not ending:
      clock.tick(30)
      for event in pygame.event.get():
        scene.handle_event(event)
        scene.update()
        scene.draw()

This is an easy way to cleanly seperate the game logic and allow context switching.

Leave a Comment