pygame.display
pygame module to control the display window and screen
Initialize the display module
Uninitialize the display module
Returns True if the display module has been initialized
Initialize a window or screen for display
Get a reference to the currently set display surface
Update the full display Surface to the screen
Update portions of the screen for software displays
Get the name of the pygame display backend
Create a video display information object
Get information about the current windowing system
Get sizes of active desktops
Get list of available fullscreen modes
Pick the best color depth for a display mode
Get the value for an OpenGL flag for the current display
Request an OpenGL display attribute for the display mode
Returns True when the display is active on the screen
Iconify the display surface
Switch between fullscreen and windowed displays
Change the hardware gamma ramps
Change the hardware gamma ramps with a custom lookup
Change the system image for the display window
Set the current window caption
Get the current window caption
Set the display color palette for indexed displays
Return the number of displays
Return the size of the window or screen
Return whether the screensaver is allowed to run.
Set whether the screensaver may run

该模块提供对PYGAME显示的控制。PYGAME有一个单一的显示界面,它要么包含在窗口中,要么全屏运行。创建显示后,您可以将其视为常规曲面。更改不会立即显示在屏幕上;您必须从两个翻转功能中选择一个来更新实际显示。

显示的原点是屏幕的左上角,其中x=0和y=0。两个轴都朝着屏幕的右下角正向增加。

PYGAME显示实际上可以在几种模式中的一种中初始化。默认情况下,该显示是基本的软件驱动的帧缓冲区。您可以请求特殊的模块,如自动缩放或OpenGL支持。它们由传递给 pygame.display.set_mode()

PYGAME在任何时候都只能有一个显示处于活动状态。使用创建一个新的 pygame.display.set_mode() 将关闭上一个显示。要检测连接的屏幕的数量和大小,您可以使用 pygame.display.get_desktop_sizes 然后选择要传递到的适当窗口大小和显示索引 pygame.display.set_mode()

为了向后兼容 pygame.display 允许精确控制像素格式或显示分辨率。对于旧的图形卡和CRT屏幕,这是必要的,但通常不再需要。使用函数 pygame.display.mode_ok()pygame.display.list_modes() ,以及 pygame.display.Info() 查询有关显示屏的详细信息。

创建显示表面后,此模块中的功能将影响单个现有显示。如果模块未初始化,则曲面无效。如果设置了新的显示模式,现有的表面将自动切换到在新的显示上运行。

当设置显示模式时,几个事件被放置在pyGame事件队列中。 pygame.QUIT 在用户请求关闭程序时发送。该窗口将接收 pygame.ACTIVEEVENT 事件作为显示获得和失去输入焦点。如果将显示设置为 pygame.RESIZABLE 旗帜, pygame.VIDEORESIZE 当用户调整窗口尺寸时,将发送事件。直接绘制到屏幕上的硬件显示器将获得 pygame.VIDEOEXPOSE 必须重新绘制部分窗口时发生的事件。

在pyGame 2.0.1中引入了一个新的WindowEvent API。有关这方面的更多信息,请查看事件模块文档

某些显示环境具有自动拉伸所有窗口的选项。启用此选项后,自动拉伸会扭曲pyGame窗口的外观。在pyGame Examples目录中,有示例代码(防止显示_伸展.py),它显示了如何在Microsoft Windows(需要Vista或更高版本)上禁用这种自动伸展的pyGame显示。

pygame.display.init()
Initialize the display module
init() -> None

初始化pyGame显示模块。在初始化之前,显示模块无法执行任何操作。当您调用更高级别时,这通常会自动为您处理 pygame.init()

在初始化时,PYGAME将从几个内部显示后端中选择一个。显示方式将根据当前用户的平台和权限进行选择。在初始化显示模块之前,环境变量 SDL_VIDEODRIVER 可以设置为控制使用哪个后端。此处列出了具有多个选择的系统。

Windows : windib, directx
Unix    : x11, dga, fbcon, directfb, ggi, vgl, svgalib, aalib

在一些平台上,可以将电子游戏显示嵌入到现有的窗口中。要做到这一点,环境变量 SDL_WINDOWID 必须设置为包含窗口ID或句柄的字符串。在初始化PYGAME显示时检查环境变量。请注意,在嵌入式显示器中运行时可能会有许多奇怪的副作用。

多次调用是无害的,重复调用没有任何效果。

pygame.display.quit()
Uninitialize the display module
quit() -> None

这将关闭整个显示模块。这意味着所有活动的显示器都将关闭。这也将在程序退出时自动处理。

多次调用是无害的,重复调用没有任何效果。

pygame.display.get_init()
Returns True if the display module has been initialized
get_init() -> bool

如果符合以下条件则返回True pygame.displaypygame module to control the display window and screen 模块当前已初始化。

pygame.display.set_mode()
Initialize a window or screen for display
set_mode(size=(0, 0), flags=0, depth=0, display=0, vsync=0) -> Surface

此函数将创建一个显示表面。传入的参数是对显示类型的请求。实际创建的显示将是系统支持的最佳匹配。

请注意,调用此函数会隐式初始化 pygame.display ,如果它以前未初始化的话。

Size参数是一对表示宽度和高度的数字。FLAGS参数是附加选项的集合。Depth参数表示用于颜色的位数。

可以像绘制常规曲面一样绘制返回的曲面,但最终会在监视器上看到更改。

如果未传递任何大小或设置为 (0, 0) 和冒牌游戏的使用 SDL 1.2.10或更高版本,创建的Surface将具有与当前屏幕分辨率相同的大小。如果仅将宽度或高度设置为 0 ,则Surface的宽度或高度将与屏幕分辨率相同。使用 SDL 1.2.10之前的版本将引发异常。

通常情况下,最好不要传递深度参数。它将默认为系统的最佳和最快的颜色深度。如果您的游戏需要特定的颜色格式,您可以使用此参数控制深度。PYGAME将模拟不可用的颜色深度,这可能会很慢。

请求全屏显示模式时,有时无法与所请求的大小完全匹配。在这些情况下,PYGAME将选择最接近的兼容匹配。返回的曲面仍将始终与请求的大小匹配。

在高分辨率显示器(4k、1080p)和微型图形游戏(640x480)上,显示得非常小,以至于无法玩。Scaled为您放大窗口。这款游戏认为它是一个640x480的窗口,但实际上它可以更大。鼠标事件会自动缩放,因此您的游戏不需要这样做。请注意,Scaled被认为是实验性的API,在未来的版本中可能会发生变化。

FLAGS参数控制您想要的显示类型。有几种类型可供选择,您甚至可以使用按位或运算符(竖线“|”字符)组合多种类型。以下是您需要选择的显示标志:

pygame.FULLSCREEN    create a fullscreen display
pygame.DOUBLEBUF     only applicable with OPENGL
pygame.HWSURFACE     (obsolete in pygame 2) hardware accelerated, only in FULLSCREEN
pygame.OPENGL        create an OpenGL-renderable display
pygame.RESIZABLE     display window should be sizeable
pygame.NOFRAME       display window will have no border or controls
pygame.SCALED        resolution depends on desktop size and scale graphics
pygame.SHOWN         window is opened in visible mode (default)
pygame.HIDDEN        window is opened in hidden mode

New in pygame 2.0.0: SCALED, SHOWN and HIDDEN

通过设置 vsync 参数设置为 1 ,可以通过垂直同步获得显示器,但不能保证一定能得到。该请求仅对调用 set_mode() 使用 pygame.OPENGLpygame.SCALED 标志已设置,即使设置了其中之一,仍不能保证。您得到的结果取决于运行pyGame的系统的硬件和驱动程序配置。以下是调用的用法示例 set_mode() 这可能会让您使用vsync进行显示:

flags = pygame.OPENGL | pygame.FULLSCREEN
window_surface = pygame.display.set_mode((1920, 1080), flags, vsync=1)

Vsync的行为被认为是实验性的,可能会在未来的版本中发生变化。

New in pygame 2.0.0: vsync

基本示例:

# Open a window on the screen
screen_width=700
screen_height=400
screen=pygame.display.set_mode([screen_width, screen_height])

显示索引 0 表示使用默认显示。如果未提供DISPLAY索引参数,则可以使用环境变量覆盖默认显示。

Changed in pygame 1.9.5: display 添加了参数

pygame.display.get_surface()
Get a reference to the currently set display surface
get_surface() -> Surface

返回对当前设置的显示曲面的引用。如果没有设置显示模式,则返回NONE。

pygame.display.flip()
Update the full display Surface to the screen
flip() -> None

这将更新整个显示的内容。如果您的显示模式使用标志 pygame.HWSURFACEpygame.DOUBLEBUF 在PYGAME 1上,这将等待垂直回程并交换表面。

在使用 pygame.OPENGL 显示模式这将执行GL缓冲区交换。

pygame.display.update()
Update portions of the screen for software displays
update(rectangle=None) -> None
update(rectangle_list) -> None

此函数类似于的优化版本 pygame.display.flip() 用于软件显示。它只允许更新屏幕的一部分,而不是整个区域。如果没有传递任何参数,它会更新整个外围应用 pygame.display.flip()

请注意,调用 display.update(None) 表示窗口的任何部分都不会更新。鉴于 display.update() 表示整个窗口都已更新。

您可以向该函数传递单个矩形或一系列矩形。一次传递多个矩形比使用单个或部分矩形列表多次调用UPDATE效率更高。如果传递一个矩形序列,则列表中不包含任何值是安全的,这将被跳过。

此调用不能用于 pygame.OPENGL 显示并将生成异常。

pygame.display.get_driver()
Get the name of the pygame display backend
get_driver() -> name

PYGAME在初始化时从许多可用显示后端中选择一个。这将返回用于显示后端的内部名称。这可用于提供有关哪些显示功能可能加速的有限信息。请参阅 SDL_VIDEODRIVER 中的标志 pygame.display.set_mode() 查看一些常见选项。

pygame.display.Info()
Create a video display information object
Info() -> VideoInfo

创建包含多个属性的简单对象来描述当前图形环境。如果在此之前调用此函数 pygame.display.set_mode() 某些平台可以提供有关默认显示模式的信息。也可以在设置显示模式后调用此函数,以验证是否满足特定的显示选项。VidInfo对象有几个属性:

hw:         1 if the display is hardware accelerated
wm:         1 if windowed display modes can be used
video_mem:  The megabytes of video memory on the display. This is 0 if
            unknown
bitsize:    Number of bits used to store each pixel
bytesize:   Number of bytes used to store each pixel
masks:      Four values used to pack RGBA values into pixels
shifts:     Four values used to pack RGBA values into pixels
losses:     Four values used to pack RGBA values into pixels
blit_hw:    1 if hardware Surface blitting is accelerated
blit_hw_CC: 1 if hardware Surface colorkey blitting is accelerated
blit_hw_A:  1 if hardware Surface pixel alpha blitting is accelerated
blit_sw:    1 if software Surface blitting is accelerated
blit_sw_CC: 1 if software Surface colorkey blitting is accelerated
blit_sw_A:  1 if software Surface pixel alpha blitting is accelerated
current_h, current_w:  Height and width of the current video mode, or
            of the desktop mode if called before the display.set_mode
            is called. (current_h, current_w are available since
            SDL 1.2.10, and pygame 1.8.0). They are -1 on error, or if
            an old SDL is being used.
pygame.display.get_wm_info()
Get information about the current windowing system
get_wm_info() -> dict

创建一个用字符串键填充的词典。字符串和值是由系统任意创建的。某些系统可能没有信息,并且将返回空词典。大多数平台将返回一个“Windows键”,其值设置为当前显示的系统ID。

New in pygame 1.7.1.

pygame.display.get_desktop_sizes()
Get sizes of active desktops
get_desktop_sizes() -> list

此函数以整数(x,y)元组列表的形式返回当前配置的虚拟桌面的大小。

列表的长度与连接的监视器的数量不同,因为一个桌面可以跨多个监视器镜像。桌面大小并不表示硬件支持的最大显示器分辨率,而是操作系统中配置的桌面大小。

为了使窗口适合当前配置的桌面,并尊重操作系统在全屏模式下配置的分辨率,此函数 应该 被用来取代许多用例 pygame.display.list_modes() 只要适用的话。

New in pygame 2.0.0.

pygame.display.list_modes()
Get list of available fullscreen modes
list_modes(depth=0, flags=pygame.FULLSCREEN, display=0) -> list

此函数用于返回指定颜色深度的可能大小列表。如果给定参数没有可用的显示模式,则返回值将为空列表。返回值为 -1 意味着任何请求的大小都应该可以工作(窗口模式很可能就是这种情况)。模式大小从大到小排序。

如果深度为 0 ,则使用显示器的当前/最佳颜色深度。标志默认为 pygame.FULLSCREEN ,但您可能需要为特定的全屏模式添加其他标志。

显示索引 0 表示使用默认显示。

从PYGAME 2.0开始, pygame.display.get_desktop_sizes() 已经接管了一些用例 pygame.display.list_modes()

要为非全屏窗口找到合适的大小,最好使用 pygame.display.get_desktop_sizes() 若要获取 当前 桌面,然后选择较小的窗口大小。这样,即使显示器配置为比硬件支持的最大分辨率更低的分辨率,也可以保证窗口适合。

为避免更改物理监视器分辨率,最好使用 pygame.display.get_desktop_sizes() 以确定全屏分辨率。强烈建议开发人员默认使用当前的物理显示器分辨率,除非用户明确要求不同的分辨率(例如,在选项菜单或配置文件中)。

Changed in pygame 1.9.5: display 添加了参数

pygame.display.mode_ok()
Pick the best color depth for a display mode
mode_ok(size, flags=0, depth=0, display=0) -> depth

此函数使用的参数与 pygame.display.set_mode() 。它用于确定请求的显示模式是否可用。它会回来的 0 如果无法设置显示模式。否则,它将返回与所要求的显示最匹配的像素深度。

通常不传递深度参数,但某些平台可以支持多个显示深度。如果通过,它将暗示哪个深度更匹配。

该函数将返回 0 如果无法设置传递的显示标志。

显示索引 0 表示使用默认显示。

Changed in pygame 1.9.5: display 添加了参数

pygame.display.gl_get_attribute()
Get the value for an OpenGL flag for the current display
gl_get_attribute(flag) -> value

在呼叫之后 pygame.display.set_mode() 使用 pygame.OPENGL 标志,最好检查任何请求的OpenGL属性的值。看见 pygame.display.gl_set_attribute() 获取有效标志的列表。

pygame.display.gl_set_attribute()
Request an OpenGL display attribute for the display mode
gl_set_attribute(flag, value) -> None

当呼叫时 pygame.display.set_mode() 使用 pygame.OPENGL 标志后,PYGAME自动处理设置OpenGL属性,如颜色和双缓冲。OpenGL提供了您可能希望控制的其他几个属性。将这些属性之一作为标志及其相应值进行传递。必须在此之前调用 pygame.display.set_mode()

许多设置都是要求的最低要求。如果OpenGL无法提供所请求的属性,则使用OpenGL上下文创建窗口将失败,但它可能会给你一个模具缓冲区,即使你没有请求,或者它可能会给你一个比请求的更大的模具缓冲区。

这个 OPENGL 标志为:

GL_ALPHA_SIZE, GL_DEPTH_SIZE, GL_STENCIL_SIZE, GL_ACCUM_RED_SIZE,
GL_ACCUM_GREEN_SIZE,  GL_ACCUM_BLUE_SIZE, GL_ACCUM_ALPHA_SIZE,
GL_MULTISAMPLEBUFFERS, GL_MULTISAMPLESAMPLES, GL_STEREO

GL_MULTISAMPLEBUFFERS

是否启用多重采样抗锯齿。默认为0(禁用)。

设置 GL_MULTISAMPLESAMPLES 设置为大于0的值以控制抗锯齿的量。典型的值为2或3。

GL_STENCIL_SIZE

模具缓冲区的最小位大小。默认为0。

GL_DEPTH_SIZE

深度缓冲区的最小位大小。默认为16。

GL_STEREO

1启用立体声3D。默认为0。

GL_BUFFER_SIZE

帧缓冲区的最小位大小。默认为0。

New in pygame 2.0.0: 其他属性:

GL_ACCELERATED_VISUAL,
GL_CONTEXT_MAJOR_VERSION, GL_CONTEXT_MINOR_VERSION,
GL_CONTEXT_FLAGS, GL_CONTEXT_PROFILE_MASK,
GL_SHARE_WITH_CURRENT_CONTEXT,
GL_CONTEXT_RELEASE_BEHAVIOR,
GL_FRAMEBUFFER_SRGB_CAPABLE

GL_CONTEXT_PROFILE_MASK

将OpenGL配置文件设置为下列值之一:

GL_CONTEXT_PROFILE_CORE             disable deprecated features
GL_CONTEXT_PROFILE_COMPATIBILITY    allow deprecated features
GL_CONTEXT_PROFILE_ES               allow only the ES feature
                                    subset of OpenGL

GL_ACCELERATED_VISUAL

设置为1以要求硬件加速,或设置为0以强制软件渲染。默认情况下,两者都被允许。

pygame.display.get_active()
Returns True when the display is active on the screen
get_active() -> bool

当Display Surface被认为可在屏幕上有效呈现并且可能对用户可见时,返回True。这是紧随其后的默认状态 pygame.display.set_mode() 。即使应用程序完全隐藏在另一个应用程序窗口后面,此方法也可能返回True。

如果显示表面已图标化或最小化(通过),则返回FALSE pygame.display.iconify() 或通过操作系统特定的方法,例如大多数桌面上可用的最小化图标)。

该方法还可以由于其他原因返回FALSE,而无需用户显式图标化或最小化应用程序。一个值得注意的例子是,如果用户具有多个虚拟桌面,并且显示表面不在活动虚拟桌面上。

备注

此函数返回True与应用程序是否具有输入焦点无关。请看 pygame.key.get_focused()pygame.mouse.get_focused() 用于输入焦点相关的接口。

pygame.display.iconify()
Iconify the display surface
iconify() -> bool

请求将显示表面的窗口图标化或隐藏。并非所有系统和显示器都支持图标化显示。如果成功,该函数将返回True。

当显示被图标化时 pygame.display.get_active() 会回来的 False 。事件队列应该接收一个 ACTIVEEVENT 窗口图标化时的事件。此外,事件队列还会收到 WINDOWEVENT_MINIMIZED 在pyGame 2上窗口已图标化的时候发生的事件。

pygame.display.toggle_fullscreen()
Switch between fullscreen and windowed displays
toggle_fullscreen() -> int

在窗口模式和全屏模式之间切换显示窗口。在使用PYGAME 1时,显示驱动程序的支持不是很好,但在PYGAME 2中,它是切换到全屏和从全屏切换的最可靠的方法。

PYGAME 1支持的显示驱动程序:

  • X11(Linux/Unix)

  • Wayland(Linux/Unix)

PYGAME 2支持的显示驱动程序:

  • Windows(Windows)

  • X11(Linux/Unix)

  • Wayland(Linux/Unix)

  • 可可(OSX/Mac)

备注

toggle_fullscreen() 在Windows上不起作用,除非窗口大小在 pygame.display.list_modes()Get list of available fullscreen modes 或者创建带有该标志的窗口 pygame.SCALED 。看见 issue #2380

pygame.display.set_gamma()
Change the hardware gamma ramps
set_gamma(red, green=None, blue=None) -> bool

在显示硬件上设置红色、绿色和蓝色伽玛值。如果绿色和蓝色参数没有通过,它们都将与红色相同。并非所有系统和硬件都支持伽马渐变,如果函数成功,它将返回 True

伽玛值为 1.0 创建线性颜色表。较低的值将使显示变暗,较高的值将变亮。

pygame.display.set_gamma_ramp()
Change the hardware gamma ramps with a custom lookup
set_gamma_ramp(red, green, blue) -> bool

使用显式查找表设置红色、绿色和蓝色Gamma渐变。每个参数应为256个整数的序列。整数的范围应在 00xffff 。并非所有系统和硬件都支持伽马渐变,如果函数成功,它将返回 True

pygame.display.set_icon()
Change the system image for the display window
set_icon(Surface) -> None

设置系统将用于表示显示窗口的运行时图标。默认情况下,所有窗口的窗口图标都有一个简单的电子游戏徽标。

请注意,调用此函数会隐式初始化 pygame.display ,如果它以前未初始化的话。

您可以通过任何表面,但大多数系统需要32x32左右的较小图像。图像可以具有将被传递到系统的色键透明度。

某些系统不允许窗口图标在显示后更改。此函数可以在之前调用 pygame.display.set_mode() 在设置显示模式之前创建图标。

pygame.display.set_caption()
Set the current window caption
set_caption(title, icontitle=None) -> None

如果显示有窗口标题,此功能将更改窗口上的名称。在pyGame 1.x中,一些系统支持替换较短的标题,用于最小化显示,但在pyGame 2中 icontitle 什么都不做。

pygame.display.get_caption()
Get the current window caption
get_caption() -> (title, icontitle)

返回显示窗口的标题和图标标题。在PYGAME 2.x中,这些值总是相同的。

pygame.display.set_palette()
Set the display color palette for indexed displays
set_palette(palette=None) -> None

这将更改8位显示器的视频显示调色板。这不会更改实际显示曲面的调色板,只会更改用于显示曲面的调色板。如果没有传递任何调色板参数,则将恢复系统默认调色板。调色板是一系列 RGB 三胞胎。

pygame.display.get_num_displays()
Return the number of displays
get_num_displays() -> int

返回可用显示的数量。如果满足以下条件,则该值始终为1 pygame.get_sdl_version()get the version number of SDL 返回低于2的主版本号。

New in pygame 1.9.5.

pygame.display.get_window_size()
Return the size of the window or screen
get_window_size() -> tuple

返回用初始化的窗口的大小 pygame.display.set_mode()Initialize a window or screen for display 。这可能与显示表面的大小不同,如果 SCALED 是使用的。

New in pygame 2.0.0.

pygame.display.get_allow_screensaver()
Return whether the screensaver is allowed to run.
get_allow_screensaver() -> bool

返回是否允许在应用程序运行时运行屏幕保护程序。默认值为 False 。默认情况下,在玩游戏时,pyGame不允许使用屏幕保护程序。

备注

有些平台没有屏幕保护程序或支持禁用屏幕保护程序。请看 pygame.display.set_allow_screensaver()Set whether the screensaver may run 对于带有屏幕保护程序支持的警告。

New in pygame 2.0.0.

pygame.display.set_allow_screensaver()
Set whether the screensaver may run
set_allow_screensaver(bool) -> None

更改在应用程序运行时是否应允许屏幕保护程序。该函数的参数的默认值为True。默认情况下,在玩游戏时,pyGame不允许使用屏幕保护程序。

如果屏幕保护程序因此功能而被禁用,则在以下情况下将自动允许其运行 pygame.quit()uninitialize all pygame modules 被称为。

可以通过环境变量影响缺省值 SDL_HINT_VIDEO_ALLOW_SCREENSAVER ,可以将其设置为 0 (禁用)或 1 (启用)。

备注

禁用屏幕保护程序取决于平台支持。当没有平台支持时,即使屏幕保护程序状态不变,此功能也会静默显示为工作。缺少反馈是因为SDL没有提供任何受支持的方法来确定其是否支持更改屏幕保护程序状态。 SDL_HINT_VIDEO_ALLOW_SCREENSAVER 在SDL 2.0.2或更高版本中提供。SDL1.2没有实现这一点。

New in pygame 2.0.0.




Edit on GitHub