How do I start a pygame app in maximized mode on Linux?

Viewed 41

Before I start, this is not a duplicate of How do you start Pygame window maximized? I am on Linux which the win32gui and win32con libraries do not run on.

What I'm trying to do as stated by the title is maximize my pygame window when it is run. I do not want the game to be fullscreen by using the pygame.FULLSCREEN tag in pygame.display.set_mode(). I want it to be maximized.

My current way of getting it maximized is by creating the window with the pygame.RESIZEABLE flag which allows me to get the pygame.VIDEOEXPOSE event as shown in the example below.

import pygame, sys

SCREEN_WIDTH,SCREEN_HEIGHT = 1280,720    
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.RESIZABLE, 32, vsync=1)
objects = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
        
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.VIDEORESIZE:
            SCREEN.blit(pygame.transform.scale(objects, event.dict['size']), (0, 0))
            pygame.display.update()
        elif event.type == pygame.VIDEOEXPOSE:  # handles window minimising/maximising
            SCREEN.fill((0, 0, 0))
            SCREEN.blit(pygame.transform.scale(objects, SCREEN.get_size()), (0, 0))
            pygame.display.update()
    pygame.display.update()

The objects surface is simply a surface I draw my game objects to in order to scale them when the window is resized. If you're wondering why I'm not just setting the screen width and height to the size of the screen by getting it using system info is because that way my objects aren't scaled properly. The only way I've been able to get my objects scaled properly is by using the VIDEORESIZE and VIDEOEXPOSE events. If there is a way to automatically press the maximize button or just start maximized in pygame that would be great.

1 Answers

Just found this issue on the pygame github for anyone who needs this. Essentially just use the library that pygame uses to draw windows which is cross platform.

This is the code snippet on the issue which worked for me.

import sys
import pygame
import pygame._sdl2
import time
 
pygame.init()
screen = pygame.display.set_mode((400, 400), pygame.RESIZABLE)
window = pygame._sdl2.Window.from_display_module()
clock = pygame.time.Clock()

window.maximize()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
     
    screen.fill((255, 255, 255))
     
    pygame.display.flip()
    clock.tick(30)

I hope this helps anyone having similar issues.

Related