Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pygame fullscreen mode exit

Tags:

python

pygame

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()
like image 676
Telarius Chromack Avatar asked Nov 26 '25 12:11

Telarius Chromack


2 Answers

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.
like image 171
skrx Avatar answered Nov 28 '25 02:11

skrx


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
like image 42
touch my body Avatar answered Nov 28 '25 01:11

touch my body



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!