You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何读取Pygame Display的当前标志(如FULLSCREEN等)?

如何读取Pygame Display的当前标志(并实现is_fullscreen())

当然可以读取Pygame Display已经设置的标志!Pygame提供了直接的方法来获取当前显示表面的所有配置标志,完全能满足你实现is_fullscreen()函数的需求。

核心方法:Surface.get_flags()

当你调用pygame.display.set_mode()后,会返回一个显示Surface对象(或者你可以通过pygame.display.get_surface()随时获取当前的显示Surface)。这个Surface对象的get_flags()方法会返回一个整数,这个整数是所有已设置标志的位掩码——每一个标志对应掩码中的一个位。

实现is_fullscreen()函数

要判断是否处于全屏状态,只需要用返回的掩码和pygame.FULLSCREEN常量做位与运算:如果结果非零,说明全屏标志已开启。

以下是完整的示例代码:

import pygame

pygame.init()

# 初始化一个窗口(示例:先窗口模式,再切换全屏)
screen = pygame.display.set_mode((800, 600))

def is_fullscreen():
    # 获取当前显示Surface
    current_surface = pygame.display.get_surface()
    if not current_surface:
        # 处理还未初始化display的情况
        return False
    # 通过位运算检查FULLSCREEN标志
    return (current_surface.get_flags() & pygame.FULLSCREEN) != 0

# 测试:切换全屏并验证
pygame.display.set_mode((800, 600), pygame.FULLSCREEN)
print(is_fullscreen())  # 输出: True

pygame.display.set_mode((800, 600))
print(is_fullscreen())  # 输出: False

拓展:检查其他标志

这个方法同样适用于其他Display标志,比如pygame.RESIZABLE、pygame.NOFRAME等,只需要替换对应的常量即可:

def is_resizable():
    surface = pygame.display.get_surface()
    return surface and (surface.get_flags() & pygame.RESIZABLE) != 0

内容的提问来源于stack exchange,提问作者Mahi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:09:53