Pygame with Multiple Windows

2019-07-03 13:13发布

问题:

I need to to build an application that has multiple windows. In one of these windows, I need to be able to play a simple game and another window has to display questions and get response from a user that influences the game.

(1) I was wanting to use pygame in order to make the game. Is there a simple way to have pygame operate with multiple windows?

(2) If there is no easy way to solve (1), is there a simple way to use some other python GUI structure that would allow for me to run pygame and another window simultaneously?

回答1:

The short answer is no, creating two pygame windows in the same process is not possible. If you want to run two windows with one process, you should look into pyglet or cocos2d.

An alternative, if you must use pygame, is to use inter-process communication. You can have two processes, each with a window. They will relay messages to each other using sockets. If you want to go this route, check out the socket tutorial here.



回答2:

Internally set_mode() probably sets a pointer that represents the memory of a unique display. So if we write:

screenA = pygame.display.set_mode((500,480), 0, 32)
screenB = pygame.display.set_mode((500,480), 0, 32)

For instance we can do something like that later:

screenA.blit(background, (0,0))
screenB.blit(player, (100,100))

both blit() calls will blit on the same surface. screenA and screenB are pointing to the same memory address. Working with 2 windows is quite hard to achieve in pygame.