pygame:图形输入+文本输入 [英] Pygame: Graphical Input + Text Input

查看:46
本文介绍了pygame:图形输入+文本输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Pygame中,如何获取图形输入(例如,单击退出按钮),并同时从终端窗口获取输入?

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

为了给您提供背景信息,我的游戏具有GUI,但从"input()"命令获取其游戏命令.如何在处理图形的同时从命令行查找输入?

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?

谢谢.

推荐答案

除非您在其他线程中使用 input 命令,否则您将无法做到这一点,但是您必须处理同步(这可能是您想要或不想要的).

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.

我是为我的宠物项目做的,它很吸引人.另外,由于您使用python开发,因此您可以接受python指令并使用 exec 来执行它们,并即时"编辑游戏

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"

这篇关于pygame:图形输入+文本输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆