如何限制在 pygame 中调整窗口大小 [英] How to put limits on resizing a window in pygame

查看:170
本文介绍了如何限制在 pygame 中调整窗口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 pygame 中设置了一个窗口,如下所示:screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT),pygame.RESIZABLE)

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

如您所见,它可以调整大小,并且该方面工作得很好,但是如果它太小,则您无法看到所有内容,因此我想设置一个限制,例如,您可以不调整屏幕大小使其宽度小于 600,或高度小于 400,有没有办法在 pygame 中做到这一点?

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?

谢谢!

推荐答案

您可以使用 pygame.VIDEORESIZE 事件来检查调整大小时的新窗口大小.您要做的是在事件中检查新的窗口大小值,根据您的限制更正它们,然后使用这些值重新创建屏幕对象.

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.

这是一个基本的脚本:

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)

根据您的游戏图形的绘制方式,您可能希望根据窗口调整大小来调整它们的大小(尚未对此进行测试,只需按照以下示例进行操作:http://www.pygame.org/wiki/WindowResizing)

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)

这篇关于如何限制在 pygame 中调整窗口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆