首页 > 解决方案 > 你如何在 python 上为基于文本的 rpg 循环?

问题描述

我正在创建一个基于文本的 rpg,我需要弄清楚如何为它做循环,这样我就可以回到他们进展的主要故事图块。我需要将 Map 循环回选项部分。

story = input("What do you do? your choices -> Map, Travel, Exit")
if story == "Exit":
    print("you left the game, goodbye" + " " + Name + "!")
    import sys
    sys.exit()
if story == "Map":
    print("Your map shows an abandoned house, Lake, Lab, and abandon asylum.")

标签: pythonloops

解决方案


我不太清楚你的意思,也许是这样的?

while True:
    story = input("What do you do? your choices -> Map, Travel, Exit")
    if story == "Exit":
        print("you left the game, goodbye" + " " + Name + "!")
        import sys
        sys.exit()
    if story == "Map":
        print("Your map shows an abandoned house, Lake, Lab, and abandon asylum.")

不过,我个人会做一些改动:

import sys

while True:
    story = input("What do you do? your choices are: Map, Travel, Exit").lower()
    if story == "exit":
        print("you left the game, goodbye {}!".format(Name))
        sys.exit()
    elif story == "map":
        print("Your map shows an abandoned house, lake, lab, and abandoned asylum.")
  • 开头导入sys
  • 确保输入中的大写无关紧要(节省令人沮丧的游戏体验)
  • 修复了最后一个打印语句中的拼写错误
  • 您可能希望将输入更改为其他内容,以便它始终有效,无论您使用 python 2 还是 3

推荐阅读