How to put limits on resizing a window in pygame

2019-04-16 03:22发布

问题:

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!

回答1:

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)