我正在为学校制作一个简单的基于文本的python游戏,如何在其中加入计时器


def bear_room():
print("nthere's a bear here")
print("nthe bear has a bunch of honey")
print("nthe fat bear is front of another door")
print("nhow are you going to move the bear?")
choice = input("nnTaunt bear, take honey, open door?: ")
if choice == "take honey":
print("nthe bear looks at you then slaps your face off")
elif choice == "open door":
print("nget the hell out")
elif choice == "Taunt bear":
print("n*Bear rips your heart out*")
else:
print("nInvalid entry")
bear_room()

bear_room()

我正在为学校制作一个简单的基于文本的python游戏,如何在其中加入计时器?我希望它是一个10秒倒计时

您可以引入另一个名为timer的函数。此函数将使用Python中的时间模块。计时器的代码为:

def timer(t):#t must be the time of the timer in seconds
while t:
mins,sec=divmod(t,60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print(timer, end='r')
time.sleep(1)
t=t-1
print("Time's Up")

这个代码就可以了。

如果你想在等待时也以秒为单位打印倒计时,那么@Shuvam Paul的答案就是这样做的方法;但如果你只需要等待10秒而不做任何其他事情,那么只需要标准的time.sleep()函数就足够了——这就是产生暂停的原因。

import time
def bear_room():
print("nthere's a bear here")
print("nthe bear has a bunch of honey")
print("nthe fat bear is front of another door")
print("nhow are you going to move the bear?")
time.sleep(10) # Shift this line to wherever you want the 10 sec delay, if not here
choice = input("nnTaunt bear, take honey, open door?: ")
if choice == "take honey":
print("nthe bear looks at you then slaps your face off")
elif choice == "open door":
print("nget the hell out")
elif choice == "Taunt bear":
print("n*Bear rips your heart out*")
else:
print("nInvalid entry")
bear_room()
bear_room()

最新更新