在Python中,如何让我的程序在5分钟后自动退出?
我正在为我的计算机科学课制作一个文本冒险代码。玩家在学校环境中,试图找出凶手是谁。我还没有完成代码,但是只有一个正确的结局,即玩家找到凶手,并且游戏可以根据玩家的需要重玩多次。我想给我的游戏设定一个时间限制,这样玩家只有 5 分钟的时间来找到找到凶手的正确方法。我该怎么做?有我应该下载的模块吗?这是我的代码:
school_map = """This is the map of your school.
-------------------------------------------
| classroom A | classroom B | classroom C |
|______ ______|_______ _____|______ ______|
| |
|------- ---------| |
| |--------- ---------|
| cafeteria | |
| | |
|___________________| gym |
| | |
| locker rooms |
|___________________|_____________________|
| ☘️
I am making a text adventure code for my computer science class. The player is in a school setting, trying to figure out who the murderer is. I have not finished the code yet, but there will be only one correct ending where the player finds the murderer and the game can be replayed as many times as the player wants. I want to set a time limit on my game, so the player only has 5 minutes to find the correct way to the murderer. How do I do that? Is there a module that I should download? This is my code:
school_map = """This is the map of your school.
-------------------------------------------
| classroom A | classroom B | classroom C |
|______ ______|_______ _____|______ ______|
| |
|------- ---------| |
| |--------- ---------|
| cafeteria | |
| | |
|___________________| gym |
| | |
| locker rooms |
|___________________|_____________________|
| ☘️ ???? ???? |
| ???? garden ???? |
| ???? ???? ???? |
-------------------------------------------
"""
answer_yes = ["yes", "Yes", "y", "Y", "YES"]
answer_no = ["no", "No", "n", "N", "NO"]
def get_choice(prompt) -> int:
while True:
choice = input(prompt).lower()
if choice == "x":
print(school_map)
elif choice in "12":
return int(choice)
else:
print("Value must be 1 or 2.")
while True:
print("WELCOME TO LIVRE DE LA MORT.")
print("*.*.*.*.*.*.*.*.*.*.*.*.*.*.")
print(" ")
name = input("What is your name? ")
print(f"You are a genius high school senior, {name}, whose goal in life is to bring justice to the world.")
print(school_map)
print("If you want to see the map again, input 'X' at any given time.")
print("You are skipping gym class in the locker rooms and looking out a window when you see a notebook fall from the sky.")
if get_choice(
"You can either: 1 - go and pick it up, or 2 - ignore it: "
) == 1:
print("You pick up the notebook and find instructions written on the inside.")
print("WELCOME.")
print("I AM THE LIVRE DE LA MORT.")
print("SINCE YOU HAVE PICKED ME UP, YOU ARE THE CHOSEN ONE.")
print("THERE IS A MURDERER AMONGST THIS SCHOOL. HE WRITES THE NAME OF ANY PERSON IN THIS NOTEBOOK, AND THEY DIE WITHIN THE NEXT 5 MINUTES.")
print("I WANT TO END THIS INJUSTICE AGAINST THE WORLD - AS THE CRIMINAL IS GAINING TOO MUCH POWER.")
if get_choice(
"WILL YOU: 1 - JOIN ME IN THIS BATTLE FOR JUSTICE AND HUNT DOWN THE MURDERER, or 2 - PUT ME BACK DOWN AND HAVE YOUR MEMORY WIPED? "
) == 1:
print("Good. We shall work together.")
print("There was a body found in either classroom A or Classroom B.")
if get_choice(
"You can either: 1 - check out classroom A, or 2 - check out Classroom B: "
) == 1:
print("You have entered classroom A.")
print("You have not found a body.")
if get_choice(
"Do you want to: 1 - check out classroom B, or 2 - go to gym class: "
) == 1:
print("You have entered Classroom B.")
print("You found a body of 16-year-old girl, Jessa Silverman!")
break
else:
print("You have entered the gym.")
print("In the corner, you see a group of people huddled around in a circle.")
break
else:
print("You have entered Classroom B.")
print("You found a body of 16-year-old girl, Jessa Silverman!")
break
else:
print("Your memory has been wiped.")
else:
print("You graduate high school and work a 8-6 job for the rest of your life as a police officer. ")
playagain = input("Would you like to play again? ")
while playagain:
if playagain in answer_yes:
print("ok.")
print(" ")
print("*.*.*.*.*.*.*.*.*.*.*.*.*.*.")
elif playagain in answer_no:
print("You are the weak link. Goodbye.")
exit()
else:
playagain = input("Would you like to play again? ")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
alarm
信号触发退出程序的处理程序。Use the
alarm
signal to trigger a handler that exits your program.