我对 pygame 的全屏选项有一些问题。下面是一些简单地绘制蓝色窗口的代码,通过按 R 我们可以在蓝色和紫色之间切换。然后我们还可以使用 F 或 G 在全屏和窗口模式之间切换。 F 是显式实现的,G 使用方法 toggle_fullscreen()
.
import pygame, sys
from pygame.locals import *
#Initializes pygame
pygame.init()
#Defines the Clock object
clock = pygame.time.Clock()
#Just draws a blue screen
size = (960, 540)
blue = (0,0,100)
purp = (100,0,100)
is_blue = True
display_surf = pygame.display.set_mode(size, RESIZABLE)
display_surf.fill(blue)
mainLoop = True
is_fullscreen = False
#Mainloop
while mainLoop:
dt = clock.tick(12)
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainLoop = False
if event.type == pygame.KEYDOWN:
#Single key pressed
if event.key == K_f:
#Toggles fullscreen
is_fullscreen = not is_fullscreen
old_surface = display_surf
setmode = FULLSCREEN if is_fullscreen else RESIZABLE
display_surf = pygame.display.set_mode(size, setmode)
display_surf.blit(old_surface, (0,0))
del old_surface
if event.key == K_q:
#Quits the app
mainLoop = False
if event.key == K_r:
#Redraws the blue or purple
print("Trying to flip colors")
display_surf.fill(purp if is_blue else blue)
is_blue = not is_blue
if event.key == K_g:
#Toggles fullscreen with the dedicated method
is_fullscreen = not is_fullscreen
pygame.display.toggle_fullscreen()
pygame.display.update()
pygame.quit()
我在 Ubuntu 18.04 上使用 Python 3.6.8。以下是我的观察:
- 使用 pygame 2.0.0.dev6,当使用 F 或 G 进入全屏时,屏幕会执行以下操作:
- 闪烁几次
- 作为最小化图标出现在任务栏中
- 如果我点击图标,屏幕会再闪烁几次,最后我们就全屏了
- 问题:屏幕全黑,并且按钮R不翻转颜色(但打印消息)
- 仍在使用 pygame 2.0.0.dev6。在这种情况下,G 和 F 按钮的行为不同:当使用 G 从全屏返回到窗口时,R kbd> 按钮不会翻转颜色,即使在窗口版本中也是如此。当使用 F 代替时,它可以工作。
- 在 pygame 版本 2.0.0.dev3 中,G 按钮根本不起作用,而 F 的行为与以前相同。
我的主要问题是 1.4.:全屏模式是全黑的。
现在让我们进行修改。更改 F 按钮的代码中的以下行
setmode = FULLSCREEN|SCALED if is_fullscreen else RESIZABLE #FULLSCREEN -> FULLSCREEN|SCALED
这会以当前屏幕分辨率全屏显示,而不是我在顶部指定的分辨率。现在问题 1.1.、1.2 和 1.3。消失了:应用程序立即进入全屏。但问题1.4。仍然存在,而且该程序不再接受输入。如果我按Q它不会退出。它不需要 Alt+Tab 或 Alt+F4,所以我必须重新启动计算机。
最佳答案
pygame.display.set_mode
创建 pygame.Surface
对象,它与窗口关联。当再次调用 pygame.display.set_mode() 时,之前关联到 Surface 的对象将失效。
你必须copy()
“旧”表面:
is_fullscreen = not is_fullscreen
old_surface = display_surf.copy()
setmode = FULLSCREEN if is_fullscreen else RESIZABLE
display_surf = pygame.display.set_mode(size, setmode)
display_surf.blit(old_surface, (0,0))
关于python - pygame的全屏模式是全黑的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59127002/