更新时间:2022-06-08 22:40:32
您确实需要两个线程.一种与主游戏循环有关,另一种与用户输入有关.两者将通过队列进行通信.
You will indeed need two threads. One to be concerned with the main game loop and one to handle user input. The two would be communicating through a Queue.
您可以让您的主进程启动游戏循环线程,然后让其从用户那里获取一行文本并将其放入"队列(即紧随runGame.start()之后).这可以很简单:
You can have your main process start the game loop thread and then have it obtaining a line of text from the user and "puting" it to the queue (i.e following runGame.start()). This can be as simple as:
while not gameFinished:
myQueue.put(raw_input()).
游戏循环线程将只是从队列中获取"一行文本,进行交互并执行它.
The game loop thread will simply be "geting" a line of text from the queue, interpert and execute it.
Python具有线程安全的队列实现,您可以使用它(包括一个非常安全的您可以将其用作多线程用法的基本示例).
Python has a thread-safe queue implementation which you can use (including a very basic example for multi-threaded usage that you can use as a guide).
还有一个简单的命令行插入器模块( cmd模块和
There's also a simple command line interperter module (the cmd module and here for a good practical overview) which might also be useful for this kind of project.