所以我正在为我在计算机科学课上的基础知识做一个文字冒险游戏,我有一个房间,你必须猜出一个介于1到100之间的数字,你有20秒的时间,否则你就死了,我可以让计时器说它已经启动,但是一旦计时器命中0,什么都不会发生。我已经分别尝试了计时器代码,它运行得很好--我可能只是误解了python的一些基本内容,很可能,下面是代码的一部分--抱歉,这可能是很长的一段时间
timer_1 = 20
random1 = random.randrange(1,100)
def random_number_1():
print("as you take the artifact you notice that under the artifact on the pedistool is a dial with the numbers 1-100 on it and below that is a engraving with the number 35 on it")
time.sleep(3)
print()
print(Fore.RED + "Water begins to fill the room and the opening to the artifacts room is suddenly blocked by a large pillar")
time.sleep(1.5)
print()
print(Fore.RED + "*QUICK GET THE DIAL TO THE CORRECT NUMBER TO STOP YOURSELF FROM DROWNING*")
timer()
guess = int(input(Fore.WHITE + 'Move the dial to a number between 1 and 100'))
if guess != random1:
if guess < random1:
print("The number was to small try moving the dial again ")
guess = int(input('Move the dial to a number between 1 and 100'))
if guess > random1:
print("The number was to big try moving the dial again")
guess = int(input('Move the dial to a number between 1 and 100'))
print('The dial clicks and the opening from whence you came opens, you run out as fast as possible as water rises from the depths of the artifacts chamber')
def timer():
print(Fore.RED + "timer has started")
global timer_1
if timer_1 != -1:
time.sleep(1)
timer_1 = (timer_1 - 1)
if timer_1 == -1:
pause()
print(Fore.BLUE + 'you are dead')
return发布于 2022-03-13 01:31:41
要做到这一点,就像您想象的那样,需要多线程和中断input()调用的能力(或者使用替代input()的方法,比如keyboard模块,这样您就可以更好地控制)。
不过,我建议不要使用实际的多线程计时器,只需跟踪时间,并在每次input()调用完成后检查它--您将无法在用户输入时中断用户,但在用户按Enter之后,您将能够告诉他们他们是否足够快。下面是一个示例实现,您可以使用它并尝试将其合并到您自己的代码中:
import datetime
import random
deadline = datetime.datetime.now() + datetime.timedelta(seconds=20)
target = random.randrange(100)
print("Water begins to fill the room...")
print("Quick, get the dial to the correct number!")
while True:
try:
guess = int(input("Move the dial to a number between 1 and 100: "))
except ValueError:
print("Butterfingers!")
continue
remaining = (deadline - datetime.datetime.now()).total_seconds()
if remaining < 0:
print("Too late! You died!")
break
if guess < target:
print("Too low!")
elif guess > target:
print("Too high!")
else:
print("You got it!")
break
print(f"{remaining:.0f} seconds left before you drown!")https://stackoverflow.com/questions/71453750
复制相似问题