问题描述
我是pygame.FULLSCREEN模式的新手,需要您的帮助.我正在写一个小游戏,玩家应该能够在普通模式和全屏模式之间切换.
I'm new to the pygame.FULLSCREEN mode, and I need your help.I'm writing a small game, in which the player is supposed to be able to switch between normal mode and full screen mode.
运行程序时,将打开窗口,然后单击最大化按钮,进入全屏模式.当我按Escape键时,我返回到正常模式.到目前为止一切正常.
When I run my program is that the window opens, and when I click on the maximize button I get into full screen mode. When I press escape, I get back to normal mode. Everything's working fine so far.
但是,当我第二次单击最大化按钮时,窗口被最大化了,但是我并没有进入全屏模式.另外,pygame使用的窗口部分仍保持正常大小.
But when I click the maximize button a second time the window is maximized, however, I do not get into full screen mode. Also, the part of the window that pygame uses remains normal size.
这是我的代码:
import pygame
pygame.init()
width = 500
height = 500
info = pygame.display.Info()
screen_width = info.current_w
screen_height = info.current_h
window = pygame.display.set_mode((width, height), pygame.RESIZABLE)
fullscreen = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.VIDEORESIZE:
window = pygame.display.set_mode((screen_width, screen_height), pygame.FULLSCREEN)
fullscreen = True
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE] and fullscreen:
window = pygame.display.set_mode((width, height), pygame.RESIZABLE)
fullscreen = False
window.fill((255, 255, 255))
pygame.display.update()
提前谢谢!
推荐答案
每次调整窗口大小时,都会执行 pygame.VIDEORESIZE
.因此,即使将窗口更改为较小的尺寸,也会发生 pygame.VIDEORESIZE
.通过 event.set
获取窗口的新大小,并根据当前状态创建新的 pygame.FULLSCREEN
或 pygame.FULLSCREEN
显示全屏的
.但是通过一个键(例如)设置 fullscreen
. pygame.event.post()
一个新的 pygame.event.Event()
和适当的参数:
The pygame.VIDEORESIZE
is executed every time when the window is resized. Hence even if the window is changed to a smaller size, then pygame.VIDEORESIZE
occurs.Get the new size of the window by event.set
and create a new pygame.FULLSCREEN
or pygame.FULLSCREEN
display dependent on the current state of fullscreen
. But set fullscreen
by a key, for instance . pygame.event.post()
a new pygame.event.Event()
with the proper arguments:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.VIDEORESIZE:
type = pygame.FULLSCREEN if fullscreen else pygame.RESIZABLE
window = pygame.display.set_mode(event.size, type)
elif event.type == pygame.KEYDOWN:
if not fullscreen and event.key == pygame.K_f:
fullscreen = True
pygame.event.post(pygame.event.Event(pygame.VIDEORESIZE, size = (screen_width, screen_height)))
if fullscreen and event.key == pygame.K_ESCAPE:
fullscreen = False
pygame.event.post(pygame.event.Event(pygame.VIDEORESIZE, size = (width, height)))
window.fill((255, 255, 255))
pygame.display.update()
这篇关于切换到Pygame全屏模式仅工作一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!