python - Python(linux)基于文本的游戏输入错误
问题描述
我一直在开发基于文本的冒险游戏。当我尝试创建 EVENTS 而不是简单地依赖大量的 PRINT 字符串时,我已经对其进行了几次修改,但似乎无法得到我想要的结果。每当我选择我想要的选项(在这种情况下为门 1),然后是以下选项,输入无响应或给我一个错误。下面是门 1 的部分代码。稍微清楚一点将不胜感激!
def main():
import sys
from colorama import init
init()
init(autoreset=True)
from colorama import Fore, Back, Style
def run_event(event):
text, choices = event
text_lines = text.split("\n")
for line in text_lines:
print(Style.BRIGHT + line)
print()
choices = choices.strip()
choices_lines = choices.split("\n")
for num, line in enumerate(choices_lines):
print(Fore.GREEN + Style.BRIGHT + str(num + 1) + ". " + line)
print()
return colored_input()
def colored_input():
return input(Fore.YELLOW + Style.BRIGHT + "> ")
print ("")
print ("")
print (" WELCOME TO THE MAZE ")
print ("")
print ("")
print ("You have found yourself stuck within a dark room, inside this room are 5 doors.. Your only way out..")
print ("")
print ("Do you want to enter door 1,2,3,4, or 5?")
print ("")
EVENT_DOOR1 = ("""
Theres an alien eating what appears to be a human arm, though its so damaged it's hard to be sure. There is a knife next to the alien.
what do you want to do?
""","""
Go for the knife
Attack alien before it notices you
""")
EVENT_ALIEN = ("""
You approach the knife slowly, While the alien is distracted. You finally reach the knife, but as you look up, the alien stares back at you.
You make a move to stab the alien, but he is too quick. With one swift motion, the alien thrusts you into the air.
You land hard, as the alien makes it's way towards you again. What should you do?
""", """
Accept defeat?
Last ditch effort?
""")
EVENT_ALIEN2 = ("""
You catch the alien off-guard. He stumbled and hisses in your direction. You scream in terror before he grabs the knife, and punctures your throat as he rips off your limbs.")
You died.. GAME OVER.. Mistakes can't be made this soon.. OUCH
""")
door = colored_input()
if door == "1":
run_event(EVENT_DOOR1)
alien = colored_input()
if alien == "1":
run_event(EVENT_ALIEN)
elif alien == "2":
run_event(EVENT_ALIEN2)
restart=input("Start over? Yes or No? ").lower()
if restart == "yes":
sys.stderr.write("\x1b[2J\x1b[H")
main()
else:
exit()
main()
解决方案
您run_event
的函数在返回时不必要地再次调用colored_input()
,导致脚本等待另一个输入时无响应。删除该return colored_input()
行,您的代码将起作用。
另请注意,您应该在分配给的单项元组中添加一个逗号EVENT_ALIEN2
;否则它将被评估为一个字符串:
EVENT_ALIEN2 = ("""
You catch the alien off-guard. He stumbled and hisses in your direction. You scream in terror before he grabs the knife, and punctures your throat as he rips off your limbs.")
You died.. GAME OVER.. Mistakes can't be made this soon.. OUCH
""",)
推荐阅读
- c++ - 运行第一个 gtest 示例时出现问题
- assembly - Nasm ld 重定位被截断以适应:R_X86_64_16
- javascript - 是否有可能在拉斐尔饼图的图例中添加卷轴?
- swift - 如何从 HealthKit 获取体脂百分比数据?
- java - 大批
- 获取单词在行号上的字符位置 - sql - 未使用 Snowflake 中的搜索优化服务
- amazon-web-services - 如何使用 AWS 生命周期管理策略仅将大于 100MB 的对象从 S3 Standard 转换到 AWS Glacier?
- javascript - 在javascript中循环对象数组
- c++ - 使用“cls”命令时,system() 函数出现问题
- html - 如何更改共享模块中共享模块的样式?