简体   繁体   中英

How to switch focus between multiple pygame apps?

There are two games (A and B), on the left and right screen sides. The game A responds to mouse clicks, the game B — to left/right keydowns. There is also a server which handles games behaviour:

  • start(A), start(B)
  • pause(A), pause(B)
  • unpause(A)
  • user clicks on window A, window A gets focus
  • pause(A), unpause(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

I run games as two separate subprocesses (using subprocess.Popen I start a.py and b.py ). Where a.py and b.py are simple pygame games that listen my server events inside their game loop.

Is it possible to share event loop between two different pygame windowed apps? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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