我想创建两个单独的pygame
显示。我知道用pygame的单个实例是不可能做到的。我想知道如何/是否可以实施以下任何解决方案:
sys.argv
,然后在第二个实例中运行该函数。我如何确保跨平台兼容性?我不在乎程序的效率如何,丑陋等等。我只是想做这个工作。
重要的是,主代码必须与相关代码通信(即,更改下面属于extraDisplayManager
的某些变量),但是我计划使用pickle保存到文件并以这种方式进行通信。我已经在相关部分进行了多线程处理,因此缺乏同步性并不重要。
我的代码相当长而且很复杂,因此我不会在此处发布它,但是相关部分的要点是:
def mainCode(*someargs):
d = pygame.display.set_mode(dimensions)
if relevantArg:
extraDisplayManager = RelevantClass(*someotherargs)
threading.Thread(target=extraDisplayManager.relevantFunction,
daemon=True).start()
...
class RelevantClass:
def relevantFunction(self, *someotherargs):
self.d = pygame.display.set_mode(dimensions)
while True:
updateDisplay(someargs) # This is part of my main code, but it
# is standalone, so I could copy it to a new module
如果您能回答我的一些问题或给我看一些相关的文档,我将不胜感激。
答案 0 :(得分:1)
如果确实(真的)需要两个显示,则可以使用python的multiprocessing模块生成一个进程,并使用Queue
在两个进程之间传递数据。
这是我一起砍的一个例子:
import pygame
import pygame.freetype
import random
import multiprocessing as mp
# a simple class that renders a button
# if you press it, it calls a callback function
class Button(pygame.sprite.Sprite):
def __init__(self, callback, *grps):
super().__init__(*grps)
self.image = pygame.Surface((200, 200))
self.image.set_colorkey((1,2,3))
self.image.fill((1,2,3))
pygame.draw.circle(self.image, pygame.Color('red'), (100, 100), 50)
self.rect = self.image.get_rect(center=(300, 240))
self.callback = callback
def update(self, events, dt):
for e in events:
if e.type == pygame.MOUSEBUTTONDOWN:
if (pygame.Vector2(e.pos) - pygame.Vector2(self.rect.center)).length() <= 50:
pygame.draw.circle(self.image, pygame.Color('darkred'), (100, 100), 50)
self.callback()
if e.type == pygame.MOUSEBUTTONUP:
pygame.draw.circle(self.image, pygame.Color('red'), (100, 100), 50)
# a simple class that display a text for 1 second anywhere
class Message(pygame.sprite.Sprite):
def __init__(self, screen_rect, text, font, *grps):
super().__init__(*grps)
self.image = pygame.Surface((300, 100))
self.image.set_colorkey((1,2,3))
self.image.fill((1,2,3))
self.rect = self.image.get_rect(center=(random.randint(0, screen_rect.width),
random.randint(0, screen_rect.height)))
font.render_to(self.image, (5, 5), text)
self.timeout = 1000
self.rect.clamp_ip(screen_rect)
def update(self, events, dt):
if self.timeout > 0:
self.timeout = max(self.timeout - dt, 0)
else:
self.kill()
# Since we start multiple processes, let's create a mainloop function
# that can be used by all processes. We pass a logic_init_func-function
# that can do some initialisation and returns a callback function itself.
# That callback function is called before all the events are handled.
def mainloop(logic_init_func, q):
import pygame
import pygame.freetype
pygame.init()
screen = pygame.display.set_mode((600, 480))
screen_rect = screen.get_rect()
clock = pygame.time.Clock()
dt = 0
sprites_grp = pygame.sprite.Group()
callback = logic_init_func(screen, sprites_grp, q)
while True:
events = pygame.event.get()
callback(events)
for e in events:
if e.type == pygame.QUIT:
return
sprites_grp.update(events, dt)
screen.fill((80, 80, 80))
sprites_grp.draw(screen)
pygame.display.flip()
dt = clock.tick(60)
# The main game function is returned by this function.
# We need a reference to the slave process so we can terminate it when
# we want to exit the game.
def game(slave):
def game_func(screen, sprites_grp, q):
# This initializes the game.
# A bunch of words, and one of it is randomly choosen to be
# put into the queue once the button is pressed
words = ('Ouch!', 'Hey!', 'NOT AGAIN!', 'that hurts...', 'STOP IT')
def trigger():
q.put_nowait(random.choice(words))
Button(trigger, sprites_grp)
def callback(events):
# in the mainloop, we check for the QUIT event
# and kill the slave process if we want to exit
for e in events:
if e.type == pygame.QUIT:
slave.terminate()
slave.join()
return callback
return game_func
def second_display(screen, sprites_grp, q):
# we create font before the mainloop
font = pygame.freetype.SysFont(None, 48)
def callback(events):
try:
# if there's a message in the queue, we display it
word = q.get_nowait()
Message(screen.get_rect(), word, font, sprites_grp)
except:
pass
return callback
def main():
# we use the spawn method to create the other process
# so it will use the same method on each OS.
# Otherwise, fork will be used on Linux instead of spawn
mp.set_start_method('spawn')
q = mp.Queue()
slave = mp.Process(target=mainloop, args=(second_display, q))
slave.start()
mainloop(game(slave), q)
if __name__ == '__main__':
main()
当然,这只是一个简单的例子;您可能需要更多的错误处理,停止疯狂的嵌套功能,等等。此外,还有其他方法可以在python中进行IPC。
最后但并非最不重要的一点是,考虑是否真的需要两个pygame显示器,因为多处理会增加大量的复杂性。我已经在SO上回答了一些pygame问题,在询问有关pygame的线程/多处理的问题时,OP几乎总是问XY问题。