python - 我如何添加一个倒计时的计时器,当它用完时它会重新启动我的程序
问题描述
我是一个非常新的初学者编码器,我还不太了解。我正在尝试创建一个猜谜游戏,并且想在程序中添加一个计时器。计时器的工作方式是我设置了一个固定的时间,即 60 秒,一旦时间用完,程序就会停止,基本上就像“对不起,你没时间了。你想再试一次吗? "
我已经在 YouTube 和 Stackoverflow 上搜索了一些视频,但我要么不明白,要么无法做到。
https://www.youtube.com/watch?v=Mp6YMt8MSAU
我已经看了 4 次这个视频,但仍然不知道该怎么做。因此,如果有人愿意向我解释/告诉我如何增加时间,那将不胜感激
def main():
import random
n = random.randint(1, 99)
chances = 5
guess = int(input("Player 2 please enter an integer from 1 to 99, you have 5 chances: " ))
while n != "guess":
chances -=1
if chances ==0:
print("out of chances, it is now player 2's turn to play. The number was", n)
break
if guess < n:
print("guess is low you have",chances,"chances left")
guess = int(input("Enter an integer from 1 to 99: "))
elif guess > n:
print ("guess is high you have",chances, "chances left")
guess = int(input("Enter an integer from 1 to 99: "))
else:
print("you guessed it and have", chances, "chances left")
break
import random
n1 = random.randint(1, 99)
chances1 = 0
guess1 = int(input("Player 2 please enter an integer from 1 to 99, you have 5 chances: "))
while n1 != "guess":
chances1 +=1
if chances1 ==5:
print("out of chances, the number was", n1)
break
if guess1 < n1:
print("guess is low you have",chances1,"chances left")
guess1 = int(input("Enter an integer from 1 to 99: "))
elif guess > n1:
print ("guess is high you have",chances1, "chances left")
guess1 = int(input("Enter an integer from 1 to 99: "))
else:
print("you guessed it and have", chances1, "chances left")
break
retry=input("would you like to play again? (please choose either 'yes' or 'no' )")
if retry == "yes":
main()
elif retry == "no":
print("Okay. have a nice day! :D ")
else:
print("Invalid input")
main()
这是我的代码。对于任何反馈,我们都表示感谢。
解决方案
要解决您的问题,您必须使用Thread
. 我拿了你的代码我在开始时添加了线程,试着看看。
线程是可以run
在主程序旁边执行函数(其函数)的对象。这就是所谓的并行编程。
在这里你需要它,因为你想:
- 检查播放器的输入
- 倒数
我不会更正您的代码或重写它,我只是添加了线程并使其工作。这很容易:
- 我要求
sleep
给定的时间 (_TIME_LIMIT
] - 计时器结束时发出一点声音。
- 我将变量设置为
True
,这意味着计时器结束
通过一些重写,您将能够在 Thread 更改变量时中断主循环。但这超出了最初问题的范围,请随时改进您的游戏。
目前,您需要输入一个数字才能知道时间是否结束。这就是我添加小噪音的原因(如果没有,您将没有任何提示)。
请随时提出任何问题。
希望这会帮助你。
from threading import Thread
from time import sleep
import random
_TIME_OUT = [False, False]
_TIME_LIMIT = 1
class MyThread(Thread):
def __init__(self, time_out, player):
Thread.__init__(self)
self.time_out = time_out
self.player = player
def run(self):
sleep(self.time_out)
_TIME_OUT[self.player - 1] = True
print('\a', end="") # Make a noise
def main():
_TIME_OUT[0] = False
_TIME_OUT[1] = False
tread_1 = MyThread(_TIME_LIMIT, 1)
n = random.randint(1, 99)
chances = 5
tread_1.start()
print(f"You have {_TIME_LIMIT} sec")
guess = int(input("Player 1 please enter an integer from 1 to 99, you have 5 chances: " ))
while n != "guess" and not _TIME_OUT[0]:
print(not _TIME_OUT, _TIME_OUT)
chances -=1
if chances == 0:
print("out of chances, it is now player 2's turn to play. The number was", n)
break
if guess < n:
print("guess is low you have",chances,"chances left")
guess = int(input("Enter an integer from 1 to 99: "))
elif guess > n:
print ("guess is high you have",chances, "chances left")
guess = int(input("Enter an integer from 1 to 99: "))
else:
print("you guessed it and have", chances, "chances left")
break
if _TIME_OUT[0]:
print("Sorry, out of time!")
tread_2 = MyThread(_TIME_LIMIT, 2)
n1 = random.randint(1, 99)
chances1 = 0
tread_2.start()
print(f"You have {_TIME_LIMIT} sec")
guess1 = int(input("Player 2 please enter an integer from 1 to 99, you have 5 chances: "))
while n1 != "guess" and not _TIME_OUT[1]:
chances1 +=1
if chances1 ==5:
print("out of chances, the number was", n1)
break
if guess1 < n1:
print("guess is low you have",chances1,"chances left")
guess1 = int(input("Enter an integer from 1 to 99: "))
elif guess > n1:
print ("guess is high you have",chances1, "chances left")
guess1 = int(input("Enter an integer from 1 to 99: "))
else:
print("you guessed it and have", chances1, "chances left")
break
if _TIME_OUT[1]:
print("Sorry, out of time!")
retry=input("would you like to play again? (please choose either 'yes' or 'no' )")
if retry == "yes":
main()
elif retry == "no":
print("Okay. have a nice day! :D ")
else:
print("Invalid input")
if __name__ == "__main__":
main()
推荐阅读
- apache-spark - 如何使用 Databricks S3-SQS 连接器读取结构化流中的 SQS 消息?
- three.js - 我似乎无法在 Three.js 场景中使用阴影
- image - 如何将图像侧添加到导航栏中的后退按钮
- excel-formula - 如何在 excel/libreoffice calc 中将单元格值除以整行值?
- sql - 如果数据已经存在,我需要更新它并在某些列中添加旧值和新值,但它在第 8 行显示语法错误
- python - 将 LSTM 与具有嵌入层的并行 MLP 网络连接起来
- vuejs2 - 获取 vue-upload-component 上传图片的宽/高
- elasticsearch - Elasticsearch Curator 在按过滤器类型删除索引时不删除最后一个索引
- c# - Unity:如何让 update() 函数等待几秒钟
- scala - 如何流式传输调度程序的响应?