【问题标题】:Struggling with my text based game in python在 python 中与我的基于文本的游戏作斗争
【发布时间】:2019-10-26 17:32:14
【问题描述】:

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

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

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

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

【问题讨论】:

  • 预期输出是什么?你已经描述了你在哪里,但不清楚你想要实现什么......
  • 我希望它指的是基于餐厅选择的门的一组新结果,我打算把一个怪物放在一个后面,一个藏宝室放在另一个后面。这意味着一个会导致游戏“获胜”,而另一个会要求重新开始。我知道我还没有定义这些,但我稍后会实施它们。我想要发生的是,就像在第 31 行它说“你已经选择”我希望它在运行时说“所以你选择了左/右门”,但到目前为止它没有,它只打印玩家进入大厅的最后一个字符串。
  • 惊喜!您的代码完全按照您的要求执行。请参阅这个 [可爱的调试站点](ericlippert.com/2014/03/05/how-to-debug-small-programs) 寻求帮助。您的整个流程仅适用于一个房间转换:您要求输入 1 或 2,调用相应的函数,然后退出程序。撇开叙述,这就是您编写的全部
  • 如果你想做不止一次的事情……使用循环?
  • 另外,将故事情节和控制流分开。学习足够多的编程来构建您从一个区域到另一个区域的互联世界——您的描述只是您已经很好地跟踪的简单描述,例如“下水道”。一旦您的玩家可以在情节中移动,然后您就可以载入故事情节。

标签: python adventure


【解决方案1】:

您的代码结构让您觉得有点尴尬。

你基本上有这个:

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

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

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

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

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

【讨论】:

  • 同意,我和班上的一些人交谈过,他们也建议使用字典来解释我最初的工作。我想我会改变它,我没有太多使用它们的经验,但我不知道它是怎么回事,谢谢你的评论!
猜你喜欢
  • 1970-01-01
  • 2016-02-20
  • 1970-01-01
  • 2015-09-10
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多