pygame双显示器与全屏模式

6 投票
4 回答
8595 浏览
提问于 2025-04-16 23:48

我正在用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 个回答

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

我不知道在OS X上是否可以这样做,但对于Windows用户来说,这个方法值得一提。如果你想让你的程序在第二个屏幕上全屏运行,只需把另一个屏幕设置为主屏幕。

这个设置可以在设置中的“重新排列显示器”选项里找到。

到目前为止,我在主显示器上能运行的任何程序,都可以这样在第二个屏幕上运行,不需要修改你的代码。

8

Pygame 目前不支持在一个程序里同时使用两个显示器。你可以查看这个问题的讨论 这里,以及开发者在 紧接着的回答,他提到:

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

所以,你可以考虑以下几种方法:

  1. 使用多个进程。你可以运行两个 pygame 实例,每个实例在自己的屏幕上全屏显示,两个实例之间可以互相通信(你可以使用很酷的 Python 的多进程模块、本地 TCP、管道、读写文件等等)。
  2. 将两个显示器设置为相同的分辨率,然后创建一个大窗口,跨越两个显示器,一半显示你的信息,另一半显示用户的内容。然后手动调整窗口的位置,让用户那一侧在他们的屏幕上,而你的内容在笔记本屏幕上。虽然这种方法有点笨,但可能比想出更好的解决方案更有效(“如果它傻但有效,那就不是傻” ;)。
  3. 使用 pyglet,它和 pygame 类似,并且 支持全屏窗口pyglet.window.Window(fullscreen=True, screens[1])

祝你好运。

撰写回答