Grab user input asynchronously and pass to an Event loop in python -


i building single player mud, text-based combat game. not networked.

i don't understand how gather user commands , pass them event loop asynchronously. player needs able enter commands @ time game events firing. pausing process using raw_input won't work. think need select.select , use threads.

in example below, have mockup function of userinputlistener() receive commands, , append them command que if there input.

if have event loop such as:

from threading import timer import time  #main game loop, runs , outputs continuously def gameloop(tickrate):      #asynchronously user input , add command que      commandque.append(userinputlistener())     curcommand = commandque(0)     commandque.pop(0)      #evaluate input of current command 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 loop, sleep     time.sleep(tickrate)  #thread runs game loop rungame = timer(0.1, gameloop(1)) rungame.start() 

how user input in there?

or more simply, can show me example of storing user input while loop running @ same time? can figure out rest if can far.

you indeed need 2 threads. 1 concerned main game loop , 1 handle user input. 2 communicating through queue.

you can have main process start game loop thread , have obtaining line of text user , "puting" queue (i.e following rungame.start()). can simple as:

while not gamefinished:     myqueue.put(raw_input()).  

the game loop thread "geting" line of text queue, interpert , execute it.

python has thread-safe queue implementation can use (including basic example multi-threaded usage can use guide).

there's simple command line interperter module (the cmd module , here practical overview) might useful kind of project.


Comments

Popular posts from this blog

How do you change vbulletin's home page to the forums instead of the default (CMS or Activity Stream)? -

c++ - Troubles when compiling phash program -