pygame 中的全屏模式是全黑的 [英] The fullscreen mode in pygame is entirely black

查看:52
本文介绍了pygame 中的全屏模式是全黑的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 pygame 的全屏选项有一些问题.下面是一些简单地绘制一个蓝色窗口的代码,通过按 R 我们可以在蓝色和紫色之间切换.然后我们还可以使用 FG 在全屏和窗口模式之间切换.F 是明确实现的,G 使用方法 toggle_fullscreen().

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

我在 Ubuntu 18.04 上使用 Python 3.6.8.以下是我的观察:

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

  1. 使用 pygame 2.0.0.dev6,当使用 FG 全屏显示时,屏幕会执行以下操作:
  1. Using pygame 2.0.0.dev6, when going fullscreen with either F or G the screen does the following:
  1. 闪烁几次
  2. 作为最小化图标进入任务栏
  3. 如果我点击图标,屏幕会再闪烁几次,最后我们全屏了
  4. 问题:屏幕全黑,按钮R翻转颜色(但打印消息)
  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)

  • 仍在使用 pygame 2.0.0.dev6.在这种情况下,GF 按钮的行为不同:当使用 G 从全屏返回窗口时,R 按钮不会翻转颜色,即使在窗口版本中也是如此.当使用 F 代替它时.
  • 在 pygame 2.0.0.dev3 版本中,G 按钮根本不起作用,而 F 具有与以前相同的行为.
  • 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.
  • With pygame version 2.0.0.dev3 the G button does not work at all, while F has the same behavior as before.
  • 我的主要问题是 1.4.:全屏模式是全黑的.

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

    现在让我们做一个修改.更改 F 按钮

    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
    

    这会以当前的屏幕分辨率全屏显示,而不是我在顶部指定的分辨率.现在是问题 1.1.、1.2 和 1.3.消失了:应用程序立即进入全屏模式.但问题 1.4.持续存在,而且程序不再接受输入.如果我按 Q 它不会退出.它不需要 Alt+TabAlt+F4,所以我必须重新启动计算机.

    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.

    推荐答案

    pygame.display.set_mode 创建一个 pygame.Surface 对象,与窗口相关联.当 pygame.display.set_mode() 再次被调用时,之前与表面关联的对象将失效.

    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.

    你必须copy()旧"表面:

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

    这篇关于pygame 中的全屏模式是全黑的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆