Is there a way to completely prevent the user form exiting the pygame window?

Viewed 111

I want to completely prevent the user from closing the Pygame window except for the key x. Currently, I'm able to prevent the user from closing it, but I am unable to prevent the user from opening another window that overlaps it (press windows key -> open chrome, which overlaps the Pygame window).

import sys
import pygame
from pygame.locals import *
pygame.init()
infoObject = pygame.display.Info()
screen = pygame.display.set_mode((infoObject.current_w, infoObject.current_h), flags=pygame.NOFRAME)
while True:
    screen.fill((0, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_x:
                pygame.quit()
                sys.exit()
        if event.type == QUIT:
            pass
    pygame.display.flip()
2 Answers

We don't have complete solutions for this task.

You can make the title var vanish away by setting display mode to NOFRAME (You have already used this): pygame.display.set_mode(flags = pygame.NOFRAME), but this is overkill for just stopping close buttons

Although any user with minimum Computer Knowledge will fire up a Task manager and kill python.exe :-(

There are ways of disabling task manager through Registry in Windows, Image File Execution Options blah blah, but your game script will barely run with Administrator priviliges (With nagging of UAC)

In order to do this, you can add this piece of code to your pygame.display.set_mode(): pygame.display.set_mode(..., flags=pygame.NOFRAME). Now, the bar with the "x" button will disapear.

Notice that if you decide to make this change, you won't be able to move your game window anymore. (Unless you change it back).

Related