进入全屏模式时屏幕尺寸未更新

问题描述 投票:1回答:1

[进入全屏模式时,窗口会进入全屏模式,但是颜色不会。我试过检查它是否处于全屏状态并使用RESIZE属性刷新,但这仅适用于在全屏之外调整大小的情况。代码:

import time
import ctypes, pygame
from Game import Character
from pygame.locals import (
    K_UP,
    K_DOWN,
    K_LEFT,
    K_RIGHT,
    K_ESCAPE,
    K_F11,
    KEYDOWN,
    QUIT
)
print("Starting")

global active
global screen

user32 = ctypes.windll.user32
SCREEN_WIDTH = int(round(user32.GetSystemMetrics(0) / 2, 0))
SCREEN_HEIGHT = int(round(user32.GetSystemMetrics(1) / 2, 0))

timeOne = time.time()

pygame.init()

def Main():
    active = True
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.RESIZABLE)
    pygame.display.set_caption("Test")

    print("Done. Complete in ", round(time.time()-timeOne, 2), "seconds.")

    while active:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                active = False
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    active = False
                elif event.key == K_F11:
                    if screen.get_flags() & pygame.FULLSCREEN:
                        pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
                    else:
                        pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.FULLSCREEN)
            elif event.type == pygame.VIDEORESIZE:
                screenSize = event.size
                screen = pygame.display.set_mode(screenSize, pygame.RESIZABLE)

        screen.fill((0, 0, 255))
        pygame.display.flip()

    pygame.quit()

Main()

这是我第一次参加pygame,因此对于任何菜鸟错误,我们都表示歉意。

python pygame
1个回答
0
投票

我正在使用的代码确定了在切换窗口determines全屏显示时的屏幕尺寸。我不认为会在开关中触发VIDEORESIZE事件,因为视频size并未实际更改(也许有一种方法可以触发该事件,但是我认为不同的全屏Vs窗口大小有点历史文物)。

结果是在切换过程中必须重新初始化显示。下面的程序将在F11上的窗口↔全屏模式之间切换,将整个屏幕涂成蓝色。在Internet上有一些较旧的代码片段和有关此切换技术的注释,其中包括对鼠标处理的更改等。我发现这些不再是必需的……至少在我的测试系统上。 (请让我们知道是否需要进一步的操作。)

import pygame

# Window size
WINDOW_WIDTH      = 1600
WINDOW_HEIGHT     = 900
WINDOW_FULLSCREEN = False

SKY_BLUE = ( 61,  55, 254)

### Initialisation
pygame.init()
SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
window  = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), SURFACE )
pygame.display.set_caption("Full Screen Test")


clock = pygame.time.Clock()
done = False
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True
        elif ( event.type == pygame.VIDEORESIZE ):
            WINDOW_WIDTH  = event.w
            WINDOW_HEIGHT = event.h
            window  = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), SURFACE )

    # Movement keys
    keys = pygame.key.get_pressed()
    if ( keys[pygame.K_q] and ( keys[pygame.K_RCTRL] or keys[pygame.K_LCTRL] ) ):
        print("^Q")
        done = True
    elif ( keys[pygame.K_F11] ):
        print("fullscreen toggle")
        pygame.display.toggle_fullscreen()
        new_screen  = pygame.display.get_surface()
        bits        = new_screen.get_bitsize()      
        screen_info = pygame.display.Info()
        new_width   = screen_info.current_w
        new_height  = screen_info.current_h 
        print("new_width=%d, new_height=%d" % (new_width, new_height) )
        if ( window.get_flags() & pygame.FULLSCREEN ): 
            flags = pygame.FULLSCREEN | pygame.HWSURFACE | pygame.DOUBLEBUF
        else:
            flags = pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE
        pygame.display.quit()
        pygame.display.init()
        window        = pygame.display.set_mode( ( new_width, new_height ), flags, bits )
        WINDOW_WIDTH  = new_width
        WINDOW_HEIGHT = new_height
        #pygame.key.set_mods( 0 ) #HACK: work-a-round for a SDL bug??

    # Update the window, but not more than 60fps
    window.fill( SKY_BLUE )
    pygame.display.flip()

    # Clamp FPS
    clock.tick_busy_loop(60)

    # Failsafe - exit after 15 seconds (in case your display goes sqrilly)
    if ( pygame.time.get_ticks() > 15000 ):
        print("Exiting Failsafe")
        break

pygame.quit()
© www.soinside.com 2019 - 2024. All rights reserved.