【问题标题】:I'm trying to toggle the screen from regular to full screen but it freezes toggling back to regular screen我正在尝试将屏幕从常规切换到全屏,但它冻结切换回常规屏幕
【发布时间】:2021-09-14 14:23:41
【问题描述】:

就像标题所说的那样,我正在尝试从正常屏幕切换到全屏并返回,但它喜欢冻结,这告诉我我做错了什么或不理解某些事情。

window_width = 800
window_height = 600
black = (0,0,0)
close_program = False
fullscreen = False
import pygame
from pygame.constants import FULLSCREEN 
pygame.init()
from configuration import *

#pygame.display.set_caption("game name")
screen = pygame.display.set_mode((window_width, window_height),0 ,32)

while close_program == False:
    evnt = pygame.event.poll()
    if evnt.type == pygame.QUIT:
        close_program = True
    if pygame.key.get_pressed()[pygame.K_F5]:
        fullscreen = not fullscreen
    if fullscreen:
        screen = pygame.display.set_mode((window_width, window_height), pygame.FULLSCREEN, 32)
    else:
        screen = pygame.display.set_mode((window_width, window_height),0 ,32)

【问题讨论】:

  • 我试过了,程序崩溃了。唯一的原因是我相信有些事情我不理解
  • 你不应该重复调用set_mode。只有在发送了相应的事件时才这样做。
  • 你有一个例子,对不起我不明白我认为这是设置屏幕的唯一方法

标签: python python-3.x visual-studio-code pygame


【解决方案1】:

您的代码存在多个问题。最大的是你应该只在偶数到来时切换全屏。通常,您应该有一个主循环,并在其中有一个 for 循环遍历所有新事件:


while close_program == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
            close_program = True
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_F5:
            fullscreen = not fullscreen
            pygame.display.toggle_fullscreen()

    screen.fill(255)
    pygame.display.update()

该代码还会检查何时按下F5 按钮,然后更改fullscreen 变量的状态(此处并不真正需要)并调用pygame.display.toggle_fullscreen()。这就是你应该切换到全屏的方式。

如果toggle_fullscreen不起作用,可以尝试手动操作:


        elif event.type == pygame.KEYDOWN and event.key == pygame.K_F5:
            fullscreen = not fullscreen
            if fullscreen:
                screen = pygame.display.set_mode((window_width, window_height), pygame.FULLSCREEN, 32)
            else:
                screen = pygame.display.set_mode((window_width, window_height), 0, 32)

然而,这不太理想,对我来说不能正常工作。

【讨论】:

  • 我得到了第一个工作的唯一问题是它是否也应该最小化所有其他程序?
  • @DemetriusAlize 它不适合我。可能取决于操作系统。例如,它会改变 Windows 上的显示分辨率,也许这就是您观察到的效果?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 1970-01-01
相关资源
最近更新 更多