2

I have a window in pygame set up like this: screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT),pygame.RESIZABLE)

As you can see, it is resizable, and that aspect is working perfectly, but if it is too small, then you can not see everything, and so I would like to set up a limit, of for example, you can not resize the screen to have a width os less then 600, or a height of less then 400, is there a way to do that in pygame?

Thank you!

rp.beltran
  • 2,764
  • 3
  • 21
  • 29

1 Answers1

7

You can use the pygame.VIDEORESIZE event to check the new windows size on a resize. What you do is on the event, you check the new windows size values, correct them according to your limits and then recreate the screen object with those values.

Here is a basic script:

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,480), HWSURFACE|DOUBLEBUF|RESIZABLE)
while True:
    pygame.event.pump()
    event = pygame.event.wait()
    if event.type == QUIT: pygame.display.quit()
    else if event.type == VIDEORESIZE:
        width, height = event.size
        if width < 600:
            width = 600
        if height < 400:
            height = 400
        screen = pygame.display.set_mode((width,height), HWSURFACE|DOUBLEBUF|RESIZABLE)

EDIT: Depending on how your game graphics are drawn, you may want to resize them according to the windows resize (haven't tested that, just going after this example: http://www.pygame.org/wiki/WindowResizing)

  • Thank you! You answer works perfectly. My graphics are resized based on screen size, but it is fine because as it turns out, I use the width/height variables to do it, and those remain accurate. Thanks again, I will mark as correct. – rp.beltran Aug 19 '13 at 00:36
  • 1
    Odd, I tried this (after fixing a couple syntax errors) and it has no effect on stopping the window resizing. – Glenn J. Schworak Oct 03 '18 at 02:13
  • This doesn't work in pygame 2 as it automatically resizes the window in pygame 2: https://www.pygame.org/wiki/WindowResizing#In%20Pygame%202 – MarcellPerger Jun 16 '23 at 17:35