测验游戏的线程计时器

发布于 2025-02-01 22:51:27 字数 669 浏览 3 评论 0原文

我必须与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2025-02-08 22:51:27

您的代码太多。有两种情况:如果用户在20秒或更短的时间内输入了正确的答案,他会得到一个观点;否则他输了一个。

计时器函数无需执行任何操作。测试正确的答案时,还可以测试以查看计时器是否过期。计时器是线程的一个子类,因此它具有函数is_alive,如果仍在运行,则返回true。

原始程序的问题是,一旦计时器到期,答案就不再重要了。但是您无论如何都会检查它,因此从计时器函数中的-1点会从答案检查逻辑中添加到plus或负1中。

from time import *
import threading as th
score = 0

correct_answer = 1
def sctn():
    pass
S = th.Timer(20.0, sctn)  
S.start()  
answer = int(input("enter : "))

if answer == correct_answer and S.is_alive():
    score += 1
else:
    score -= 1
S.cancel()
print(score)

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.

from time import *
import threading as th
score = 0

correct_answer = 1
def sctn():
    pass
S = th.Timer(20.0, sctn)  
S.start()  
answer = int(input("enter : "))

if answer == correct_answer and S.is_alive():
    score += 1
else:
    score -= 1
S.cancel()
print(score)
一桥轻雨一伞开 2025-02-08 22:51:27

在显示问题时,您可以使用方法后使用方法来启动计时器,并在用户未及时响应时扣除分数。如果用户及时响应,则可以用 的返回值调用取消作业。

例如,当您想启动计时器时,您将执行类似的操作:

def start_timer():
    global after_id
    root.after(20000, times_up)

这是您具有名为root的全局变量,该变量指的是root窗口。不过,您可以使用任何小部件。它的作用是指示tkinter在20秒内(20,000毫秒)

在该功能中调用函数times_up您可以将分数降低一个,例如:

def times_up():
    global score
    score += 1

然后,在处理用户选择一个的代码中答案,您可以停止计时器并防止分数下降:

def user_picked_an_answer():
    global after_id
    root.after_cancel(after_id)

如果用户选择答案时调用此功能,则它将计算新的分数并防止计时器触发。

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 call after_cancel with the return value of after to cancel the job.

For example, when you want to start the timer you would do something like this:

def start_timer():
    global after_id
    root.after(20000, times_up)

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 function times_up in 20 seconds (20,000 milliseconds)

In that function you can reduce the score by one, for example:

def times_up():
    global score
    score += 1

Then, in the code that processes the user picking an answer, you can stop the timer and prevent the score from doing down:

def user_picked_an_answer():
    global after_id
    root.after_cancel(after_id)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文