如何在 PyGame 中最大化显示屏幕?

How do I maximize the display screen in PyGame?

我是 Python 编程新手,最近开始使用 PyGame 模块。下面是一段简单的代码来初始化显示屏幕。我的问题是:目前,最大化按钮被禁用,我无法调整屏幕大小。如何让它在全屏和返回之间切换? 谢谢

import pygame, sys
from pygame.locals import *

pygame.init()

#Create a displace surface object
DISPLAYSURF = pygame.display.set_mode((400, 300))

mainLoop = True

while mainLoop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainLoop = False
    pygame.display.update()

pygame.quit()

您要找的方法是pygame.display.toggle_fullscreen

或者,按照指南在大多数情况下的建议,调用 pygame.display.set_mode() with the FULLSCREEN tag

在你的情况下,这看起来像

DISPLAYSURF = pygame.display.set_mode((400, 300), pygame.FULLSCREEN)

(请使用 pygame.FULLSCREEN 而不是 FULLSCREEN 因为在使用我自己的系统进行测试时 FULLSCREEN 只是最大化了 window 而没有适合分辨率,而 pygame.FULLSCREEN 适合我的分辨率以及最大化。)

要以原始分辨率全屏显示,请执行

DISPLAYSURF = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

要使 window 可调整大小,请在设置模式时添加 pygame.RESIZABLE 参数。您可以多次设置屏幕表面的模式,但您可能必须先执行 pygame.display.quit() 然后再执行 pygame.display.init()

您还应在此处查看 pygame 文档 http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode

您必须像这样将全屏参数添加到显示声明中:

import pygame, sys
from pygame.locals import *

pygame.init()

#Create a displace surface object
DISPLAYSURF = pygame.display.set_mode((400, 300), FULLSCREEN)

mainLoop = True

while mainLoop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainLoop = False
    pygame.display.update()

pygame.quit()

这将使您可以从最大化切换到初始大小

import pygame, sys
from pygame.locals import *

pygame.init()

#Create a displace surface object
#Below line will let you toggle from maximize to the initial size
DISPLAYSURF = pygame.display.set_mode((400, 300), RESIZABLE)

mainLoop = True

while mainLoop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainLoop = False
    pygame.display.update()

pygame.quit()
import pygame, os

os.environ['SDL_VIDEO_CENTERED'] = '1' # You have to call this before pygame.init()

pygame.init()

info = pygame.display.Info() # You have to call this before pygame.display.set_mode()
screen_width,screen_height = info.current_w,info.current_h

这些是您 screen/monitor 的尺寸。您可以使用这些或减少它们来排除边框和标题栏:

window_width,window_height = screen_width-10,screen_height-50
window = pygame.display.set_mode((window_width,window_height))
pygame.display.update()