基本的にテキストベースの戦闘ゲームであるシングルプレイヤー MUD を構築しています。ネットワーク化されていません。
ユーザー コマンドを収集してイベント ループに非同期で渡す方法がわかりません。ゲーム イベントが発生している間、プレイヤーはいつでもコマンドを入力できる必要があります。そのため、raw_input を使用してプロセスを一時停止しても機能しません。select.select のようなことをしてスレッドを使用する必要があると思います。
以下の例では、userInputListener() のモックアップ関数を使用しています。ここでコマンドを受け取り、入力がある場合はそれらをコマンド Que に追加します。
次のようなイベント ループがある場合:
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()
そこにユーザー入力を取得するにはどうすればよいですか?
もっと簡単に言えば、別のループが同時に実行されている間にユーザー入力を保存する例を誰か教えてもらえますか? そこまで行けば残りはわかる。