Pygame编程(2)display模块
pygame编程2-display设备显示
-
pygame.display.init()
- 初始化 display 模块
- init() -> None
-
pygame.display.get_init()
- 获取display初始化 状态,如果已经初始化,返回 True,否则返回False
- get_init() -> bool
-
pygame.display.quit()
- 退出 display 模块
- quit() -> None
-
pygame.display.set_mode()
- 初始化一个准备显示的窗口或屏幕
- set_mode(size=(0, 0), flags=0, depth=0, display=0, vsync=0) -> Surface
flags = pygame.OPENGL | pygame.FULLSCREEN
window_surface = pygame.display.set_mode((1920, 1080), flags, vsync=1)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 -
pygame.display.get_surface()
- 获取当前显示的 Surface 对象
- get_surface() -> Surface
-
pygame.display.flip()
- 更新整个待显示的 Surface 对象到屏幕上
- flip() -> None
-
pygame.display.update()
- 更新部分软件界面显示
- update(rectangle=None) -> None
- update(rectangle_list) -> None
-
pygame.display.get_dirver()
- 获取 Pygame 显示驱动的名字
- get_driver() -> name
-
pygame.display.Info()
- 获取有关显示界面的信息对象
- Info() -> VideoInfo
-
pygame.display.get_wm_info()
- 获取关于当前窗口系统的信息
- get_wm_info() -> dict
-
pygame.display.get_desktop_sizes()
- 获取当前桌面大小
- get_desktop_sizes() -> list
-
pygame.display.list_modes()
- 获取全屏模式下可使用的分辨率
- list_modes(depth=0, flags=pygame.FULLSCREEN, display=0) -> list
-
pygame.display.mode_ok()
- 为显示模式选择最佳颜色深度
- mode_ok(size, flags=0, depth=0, display=0) -> depth
-
pygame.display.gl_get_attribute()
- 获取当前显示的OpenGL标志的值
- gl_get_attribute(flag) -> value
-
pygame.display.gl_set_attribute()
- 设置当前显示的OpenGL标志的值
- gl_set_attribute(flag, value) -> None
-
pygame.display.get_active()
- 当显示在屏幕上处于活动状态时返回True
- get_active() -> bool
-
pygame.display.iconify()
- 最小化显示窗口
- iconify() -> bool
-
pygame.display.toggle_fullscreen()
- 在全屏和窗口显示之间切换
- toggle_fullscreen() -> int
-
pygame.display.set_gamma()
- 改变硬件伽马ramps
- set_gamma(red, green=None, blue=None) -> bool
-
pygame.display.set_gamma_ramp
- 使用自定义查找更改硬件伽马ramp
- set_gamma_ramp(red, green, blue) -> bool
-
pygame.display.set_icon()
- 设置窗口图标
- set_icon(Surface) -> None
-
pygame.display.set_caption()
- 设置当前窗口标题
- set_caption(title, icontitle=None) -> None
-
pygame.display.get_caption()
- 获取当前窗口标题
- get_caption() -> (title, icontitle)
-
pygame.display.set_palette()
- 为索引显示设置显示调色板
- set_palette(palette=None) -> None
-
pygame.display.get_num_displays()
- 返回显示的个数
- get_num_displays() -> int
-
pygame.display.get_window_size()
- 返回窗口或屏幕的大小
- get_window_size() -> tuple
-
pygame.display.get_allow_screensaver()
- 返回是否允许屏幕保护程序运行
- get_allow_screensaver() -> bool
-
pygame.display.set_allow_screensaver()
- 设置屏幕保护程序是否可以运行
- set_allow_screensaver(bool) -> None
import sys
import pygame
from pygame.locals import *# 参数定义
DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 600COLOR_BLACK = (0, 0, 0)
COLOR_WHITE = (255, 255, 255)
COLOR_RED = (255, 0, 0)
COLOR_GREEN = (0, 255, 0)
COLOR_BLUE = (0, 0, 255)# 1. 初始化模块
print('pygame初始化状态:', pygame.get_init())
pygame.init()# 2. 设置主屏窗口
print('display初始化状态:', pygame.display.get_init())
display = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT), flags=RESIZABLE)# 设置窗口标题
pygame.display.set_caption('显示模块Demo')# 获取窗口标题
caption = pygame.display.get_caption()
print('caption:', caption)# 设置窗口ICON
img = pygame.image.load('D:/project/python/pygame/demo1/icons/wifi_icon.png')
pygame.display.set_icon(img)# 获取窗口大小
window_size = pygame.display.get_window_size()
print('窗口大小:', window_size)# 获取屏保使能状态
screensaver = pygame.display.get_allow_screensaver()
print('screensaver:', screensaver)
# 设置屏保使能状态
pygame.display.set_allow_screensaver(True)# 最小化窗口,再次调用恢复正常窗口尺寸
pygame.display.iconify()# toggle全屏显示
pygame.display.toggle_fullscreen()# 获取全屏模式下可使用的分辨率
modes = pygame.display.list_modes()
print(modes)
# [(1920, 1080), (1680, 1050), (1600, 900), (1440, 900), (1400, 1050), (1366, 768), (1360, 768), (1280, 1024), (1280, 800), (1280, 768), (1280, 720), (1024, 768), (800, 600), (640, 480), (640, 400), (512, 384), (400, 300), (320, 240), (320, 200)]# 获取桌面尺寸
desktop_sizes = pygame.display.get_desktop_sizes()
print(desktop_sizes)
# [(1536, 864), (1536, 864)]pygame.display.flip()pygame.display.update()# 3. 程序主循环
while True:for event in pygame.event.get():if event.type == QUIT:# 4. 退出模块pygame.quit()# 5. 终止程序sys.exit()