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

查看:22
本文介绍了异步抓取用户输入并传递给 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).

还有一个简单的命令行 interperter 模块(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天全站免登陆