简体   繁体   中英

How to make a timer run concurrently in python

I am going through Zed Shaw's Learn Python the Hard Way and as one of the exercise we are asked to create a game. I wanted to create a rhyming game that would print out a word and the player had 5 seconds to guess a word that rhymed with the word printed out.

I was able to get the game to work without a timer. However, I can't figure out how to make the timer run concurrently with the game. Below is the code:

#libraries
libs = [about, cake, chart, score]
#points 
p = 0
#timer
class countdown(object):

    def __init__(self, timer):
        self.timer = timer

        while timer != 0:

            print "timer: %i" % timer
            sleep(1)
            timer -= 1

        else:
            exit("You ran out of time!")

class engine(object):
    # randomly select a library    
    i = randint(0, len(libs)-1)

    randlib = libs[i].words.split(' ')     

    rhymeword = sample(randlib, 1)

    print rhymeword
    guess = raw_input("> ")

    while guess not in randlib:

        print rhymeword
        guess = raw_input("> ")

    else:
        # to reset the timer    
        timer = 5
        # points assignment
        if guess in libs[i].onesyl.split(' '):
            p += 1
            print "Points: %s" % p
        elif guess in libs[i].twosyl.split(' '):
            p += 2
            print "Points: %s" % p
        elif guess in libs[i].threesyl.split(' '):
            p += + 3
        print "Points: %s" % p

t = countdown(5)
e = engine()

t.start()
e.start()

What I would like to do is have the timer counting down while the user tries to guess and then have the timer restart once a correct answer is enter.

I looked into multiprocessing in the python documentation, but I wasn't sure how to make that work.

really this is bad design if you want a timer like this you should look into a gui or at least curses ... your output likely wont look how you hope

all that said I guess this is roughly the behaviour you want

import sys,time,threading

def wait_for_secs(N):
    for i in range(N):
        print N-i
        time.sleep(1)
    print "OUT OF TIME!!!"
    sys.exit(1)

threading.Thread(target=wait_for_secs,args=(15,)).start()
answer = raw_input("5+6*3=? >")
print "You Answered:",answer
sys.exit(0)

You can use the standard module signal to make a simple timer. Here is an example :

import signal, sys

def handler(signum, frame):
    #print 'Signal handler called with signal', signum
    print "You ran out of time!"
    sys.exit()


def engine():    
    guess = raw_input("> ")
    print "fast enough to input",guess

# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, handler)
signal.alarm(5)

engine()

signal.alarm(0)          # Disable the alarm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM