Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
443 views
in Technique[技术] by (71.8m points)

python - The fullscreen mode in pygame is entirely black

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...