异步获取用户输入并传递给python中的事件循环 [英] Grab user input asynchronously and pass to an Event loop in python

查看:160
本文介绍了异步获取用户输入并传递给python中的事件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个单人游戏MUD,它基本上是一个基于文本的格斗游戏.它没有联网.

I am building a single player MUD, which is basically a text-based combat game. It is not networked.

我不明白如何收集用户命令并将它们异步传递到我的事件循环中.游戏事件触发时,玩家需要能够随时输入命令.因此,使用raw_input暂停过程将无法进行.我想我需要做类似select.select和使用线程的事情.

I don't understand how to gather user commands and pass them into my event loop asynchronously. The player needs to be able to enter commands at any time as game events are firing. So pausing the process by using raw_input won't work. I think I need to do something like select.select and use threads.

在下面的示例中,我有一个userInputListener()的模型函数,该函数是我喜欢接收命令的地方,并将它们附加到命令Que(如果有输入的话).

In the example below, I have a mockup function of userInputListener() which is where I like to receive commands, and append them to the command Que if there is input.

如果有事件循环,例如:

If have an event loop such as:

from threading import Timer
import time

#Main game loop, runs and outputs continuously
def gameLoop(tickrate):

    #Asynchronously get some user input and add it to a command que 
    commandQue.append(userInputListener())
    curCommand = commandQue(0)
    commandQue.pop(0)

    #Evaluate input of current command with regular expressions
    if re.match('move *', curCommand):
        movePlayer(curCommand)
    elif re.match('attack *', curCommand):
        attackMonster(curCommand)
    elif re.match('quit', curCommand):
        runGame.stop()
    #... etc    

    #Run various game functions...
    doStuff()

    #All Done with loop, sleep
    time.sleep(tickrate)

#Thread that runs the game loop
runGame = Timer(0.1, gameLoop(1))
runGame.start()

如何在其中输入用户输入?

How do I get my user input in there?

或更简单地说,有人可以在同时运行另一个循环的同时给我展示存储用户输入的任何示例吗?如果我们能做到的话,我可以弄清楚其余的事情.

Or more simply, can anyone show me any example of storing user input while another loop is running at the same time? I can figure out the rest if we can get that far.

推荐答案

您确实需要两个线程.一种与主游戏循环有关,另一种与用户输入有关.两者将通过队列进行通信.

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.

这篇关于异步获取用户输入并传递给python中的事件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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