简体   繁体   English

如何在多个pygame应用之间切换焦点?

[英]How to switch focus between multiple pygame apps?

There are two games (A and B), on the left and right screen sides. 屏幕左右两侧有两个游戏(A和B)。 The game A responds to mouse clicks, the game B — to left/right keydowns. 游戏A响应鼠标单击,游戏B响应左/右键按下。 There is also a server which handles games behaviour: 还有一个处理游戏行为的服务器:

  • start(A), start(B) 开始(A),开始(B)
  • pause(A), pause(B) 暂停(A),暂停(B)
  • unpause(A) 暂停(A)
  • user clicks on window A, window A gets focus 用户单击窗口A,窗口A获得焦点
  • pause(A), unpause(B) 暂停(A),取消暂停(B)
  • user tries to press left/right keys, it obviously doesn't focus window B and I can't read keydown events from the game B 用户尝试按向左/向右键,显然它没有聚焦在窗口B上,并且我无法从游戏B中读取击键事件

I run games as two separate subprocesses (using subprocess.Popen I start a.py and b.py ). 我将游戏作为两个单独的子a.py运行(使用a.py我启动a.pyb.py )。 Where a.py and b.py are simple pygame games that listen my server events inside their game loop. 其中a.pyb.py是简单的pygame游戏,它们在游戏循环内监听我的服务器事件。

Is it possible to share event loop between two different pygame windowed apps? 是否可以在两个不同的pygame窗口化应用之间共享事件循环? Or may be it's better to change focus in some OS-specific way? 还是以某些特定于操作系统的方式更改焦点更好?

Okay, I have got a solution which works like a charm. 好的,我有一个像魅力一样的解决方案。

Since we have a server for handling some game events, we decided to grab a focus inside one game and share all keyboard events to other games through the server using web sockets. 由于我们有一台用于处理某些游戏事件的服务器,因此我们决定将重点放在一个游戏中,并使用Web套接字通过服务器将所有键盘事件共享给其他游戏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM