我正在写我自己的游戏,在你的头脑中进行一些心理操作,比如加减乘除。现在,游戏运行顺利,加减运算,但我现在想问用户他/她的猜测,但如果他/她延迟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
欢迎所有建议,并随时修改我的代码:)
见this post。在
您当前的while循环不起作用;这并不是真正正确地使用它,它只是无休止地等待您提供输入。在您的例子中,我认为一个好的方法是定义一个函数来询问用户每次递归调用的答案线程。计时器到期。一些示例代码:
我相信这会使函数在计时器过期时重新启动,或者在响应(无论正确与否)时重新启动。具体细节显然取决于你。在
为打字而编辑。在
然后将所有的内容重新打印到屏幕上,输入信息除外。在
另一种清除屏幕的方法是:
^{pr2}$但您需要检查您定义的变量是否在此之后仍然保留。在
关于禁用
raw_input
函数,请检查post。在多亏了@aenda和@lennon310的建议,并对以下帖子做了一些研究:Non-blocking raw_input code from Gary Robinson,Raw input and timeout和{a3}我能够做一些细微的修改,并生成以下代码来解决我的问题。在
相关问题 更多 >
编程相关推荐