【发布时间】:2019-05-23 09:47:00
【问题描述】:
我刚刚进入 python 并正在尝试创建一个问答游戏。我想在我的测验中添加一个 2 分钟的倒数计时器,这样当计时器达到零时,问题就会停止并说你没时间了。我也试过用秒表来做这件事,但不知道如何终止游戏,所以如果你知道怎么做,那也一样好。
注意:我没有使用 pygame。许多其他用户在之前的帖子中提出过问题。
这是我从其他文档中收集的代码,但它不会在达到 2 分钟或 0 分钟后停止。
def countdown():
t = 60
while t:
mins, secs = divmod(t, 60)
timeformat = '{:02d}:{:02d}'.format(mins, secs)
print(timeformat, end='\r')
time.sleep(1)
t -= 1
print("You're out of time!\n")
sys.exit ("goodbye")
def main_game():
count_thread = threading.Thread(None, countdown)
question_4 = ("question 1?")
option_4 = (" a. 1 \n b. 5 \n c. 3 \n d. 7")
print(question_4)
print(option_4)
answer_4 = input (">")
if answer_4.lower()== "b":
score = score + 100
else:
print("Incorrect")
import time
start1 = time.time()
if start1 >= 120:
import sys
sys.exit
question_a = ("question?")
option_a = (" a. 7 \n b. 8 \n c. 2 \n d. 1")
print(question_a)
print(option_a)
answer_a = input (">")
if answer_a.lower()== "b":
score = score + 100
else:
print("Incorrect")
###rest of questions
end1 = time.time()
这两个代码都是我尝试过的两个不同版本。两个代码都不起作用。底部代码对播放进行计时,但不会在 2 分钟标记处停止。
我们将不胜感激任何帮助、建议或反馈!
【问题讨论】:
-
你必须使用
count_thread.run()来运行线程。 -
查找函数
getchar()- 它不会阻塞控制台,因此您可以创建检查键盘和检查时间的循环 - 它不需要线程。但它不是标准功能:stackoverflow.com/questions/2408560/… -
我会在哪里添加这个?
-
您必须在创建
count_thread之后使用run()并在使用input()之前使用它来阻止代码。
标签: python python-3.x pygame