Pygame: correct coding sequence for minimizing and restore of window

Trying to ensure Users can minimize (a.k.a. "pause") game, and later restore with all visible updated. It isn't working for me.

My code is as follows:

####################################################################################################
###     Code Segment -- Game Interraction and Actions
####################################################################################################
while not abandonGame :

    showAgressor("Select grid (click) on targetting board ...")

    ################################################################################################
    ###     Get interrupt or user-click at grid position within defined window range
    ################################################################################################
    #bypass = 0

    for event in pygame.event.get() :
        gridClick = False
        if event.type == pygame.WINDOWMINIMIZED :
            displayRefresh.tick(2)
            for event in pygame.event.get() :
                while  event.type != pygame.QUIT :
                    if event.type == pygame.WINDOWRESTORED :
                        pygame.display.update()
                        #pygame.display.flip()

        if event.type == pygame.QUIT :
            ########################################################################################
            ###     Set flag to abandon game-play loop
            ########################################################################################
            abandonGame = True

        elif event.type == pygame.MOUSEBUTTONDOWN :
                        #######################################################

Also, after I restored, the window's (X) for closing is not responding to "QUIT". Any ideas on how to fix ?

1 Like

some help ... Pygame - Event Handling - GeeksforGeeks

I do not see event WINDOWMINIMIZED or WINDOWRESTORED

2 Likes

First, the main event processing cycle encloses another event processing cycle which refers to the same event queue. I'd strongly discourage such practice.

Second, my pygame program's window is minimized and maximized without a problem with no special handling. The main cycle code is inserted below. Please note FRAMEEVENT handling.

    pygame.init()
    screen = pygame.display.set_mode((defaults['width'], defaults['height']))
    pygame.display.set_caption(defaults['caption'])
    screen.fill(defaults['background'])
    pygame.display.flip()
    
    clock  = pygame.time.Clock()
    
    PAUSEEVENT = pygame.USEREVENT+1
    FRAMEEVENT = pygame.USEREVENT+2
    
    pygame.time.set_timer(PAUSEEVENT, defaults['timeout'])
    pygame.time.set_timer(FRAMEEVENT, 1000 // defaults['fps'])
    
    pause = False

while True:
        if not pause:
            try:
                pixel = next(pixels)
            except StopIteration:
                pygame.time.set_timer(PAUSEEVENT, 0)
                pause = True
                print('Paused. No more pixel data available.')
                
            screen.set_at((pixel[0], pixel[1]), pixel[2])
            
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print( 'Shutting down.')
                pygame.quit()
                return
            elif event.type == FRAMEEVENT:
                pygame.display.flip()
                pygame.time.set_timer(FRAMEEVENT, 1000 // defaults['fps'])
            elif event.type == PAUSEEVENT:
                pygame.time.set_timer(PAUSEEVENT, 0)
                print( 'Paused due to timeout')
                pause = True
            elif event.type == pygame.KEYDOWN:
                keys_pressed = pygame.key.get_pressed()
                if keys_pressed[pygame.K_SPACE]:
                    if pause:
                        # restart the computation
                        print( 'Pause cleared')
                        pause = False
                        pygame.time.set_timer(PAUSEEVENT, defaults['timeout'])
                    else: 
                        # shut down computation
                        pygame.time.set_timer(PAUSEEVENT, 0) # cancel pause timer
                        print( 'Pause invoked')
                        pause = True
                elif keys_pressed[pygame.K_RETURN] or keys_pressed[pygame.K_KP_ENTER]:
                    fname = defaults['caption'] + ' ' + datetime.datetime.today().isoformat("_").replace(':','-')[:19] + '.png'
                    print( 'Saving image into ' + fname)
                    pygame.image.save(screen, fname)
                elif keys_pressed[pygame.K_ESCAPE]:
                    print( 'Shutting down.')
                    pygame.quit()
                    return
                else:
                    pass

2 Likes

Thank you, Pavlos. That is very useful.

As for the WINDOWMINIMIZED and WINDOWRESTORED events, they were documented here:

pygame.event — pygame v2.6.0 documentation

I was wondering about that. :slight_smile:

Thank you also for that code segment. I will study that alongside the reference I got from Pavlos.

Definitely this. Set (or check) a variable that represents a "state" if you want the same event to perform different actions when the program is in different states (e.g. window minimised vs not).

1 Like