首页 > 解决方案 > 在 python 中与我的基于文本的游戏作斗争

问题描述

我正在为我的大学项目而苦苦挣扎。我正在做一个基于文本的冒险游戏,但我似乎无法超越这个障碍。

我尝试调用列表以显示“您已选择...”,但我似乎无法做到这一点,我尝试按照我之前对另一个列表所做的操作,但我认为我迷路了。

import random
import time

list_intro = ["The drawbridge.", "The sewers."]  # naming the 2 indexes to paths ingame.
hall_rooms = ["Left door", "Right door"]


def intro():
    print("You heard at the local tavern of a abandoned castle holding many treasures.")
    print("You journey for 7 days until you find yourself at the bottom of the path leading to the castle.")
    print("Standing at the foot of the castles runway you see two options to enter.")
    for index, path in enumerate(list_intro):
        # this is the loop that prints both options as well as the index value +1 (index starts @ 0).
        print(str(index + 1) + ".", path)
    # print("1. To enter through the drawbridge.")
    # print("2. To swim under the moat and enter the sewers.")


story = {}


def chosenpath():
    path = ""
    while path != "1" and path != "2":  # we are including input validation, meaning only predefined values will work.
        path = input(
            "What path will you choose? (1 or 2): ")  # the inclusion of the boolean operator "and" is also here

        return int(path)


def checkpath(path):  # here we are simply making a function to return a string based on the option the user chooses.
    print("You have chosen", list_intro[path - 1])
    return entering_path(path)


def entering_path(path):
    print(f"So you've entered {list_intro[path - 1]}")  # here we are simply using the user input to call a item from
    # our list using index values.
    if path == 1:
        return """You cross the drawbridge and see a gargoyle looking straight at you,
before you have time to react you feel a slight brush across your neck, you then fall to the ground
but see your body standing, it seems your head is no longer attached to your body.
Better luck next time!"""
    if path == 2:  # Adding a new string here for the other path.
        return """You climb over the small ledge leading into the castles underbelly,unfortunately the swim wasn't great,
& you now wreak of old sewage and damp water. After walking up some stairs you find yourself in a grand dining hall,
At the back of the hall there are two doors, one on the left and one on the right. What one do you choose?"""
    print(hall_rooms)


def Dining_Hall_Rooms():
    print("So you've chosen", hall_rooms[-1])


intro()
path = chosenpath()
print(checkpath(path))

我没有收到错误消息,但是当我沿着“下水道”的路径运行代码时,我得到了这个 -

You heard at the local tavern of a abandoned castle holding many treasures.
You journey for 7 days until you find yourself at the bottom of the path leading to the castle.
Standing at the foot of the castles runway you see two options to enter.
1. The drawbridge.
2. The sewers.
What path will you choose? (1 or 2): 2
You have chosen The sewers.
So you've entered The sewers.
You climb over the small ledge leading into the castles underbelly,unfortunately the swim wasn't great,
& you now wreak of old sewage and damp water. After walking up some stairs you find yourself in a grand dining hall,
At the back of the hall there are two doors, one on the left and one on the right. What one do you choose?

Process finished with exit code 0

如果我遗漏了一些非常明显的东西,我很抱歉,编码当然不是我最擅长的领域,但我真的很想改进。也为语法错误道歉。

标签: pythonadventure

解决方案


你这里的代码结构让你觉得有点尴尬。

你基本上有这个:

def entering_path:
    if path == 1:
        return
    if path == 2:
        return
    print(“foo”)

但是如果 path 为 1 或 2,最后一行 print 语句将永远不会运行,因为您已经从退出它的函数返回了一个值。

您需要将 print 语句移出该函数,或者使该函数直接打印您的步骤,而不是返回打印的值。

不过,我建议您对程序流程进行更多研究,而不是修复您的代码。您希望您的故事(只是数据)与您的逻辑分开存储并与用户交互,并且您希望逻辑流程易于阅读。

理想情况下,您应该有一个包含故事的数据结构和一个解析该数据的函数,这样您就可以在不修改函数的情况下添加或删除部分故事,并且仍然拥有完整的故事工作。


推荐阅读