python - 在不崩溃的情况下跳出 Pygame 全屏窗口

标签 python pygame

所以我正在使用 Pygame 为我正在编写的程序创建精美的显示。我选择 Pygame 是因为它很容易上手并且在动画方面做得很好。我希望显示器尽可能大,以便显示尽可能多的信息。问题是,我仍然希望能够进入程序的控制台。

Pygame 强制将全屏窗口置于前面,因此您无法跳出,将窗口移动到另一个 Windows 桌面会使显示崩溃。我会做一个关键技巧来切换 pygame 模式,但我不能使用 pygame.event.get() 因为程序是如何线程化的。

有没有办法让它成为一个全屏窗口,这样我就可以跳出并将它留在后台?我真的不希望它只是一个普通的窗口,因为它没有那么大。

在我退出并返回后显示崩溃,这是它的样子: enter image description here 我还得到一个非零退出代码:-805306369 (0xCFFFFFFF)

这是代码的分解版本,它仍然给我这个错误,你会注意到这里有一些东西,如果这是你的完整程序,你不会有,但我想保留尽可能多的架构我可以。

import pygame
import os

BACKGROUND = (9, 17, 27)

os.environ['SDL_VIDEO_WINDOW_POS'] = "0,0"
pygame.init()
pygame.font.init()

infoObject = pygame.display.Info()
SIZE = (infoObject.current_w, infoObject.current_h)
X_CENTER = SIZE[0]/2
Y_CENTER = SIZE[1]/2

# create a borderless window that's as big as the entire screen
SCREEN = pygame.display.set_mode((SIZE[0], SIZE[1]), pygame.NOFRAME)
clock = pygame.time.Clock()

TextFont = pygame.font.SysFont('Courant', 30)


class DisplayState:

    state = type(bool)

    def __init__(self):
        self.state = True

    def get_state(self):
        return self.state

    def change_state(self, new_state):
        self.state = new_state


def main(display_state_object):

    running = True

    while running:
        if display_state_object.get_state():
            SCREEN.fill(BACKGROUND)
            pygame.display.flip()
        else:
            return 1
    return

if __name__ == "__main__":
    main(DisplayState())

编辑

我觉得是多线程的问题!看这段代码:

产生错误

def start_display():
     display(params)

def display(params):
     pygame loop

if __name__ == "__main__":
     display_thread = threading.Thread(target=start_display)
     display_thread.start()

不会产生错误

def display(params):
     pygame loop

if __name__ == "__main__":
     display_thread = threading.Thread(target=display(params))
     display_thread.start
     # marker

确实有效的版本有一个问题,程序似乎没有在线程外继续前进(即永远不会到达标记)。这是线程库的工作方式吗?这可以解释为什么我有中间人功能。也许这是一个不同的问题,值得单独提问?

编辑

像这样设置线程允许主线程继续,但会返回 pygame 错误:

threading.Thread(target=display, args=(DisplayState(),))

最佳答案

在 windows/sdl 上使用真正的全屏模式没有简单的方法来做到这一点,解决这个问题的通常方法是使用无边框窗口。

下面是如何在 pygame 中创建这样一个“假的”全屏窗口:

import pygame
import os  

# you can control the starting position of the window with the SDL_VIDEO_WINDOW_POS variable
os.environ['SDL_VIDEO_WINDOW_POS'] = "0,0"
pygame.init()

# now let's see how big our screen is
info = pygame.display.Info()

# and create a borderless window that's as big as the entire screen
screen = pygame.display.set_mode((info.current_w, info.current_h), pygame.NOFRAME)

关于python - 在不崩溃的情况下跳出 Pygame 全屏窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51956370/

相关文章:

python - 将行向量转换为稀疏矩阵的 inptrs 的快速矢量化方法?

python - 如何将每 500 个文件移动到不同的文件夹中

python - Python 3.6.2、Pygame 1.9.3 中的开始菜单错误

python - Pygame 知道何时按下 shift+other 键

python - tempfile 和 listdir 奇怪的行为

python - Bottle 模板支持?

python - 使用列中的数值或 NaN 过滤 DataFrame 中的行

python - 无法再添加三个立方体

python - 为什么 python 会跳过这些代码行?

Python 游戏网络