Pygame: Graphical Input + Text Input

2019-07-15 11:57发布

问题:

In Pygame, how can I get graphical input(e.g. clicking exit button) and also get input from the a terminal window simultaneously?

To give you context, my game has a GUI but gets its game commands from a "input()" command. How can I look for input from the command line while also handling graphics?

I'm not sure if this is possible, but if not, what other options do I have for getting text input from the user?

Thanks in advance.

回答1:

You can't do that, unless you use the input command in a different thread, but then you have to deal with syncronization (which might be what you want or don't want to do).

The way I'd implement this is to create a kind of in-game console. When a special key (e.g. '\') is pressed you make the console appear, and when your application is in that state you interpreter key pressing not as in-game commands but... well, as text. You can print them in the console (using fonts). When a key (e.g "return") is pressed you can make the console disappear and the keys take back their primary functionality.

I did this for my pet-project and it works as a charm. Plus, since you are developing in python you can accept python instructions and use exec to execute them and edit your game "on fhe fly"