[英]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()
要以原始分辨率全屏显示,请执行
DISPLAYSURF = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
要使窗口可调整大小,请在设置模式时添加pygame.RESIZABLE
参数。 您可以多次设置屏幕表面的模式,但您可能必须执行pygame.display.quit()
后跟pygame.display.init()
您还应该在此处查看 pygame 文档http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode
您正在寻找的方法是pygame.display.toggle_fullscreen
或者,正如指南在大多数情况下建议的那样, 使用FULLSCREEN
标记调用pygame.display.set_mode()
。
在你的情况下,这看起来像
DISPLAYSURF = pygame.display.set_mode((400, 300), pygame.FULLSCREEN)
(请使用pygame.FULLSCREEN
而不是FULLSCREEN
,因为在使用我自己的系统进行测试时, FULLSCREEN
只是最大化了窗口而不适合分辨率,而pygame.FULLSCREEN
适合我的分辨率以及最大化。)
这将让您从最大化切换到初始大小
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
这些是您的屏幕/显示器的尺寸。 您可以使用这些或减少它们以排除边框和标题栏:
window_width,window_height = screen_width-10,screen_height-50
window = pygame.display.set_mode((window_width,window_height))
pygame.display.update()
您必须将全屏参数添加到显示声明中,如下所示:
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()
只需使用 pygame.RESIZABLE 作为更简单的选项。
screen = pygame.display.set_mode()
或者
screen = pygame.display.set_mode(flags=pygame.FULLSCREEN)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.