测验游戏的线程计时器
我必须与Python一起在大学里进行一些练习。因此,我需要构建测验游戏。 And the requirements are:
- Right answer get +1 point.
- 错误的答案获取-1
播放器必须在20秒内回答每个问题。如果玩家以20秒的时间回答,那么他的回答是正确的,他得到-1点。
我只是尝试了螺纹
和计时器
。我的代码就是这样:
from time import *
import threading as th
score = 0
correct_answer = 1
def sctn():
global score
score -=1
print(score)
S = th.Timer(20.0, sctn)
S.start()
answer = int(input("enter : "))
if answer == correct_answer :
score += 1
else:
score -= 1
print(score)
S.cancel()
我为正确答案设定了一个示例。但是,当我运行代码时,如果我键入正确的答案超过20秒,则得分= -1 + 1 = 0,或者如果我输入错误的答案超过20秒,则分数= -1 -1 = -2。
I have to do some exercises in my university with Python.So I need to build a quiz game. And the requirements are:
- Right answer get +1 point.
- Wrong answer get -1 point
The player has to answer each question in 20 seconds. If the player answers with more than 20 seconds, he gets -1 point even his answer is correct.
I just tried with threading
and Timer
. My code is like this:
from time import *
import threading as th
score = 0
correct_answer = 1
def sctn():
global score
score -=1
print(score)
S = th.Timer(20.0, sctn)
S.start()
answer = int(input("enter : "))
if answer == correct_answer :
score += 1
else:
score -= 1
print(score)
S.cancel()
I set an example for the correct answer. But when I run the code, if I type the correct answer more than 20 seconds, after that the score = -1 + 1 = 0, or if I type the wrong answer more than 20 seconds, the score = -1 -1 = -2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码太多。有两种情况:如果用户在20秒或更短的时间内输入了正确的答案,他会得到一个观点;否则他输了一个。
计时器函数无需执行任何操作。测试正确的答案时,还可以测试以查看计时器是否过期。计时器是线程的一个子类,因此它具有函数
is_alive
,如果仍在运行,则返回true。原始程序的问题是,一旦计时器到期,答案就不再重要了。但是您无论如何都会检查它,因此从计时器函数中的-1点会从答案检查逻辑中添加到plus或负1中。
You have too much code. There are two cases: if the user has entered the correct answer in 20 seconds or less, he gets a point; otherwise he loses one.
The timer function doesn't need to do anything. When you test for the correct answer, you can also test to see if the timer has expired. Timer is a subclass of Thread, therefore it has an function
is_alive
that returns True if it's still running.The problem with your original program is that once the timer expires, the answer doesn't matter any more. But you check it anyway, so the -1 point from the timer function gets added to the plus or minus 1 from the answer-checking logic.
在显示问题时,您可以使用方法后使用方法来启动计时器,并在用户未及时响应时扣除分数。如果用户及时响应,则可以用 的返回值调用
取消作业。
例如,当您想启动计时器时,您将执行类似的操作:
这是您具有名为
root
的全局变量,该变量指的是root窗口。不过,您可以使用任何小部件。它的作用是指示tkinter在20秒内(20,000毫秒)在该功能中调用函数
times_up
您可以将分数降低一个,例如:然后,在处理用户选择一个的代码中答案,您可以停止计时器并防止分数下降:
如果用户选择答案时调用此功能,则它将计算新的分数并防止计时器触发。
You can use the
after
method of tkinter to start a timer when a question is shown, and deduct a score if the user hasn't responded in time. If the user responds in time you can callafter_cancel
with the return value ofafter
to cancel the job.For example, when you want to start the timer you would do something like this:
This assumes you have a global variable named
root
which refers to the root window. Though, you can use any widget for this. What it does is instruct tkinter to call the functiontimes_up
in 20 seconds (20,000 milliseconds)In that function you can reduce the score by one, for example:
Then, in the code that processes the user picking an answer, you can stop the timer and prevent the score from doing down:
If this function is called when the user picks an answer, it will compute the new score as well as prevent the timer from ever triggering.