pygame fullscreen mode exit

浪尽此生 提交于 2019-12-24 01:55:25

问题


I'm running this little program that loads images to the screen in fullscreen mode but once it loads the program will not exit by any key combination and I end up having to reset the computer to complete whatever else I was doing.

import pygame

pygame.init()
WIDTH=1366; HEIGHT=768
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
pygame.display.set_caption('Katso')
PENGUIN = pygame.image.load("assets/download.png")
MICKEY = pygame.image.load("assets/mickey.jpg")
ADV = pygame.image.load("assets/adv.jpg")
CAT = pygame.image.load("assets/cat.jpg")
FLV = pygame.image.load("assets/flavours.jpg")
HALL = pygame.image.load("assets/hallway.jpg")
x = 0; # x coordnate of image
y = 0; # y coordinate of image
screen.blit(PENGUIN,(x,y)); pygame.display.update()
running = True
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.QUIT()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_1:
            screen.blit(MICKEY,(x,y)); pygame.display.update()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_2:
            screen.blit(PENGUIN,(x,y)); pygame.display.update()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_3:
            screen.blit(ADV,(x,y)); pygame.display.update()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_4:
            screen.blit(FLV,(x,y)); pygame.display.update()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_5:
            screen.blit(CAT,(x,y)); pygame.display.update()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_6:
            screen.blit(HALL,(x,y));pygame.display.update()
pygame.QUIT()

回答1:


pygame.QUIT is a constant value (an integer) that is used to check the type of an event. To uninitialize pygame, you have to call pygame.quit() (lowercase), but that actually doesn't quit your game but only uninitializes the loaded pygame modules. I think it's only needed to close the window if you've started your game in a tkinter based editor like IDLE.

To quit a game you can just break out of the while loop and let Python end the program as usual, or you can call sys.exit().

Since you can't click on the 'x' button to close the window in fullscreen mode, you have to use a key like Esc.

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False  # Set running to False to end the while loop.



回答2:


There's a boolean, running, that's not being used.

Instead of this:

running = True
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.QUIT()

...consider doing this:

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False


来源:https://stackoverflow.com/questions/49592133/pygame-fullscreen-mode-exit

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