The fullscreen mode in pygame is entirely black

不问归期 提交于 2020-08-09 10:18:03

问题


I have some issues with the fullscreen option of pygame. Here is some code that simply draws a blue window and by pressing R we can switch between blue and purple. Then we can also toggle between fullscreen and windowed mode using F or G. F is implemented explicitly and G uses the method 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()

I am on Ubuntu 18.04 using Python 3.6.8. Here are my observations:

  1. Using pygame 2.0.0.dev6, when going fullscreen with either F or G the screen does the following:
    1. flashes a few times
    2. goes in the task bar as a minimized icon
    3. if I click on the icon the screen flashes a few more times and finally we are fullscreen
    4. problem: the screen is entirely black and the button R does not flip the colors (but prints the message)
  2. Still using pygame 2.0.0.dev6. In this case the G and the F button behave differently: when going back from fullscreen to windowed with G, the R button doesn't flip the colors, even in the windowed version. When using F instead it works.
  3. With pygame version 2.0.0.dev3 the G button does not work at all, while F has the same behavior as before.

My major problem is 1.4.: the fullscreen mode is entirely black.

Now let's do a modification. Change the following line in the code for the F button

setmode = FULLSCREEN|SCALED if is_fullscreen else RESIZABLE    #FULLSCREEN -> FULLSCREEN|SCALED

This goes fullscreen with the current screen resolution and not the one I specify at the top. Now the problems 1.1., 1.2 and 1.3. are gone: the app goes to fullscreen immediately. But the problem 1.4. persists and furthermore the program does not accept inputs anymore. If I press Q it won't quit. It doesn't take Alt+Tab or Alt+F4 and so I have to restart the computer.


回答1:


pygame.display.set_mode creates a pygame.Surface object, which is associated to the window. When pygame.display.set_mode() is called a again, then the object which was associated to the surface before gets invalide.

You've to copy() the "old" surface:

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))


来源:https://stackoverflow.com/questions/59127002/the-fullscreen-mode-in-pygame-is-entirely-black

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!