Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Displays in Pygame

I'm making a little game and I want to make another window separately from my main one.

I have the the main game in a main window, and I want to open a new window and do a little animation when the user does something.

In my example code below, when the user presses "a" I want it to open a new window and blit to there.

Here I set up the two windows: (I know this doesnt work, its what I'm asking how to do)

SCREEN_X = 400
SCREEN_Y = 400
BSCREEN_X = 240
BSCREEN_Y = 160

BATTLE_SCENE = pygame.display.set_mode((BSCREEN_X, BSCREEN_Y))
SCREEN = pygame.display.set_mode((SCREEN_X, SCREEN_Y))

and then the program:

def run_ani ():
    #Do animation, blitting to BATTLE_SCENE
    return

def main_game():
    ending=False
    while ending==False:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT: ending=True
            if event.type == KEYDOWN: # key down or up?
                if event.key == K_ESCAPE:
                    ending=True # Time to leave
                    print("Stopped Early by user")
                elif event.key == K_a:
                    run_ani()
        #Normal screen motion, blitting to SCREEN
        if ending: pygame.quit()
    return

So far what this does is draws the main screen, then when A is pressed, it stops drawing the main screen animations, but still draws the other animations on the main screen and draws in the top left corner.

I'm pretty sure it does this because I am setting BATTLE_SCENE to be smaller than the main screen, thus when blitting to BATTLE_SCENE it blits to the area I created (240x160) in the top corner of the main screen.

However I want BATTLE_SCENE to be a seperate window, so that when I press 'a' it will pop up, do its thing, then close or at least go behind the main screen.

How to do this? Is it even possible?

like image 450
hammythepig Avatar asked Jun 19 '12 17:06

hammythepig


People also ask

How do I set up a second screen in pygame?

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.

Can you have multiple windows in pygame?

The short answer is no, you cannot open two Pygame windows in the same process. Look into pyglet or cocos2d if you want to run two windows with one process. If you must use pygame, another option is to employ inter-process communication.

What does the Blit () function do in pygame?

blit() — blit stands for Block Transfer—and it's going to copy the contents of one Surface onto another Surface . 00:17 The two surfaces in question are the screen that you created and the new Surface . So, . blit() will take that rectangular Surface and put it on top of the screen.


1 Answers

Yes, that is possible. SDL2 is able to open multiple windows. In the example folder you can take a look at "video.py".

https://github.com/pygame/pygame/blob/main/examples/video.py

"This example requires pygame 2 and SDL2. _sdl2 is experimental and will change."

like image 51
Hills Avatar answered Sep 28 '22 06:09

Hills