我一直在使用Python开发基于文本的冒险游戏。这可能是一个简单的修复程序,但我看不到它。当我在代码中添加另一个事件时,它给我一个INVALID SYNTAX错误。例如,当我添加EVENT_DOOR2时,我得到了无效的语法错误,特别是在door = Colored_input():上。以下是我原本较长的代码片段。任何见解都会有所帮助!谢谢!
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()
def colored_input():
return input(Fore.YELLOW + Style.BRIGHT + "> ")
print ("")
print ("")
print (Fore.YELLOW + Style.BRIGHT + " 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_DOOR2 = ("""You seem to have entered a different world.
Objects and natural elements seem to be alive, grass attempts to wrap around your ankles.
Ahead lies a sword, within what seems to be a large venus fly trap. What should you do?
""","""
Run through this world as fast as you can.
Attempt to grab the sword.
Explore Further.""")
door = colored_input()
if door == "1":
run_event(EVENT_DOOR1)
elif door == "2":
run_event(EVENT_DOOR2)
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
""")
alien = colored_input()
if alien == "1":
run_event(EVENT_ALIEN)
elif alien == "2":
run_event(EVENT_ALIEN2)
restart=input(Fore.YELLOW + Style.BRIGHT + "Start over? Yes or No? ").lower()
if restart == "yes":
sys.stderr.write("\x1b[2J\x1b[H")
main()
else:
exit()
main()