I am building a single player MUD, which is basically a text-based combat game. It is not networked.
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.
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.