Is there an event handling option to minimize and maximize screen in pygame?

蹲街弑〆低调 提交于 2019-12-11 12:04:11

问题


Is there any event handler to minimize or maximize screen like there is to quit screen?

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

回答1:


Pygame adds pygame.ACTIVEEVENTs to the event queue when the window is minimized/iconified or maximized. You can check if event.gain == 1 and event.state == 6: and if event.gain == 0 and event.state == 6: to see if the window was maximized or minimized. The only problem is that event.gain == 1 and event.state == 6 is also True when the window gains input focus.

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.KEYDOWN:
            if event.key == pg.K_i:
                pg.display.iconify()
        elif event.type == pg.ACTIVEEVENT:
            if event.gain == 1 and event.state == 6:
                print('maximized')
            elif event.gain == 0 and event.state == 6:
                print('minimized')

    screen.fill(BG_COLOR)
    pg.display.flip()
    clock.tick(60)

If you want to minimize/iconify the window with a key press, you can call pygame.display.iconify().



来源:https://stackoverflow.com/questions/54049637/is-there-an-event-handling-option-to-minimize-and-maximize-screen-in-pygame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!