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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31538506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:11:52  来源:igfitidea点击:

How do I maximize the display screen in PyGame?

pythonpygame

提问by Addarsh Chandrasekar

I am new to Python programming and I recently started working with the PyGame module. Here is a simple piece of code to initialize a display screen. My question is : Currently, the maximize button is disabled and I cannot resize the screen. How do I enable it to switch between full screen and back? Thanks

我是 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()

采纳答案by muddyfish

To become fullscreen at native resolution, do

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

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

To make the window resizeable, add the pygame.RESIZABLEparameter when seting the mode. You can set the mode of the screen surface multiple times but you might have to do pygame.display.quit()followed by pygame.display.init()

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

You should also check the pygame documentation here http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode

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

回答by Zenohm

The method you are looking for is pygame.display.toggle_fullscreen

您正在寻找的方法是 pygame.display.toggle_fullscreen

Or, as the guide recommends in most situations, calling pygame.display.set_mode()with the FULLSCREENtag.

或者,正如指南在大多数情况下建议的那样,pygame.display.set_mode()使用FULLSCREEN标签调用。

In your case this would look like

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

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

(Please use the pygame.FULLSCREENinstead of just FULLSCREENbecause upon testing with my own system FULLSCREENjust maximized the window without fitting the resolution while pygame.FULLSCREENfit my resolution as well as maximizing.)

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

回答by Michael Chav

You'd have to add the full screen parameter onto the display declaration like this:

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

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()

回答by Tanmaya Meher

This will let you toggle from maximize to the initial size

这将让您从最大化切换到初始大小

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()

回答by Apostolos

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

These are the dimensions of your screen/monitor. You can use these or reduce them to exclude borders and title bar:

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

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