2

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.

4
  • What sort of input are we talking about? Just simple text entry? Commented Nov 19, 2012 at 10:00
  • Plain text entry into a terminal window. Commented Nov 19, 2012 at 10:02
  • 1
    If the stuff you're trying to collect is not too complex, you might want to just roll your own. Put keyboard events in a queue, and then collect them all when a trigger, such as the return key, is pressed. Commented Nov 19, 2012 at 10:03
  • You can make a "console" in-game. Commented Nov 19, 2012 at 10:11

1 Answer 1

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"

Sign up to request clarification or add additional context in comments.

6 Comments

At this stage I'm not sure which way I want to go. Since it will be a multiplayer game with 2 clients accessing a server, I'm not sure if I'll need threading to handle that part while still running the game. I probably should have stated earlier that I'm relatively new to Python.
I strongly suggest to not use threads for the input. At the very least you'll need to create a new window just to manage the input, since you can't use the main thread for that (the main thread is busy rendering stuff).
if it suits your need you could implement the console as another "client": after all the network stuff will be already in place and you just need another connection to accept strings...
I think I will try implementing the "in-game console". Now for figuring out how the hell I get 2 clients to access a server and update the server while getting the game state from it and keeping the 2 clients in sync.
Oh, one quick thing. Is there anyway I could see how you implemented your in-game console? Like some of your code or something.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.