在python中挣扎于基于文本的游戏

2024-03-28 23:03:56 发布

您现在位置: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

我很抱歉,如果我错过了一些非常明显的东西,编码当然不是我最擅长的领域,但我真的想提高。也为语法错误道歉。你知道吗


Tags: andofthetopathyouoneat
0条回答
网友
1楼 · 发布于 2024-03-28 23:03:56

你的代码结构让你觉得有点尴尬。你知道吗

基本上你有:

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

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

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

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

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

相关问题 更多 >