我对pygame的全屏选项有一些问题。这是一些简单地绘制蓝色窗口的代码,通过按R我们可以在蓝色和紫色之间切换。然后,我们还可以使用F或G在全屏模式和窗口模式之间切换。 F是显式实现的,G使用方法toggle_fullscreen()
。
toggle_fullscreen()
我在使用Python 3.6.8的Ubuntu 18.04上。这是我的观察结果:
现在让我们进行修改。更改
F
按钮的代码中的以下行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()
这将以当前的屏幕分辨率进入全屏显示,而不是我在顶部指定的分辨率。现在的问题是1.1。,1.2和1.3。不见了:该应用立即进入全屏模式。但是问题1.4。仍然存在,并且该程序不再接受输入。如果按,它不会退出。它不需要Alt + Tab或Alt + F4,所以我必须重新启动计算机。Q
setmode = FULLSCREEN|SCALED if is_fullscreen else RESIZABLE #FULLSCREEN -> FULLSCREEN|SCALED
创建一个pygame.display.set_mode
对象,该对象与窗口关联。当再次调用pygame.display.set_mode
时,之前与该表面关联的对象将失效。您必须pygame.Surface
“旧”表面:
pygame.Surface