如何用倒计时停止显示输入用户猜测

2024-07-04 04:57:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写我自己的游戏,在你的头脑中进行一些心理操作,比如加减乘除。现在,游戏运行顺利,加减运算,但我现在想问用户他/她的猜测,但如果他/她延迟5秒,输入信息应该消失,正确的结果应该出现,另一个操作应该出现。以下是游戏代码:

def level_one():
    goodGuess=0
    badGuess=0
    time_easy_level = default_timer()
    numbers_with_operators=[]
    local_operators_easy=["+","-"]
    global continuePlaying
        #====Repeat operations 5 times=================
    for x in range(5):
        #===10 random numbers between 1 and 10 are generated =========
        easy_level=[randint(1,10) for i in range(1,10)]
            #===Each list of random numbers is appended with a random operator===
        for item in easy_level:
            numbers_with_operators.append(item)
            time.sleep(1)
            numbers_with_operators.append(local_operators_easy[randint(0,1)])
            if len(numbers_with_operators)==18:
                numbers_with_operators.append(randint(1,10))
            print numbers_with_operators
        time_for_guess=time.time()
        deadline_for_guess=time_for_guess+5
        while time_for_guess<deadline_for_guess:
            user_guess=int(raw_input("What is the result? "))
            break
        computer_result=compute_list(numbers_with_operators)
        if user_guess==computer_result:
            goodGuess+=1
            print "Good guess!"
        else:
            print "Sorry, that is not the result"
            badGuess+=1
            print computer_result
        del numbers_with_operators[:]
    duration=default_timer()-time_easy_level
    continuePlaying=False
    print "Your results are: \n"
    print "Good guesses: "+str(goodGuess)+"\nBad guesses: "+str(badGuess)
    print "Total seconds playing:\n"+str(duration)+" seconds"
    return continuePlaying

欢迎所有建议,并随时修改我的代码:)


Tags: in游戏fortimewitheasyresultlevel
3条回答

this post。在

您当前的while循环不起作用;这并不是真正正确地使用它,它只是无休止地等待您提供输入。在您的例子中,我认为一个好的方法是定义一个函数来询问用户每次递归调用的答案线程。计时器到期。一些示例代码:

import threading
def guess_a_number():
    #generate random numbers
    #generate operators
    timer = threading.Timer(5.0, guess_a_number())
    timer.start()
    #get user input
    #check if user input is correct
    if True:
        print "You were right!"
        guess_a_number()
    else:
        print "Sorry, you were wrong."
        guess_a_number()

我相信这会使函数在计时器过期时重新启动,或者在响应(无论正确与否)时重新启动。具体细节显然取决于你。在

为打字而编辑。在

N = 100 # you might need to adjust this
for i in range(N):
    print "\n"

然后将所有的内容重新打印到屏幕上,输入信息除外。在

另一种清除屏幕的方法是:

^{pr2}$

但您需要检查您定义的变量是否在此之后仍然保留。在

关于禁用raw_input函数,请检查post。在

多亏了@aenda和@lennon310的建议,并对以下帖子做了一些研究:Non-blocking raw_input code from Gary RobinsonRaw input and timeout和{a3}我能够做一些细微的修改,并生成以下代码来解决我的问题。在

timeout=7

class AlarmException(Exception):
    pass

def alarmHandler(signum,frame):
    raise AlarmException

def my_raw_input(prompt,timeout):
    signal.signal(signal.SIGALRM,alarmHandler)
    signal.alarm(timeout)
    try:
        userGuess=int(raw_input(prompt))
        signal.alarm(0)
        return userGuess
    except AlarmException:
        print "Uh-oh! Time's for that one!"
    signal.signal(signal.SIGALRM,signal.SIG_IGN)
    return ''

def level_one():
    goodGuess=0
    badGuess=0
    time_easy_level = default_timer()
    numbers_with_operators=[]
    local_operators_easy=["+","-"]
    global continuePlaying
    global timeout
    #====Repeat operations 5 times=================
        for x in range(5):
        #===10 random numbers between 1 and 10 are generated =========
            easy_level=[randint(1,10) for i in range(1,10)]
            #===Each list of random numbers is appended with a random operator===
            for item in easy_level:
                numbers_with_operators.append(item)
                time.sleep(1)
                numbers_with_operators.append(local_operators_easy[randint(0,1)])
                    if len(numbers_with_operators)==18:
                        numbers_with_operators.append(randint(1,10))
                print numbers_with_operators
                user_guess = my_raw_input("What's the result? ",timeout)
                computer_result=compute_list(numbers_with_operators)
                if user_guess==computer_result:
                    goodGuess+=1
                    timeout=7
                    print "Good guess!"
                else:
                    print "Sorry, that is not the result"
                    badGuess+=1
                    timeout=7
                    print computer_result
              print "That was the number operation "+str(x)
              del numbers_with_operators[:]
        duration=default_timer()-time_easy_level
        continuePlaying=False
        print "Your results are: \n"
        print "Good guesses: "+str(goodGuess)+"\nBad guesses: "+str(badGuess)
        print "Total seconds playing:\n"+str(duration)+" seconds"
        return continuePlaying

相关问题 更多 >

    热门问题