Pages

sâmbătă, 6 iulie 2019

PyGame : Using the fullscreen with PyGame.

In this tutorial, I will show you how to use the fullscreen feature with pygame python module.
The scrip I used uses two keys: F12 for fullscreen and ESC for quit to windows.
The script starts with the default initialization of the PyGame python module.
The open the window in fullscreen with a resolution of 1024x768.
Using the while True condition you can change it into window mode with a resolution of 640x480 by pressing the F12 key.
In this loop is set the key ESC to quit the application.
import pygame,sys
pygame.init()
#set the fullscreen - my display is 1024 by 576
screen = pygame.display.set_mode((1024, 576),pygame.FULLSCREEN,32) 
pygame.display.set_caption("Example fullscreen")
cursor_x,cursor_y = 0,0
cmddown = False
fullscreen = True
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            #Toggle Fullscreen (press escape to exit/enter fullscreen)
            if event.key == pygame.K_F12:
                if fullscreen == True:
                    #exits fullscreen
                    screen = pygame.display.set_mode((640, 480)) 
                    pygame.display.set_caption("Example window 640x480")
                    fullscreen = False
                else:
                    screen = pygame.display.set_mode((1024, 576),pygame.FULLSCREEN,32)
                    pygame.display.set_caption("Example fullscreen 1024x768")
                    fullscreen = True
            if event.key == pygame.K_ESCAPE:
                if cmddown == True:
                    pygame.quit()
                    sys.exit()
        elif event.type == pygame.KEYUP:
            cmddown = False
    screen.fill((0,0,0))
    pygame.display.flip()