Pygame双显示器和全屏模式

6

我正在使用pygame编写一个简单的行为测试程序。我在我的MacBook Pro上运行它,几乎所有功能都正常工作。然而,在测试期间,被试者会看到第二个外部显示器和笔记本电脑显示器。我想让游戏全屏显示在外部显示器上,而不是笔记本电脑的显示器上,以便我可以监控性能。目前,文件的开头看起来像这样:

#! /usr/bin/env python2.6

import pygame
import sys

stdscr = curses.initscr()
pygame.init()
screen = pygame.display.set_mode((1900, 1100), pygame.RESIZABLE)

我在考虑在可调整大小的屏幕上启动游戏,但是 OS X 在调整窗口大小时会出现问题。

4个回答

8
Pygame目前还不支持在单个进程中使用两个显示器。请参考这里的问题和开发者回答,其中他说:

一旦SDL 1.3完成,pygame就会得到支持,在同一个进程中使用多个窗口。

因此,你有以下选项:

  1. 使用多个进程。两个pygame实例,每个实例都最大化在自己的屏幕上,相互通信(你可以使用任何:非常酷的Python multiprocessing模块,本地TCP,管道,写入/读取文件等)
  2. 在两个显示器上设置相同的分辨率,并创建一个跨越它们的大型(宽)窗口,其中一个半部分显示你的信息,另一个半部分显示用户界面。然后手动将窗口放置在用户屏幕上,你的屏幕上。这是hacky的,但可能比工程化更好的解决方案更节省时间(“如果它很蠢但有效,那就不是蠢的”;)。
  3. 使用类似于pygame的pyglet,支持全屏窗口pyglet.window.Window(fullscreen=True, screens[1])

祝你好运。


我有一个类似的问题,我的投影仪正在发出光的图案,而我仍然希望能够在另一个显示器上后台做其他事情。这是否可能? - user391339
1
Pyglet在MacOS上不支持多显示器全屏。我是通过浪费大量时间并找到Pyglet源码中的这条注释才发现的:“#BUG:我怀疑当使用多个显示器(全屏显示窗口所在的显示器)时,它不能做正确的事情。然而,我没有测试的方法。” 它无法工作,实际上,我在MacOS上没有找到任何平台可以打开多个全屏显示器。 - David Stein
1
这并没有回答提问者的问题:“我想让游戏在外部显示器上全屏,而不是在笔记本电脑的显示器上。” 问题是关于如何从多个屏幕中选择特定的屏幕,而不是同时在多个屏幕上运行。 - Lummo

0

我做了一些傻事,但它确实有效。

我使用 get_monitors() 获取显示器数量,然后使用 SDL 将 Pygame 窗口的显示位置更改为添加到最小屏幕的宽度,以确保窗口定位在第二个监视器上。

from screeninfo import get_monitors

numberOfmonitors = 0
smallScreenWidth = 9999

for monitor in get_monitors():
    #getting the smallest screen width
    smallScreenWidth = min(smallScreenWidth, monitor.width)
    numberOfmonitors += 1

if numberOfmonitors > 1:
    x = smallScreenWidth
    y = 0
    #this will position the pygame window in the second monitor
    os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)

#you can check with a small window
#screen = pygame.display.set_mode((100,100))
#or go full screen in second monitor
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
#if you want to do other tasks on the laptop (first monitor) while the pygame window is being displayed on the second monitor, you shoudn't use fullscreen but instead get the second monitor's width and heigh using monitor.width and monitor.height, and set the display mode like
screen = pygame.display.set_mode((width,height))

0
display = pyglet.canvas.get_display()
display = display.get_screens()

win = pyglet.window.Window(screen=display[1])

------------------------------------------------------
screen=display[Номер монитора]

------------------------------------------------------
display = pyglet.canvas.get_display()
display = display.get_screens()

print(display) # Все мониторы которые есть

2
欢迎来到Stack Overflow!请阅读[答案]并[编辑]您的答案,以包含有关此代码实际解决问题的说明。请记住,您不仅要解决问题,还要教育OP和任何未来的读者。还请注意,Stack Overflow上的帖子必须是英文,即请确保用英语添加说明。 - Adriaan

0

我不确定在OS X上是否可以这样做,但值得一提的是对于Windows用户来说,如果您只想让程序在第二个屏幕上全屏运行,并且您正在使用Windows,则只需将另一个屏幕设置为主屏幕。

该设置可以在设置中的重新排列您的显示器下找到。

到目前为止,我所能在主屏幕上运行的任何东西都可以以这种方式运行,无需更改代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接