如何启动文本游戏?

huangapple go评论98阅读模式
英文:

How do I get the text game to start?

问题

以下是您提供的代码的翻译部分:

def main_menu():
    print("你已经在废弃的城镇里闲逛了几天了。自世界末日以来,没有太多其他事可做。")
    print("你在穿越一个偏僻的街区时,发现了这座房子。")
    print("与其他房子相比,它看起来要好得多。较少的残破感。")
    print("你决定这看起来是一个好地方去探索,即使看得太久会让你有一种奇怪的感觉。")
    print("你进入了前院。")
    print()
    print("欢迎来到这座房子。收集七件物品并进入最终房间。")
    print("要移动,请输入'前'、'后'、'左'或'右'。")
    print("要拾取物品,请输入'是'或'否'。")
    print("要退出游戏,请输入'退出'。")

def inventory():
    items = {
        "Front Entrance Key" : {"name" : "Front Entrance Key", "description" : "一把古老的生锈钥匙。"},
        "Cutting Knife" : {"name" : "Cutting Knife", "description" : "用于切割肉类的大刀。"},
        "Attic Key" : {"name" : "Attic Key", "description" : "一把小巧的银色钥匙。"},
        "Family Photo" : {"name" : "Family Photo", "description" : "一张描述一个小家庭的照片;一个戴眼镜的男人,一个黑发女人,和一个卷曲的小女孩。"},
        "Emily's Journal" : {"name" : "Emily's Journal", "description" : "属于照片中年轻女孩的日记。阅读它描述了一个病重但开朗的女孩,逐渐对她父亲坚持不断给她注射不记得医生开的奇怪药物以及父母突然爆发的争吵感到害怕和疲惫。"},
        "Data Journal" : {"name" : "Data Journal", "description" : "由Emily的父亲保留的日志。阅读条目描述了一个如此绝望拯救女儿的人,他愿意让人和动物接受奇怪而痛苦的实验。朝着最后,他似乎很成功,尽管手写文字的恶化显示出他奇怪的精神状态。"},
        "Apocalyptic Newspaper" : {"name" : "Apocalyptic Newspaper", "description" : "世界末日前后出版的报纸。责怪一个男人的癌症治疗导致患者突然发生无法控制的暴力行为。"}
    }
    
def house():
    rooms = {
        "Front Yard" : {"name" : "Front Yard", "right" : "Back Yard", "forward" : "Front Entrance",
                        "text" : "你进入了前院。在你面前是一座房子,它看起来古雅而安静。有一个门廊和一个看起来像古董的前门。向右边是分隔后院和前院的篱笆。"},
        "Back Yard" : {"name" : "Back Yard", "left" : "Front Yard", "item" : "Front Entrance Key",
                       "text" : "你在后院。这里看起来糟糕得多,有一个老旧的棚子和一辆破旧的卡车。它的油漆锈迹斑斑,有些地方甚至看起来几乎是粉色的。里面很干净,虽然古老而破旧。控制台是解锁的。你看向里面。"},
        "Front Entrance" : {"name" : "Front Entrance", "back" : "Front Yard", "right" : "Kitchen",
                            "left" : "Living Room", "forward" : "Upstairs Hallway",
                            "text" : "穿过前门,你看到了一个适中的家。在你面前是一组楼梯,右边是厨房,左边是客厅。"},
        "Kitchen" : {"name" : "Kitchen", "left" : "Front Entrance", "item" : "Cutting Knife",
                     "text" : "厨房一团糟。食物摆在桌子上,好像家人没打算离开。冰箱锈迹斑斑,有些橱柜被撕开。在柜台上有一个装满餐具的刀架。"},
        "Living Room" : {"name" : "Living Room", "right" : "Front Entrance", "forward" : "Downstairs Bathroom",
                         "item" : "Family Photo",
                         "text" : "客厅很小,角落里有一台老式电视和一张朝向它的沙发和椅子。向前走,有一条通向带有照片装饰的浴室的小走廊。"},
        "Downstairs Bathroom" : {"name" : "Downstairs Bathroom", "back" : "Living Room", "item" : "Attic Key",
                                 "text" : "浴室很小,只有半浴室而不是全浴室。水槽上有东西闪闪发光。"},
        "Upstairs Hallway" : {"name" : "Upstairs Hallway", "back" : "Front Entrance", "left" : "Emily's Room",
                              "right" : "Upstairs Bathroom", "forward" : "Hallway End",
                             

<details>
<summary>英文:</summary>

I&#39;m new to code. I&#39;m currently working on the final project for my very first class. We&#39;re supposed to make a text based game. I&#39;m trying to make a horror game and I&#39;m wanting the rooms and items to be decribed as the player comes across them in order for the story to be told. I&#39;ve got a working code (I think), but I can&#39;t actually get the game to start so that the player can begin typing in inputs.

Edit: I&#39;ve removed the indent in the &quot;print()&quot; line under the &quot;while true&quot; loop, but I&#39;m still stuck on the main menu. The game isn&#39;t describing the current room or pompting me to put in any inputs.

This is the full code to my game:
```python
def main_menu():
    print(&quot;You have been wandering the abandoned town for a few days now. There hasn&#39;t been &quot;
          &quot;much else to do since the world ended.&quot;)
    print(&quot;You come across the house while trekking a back-wood neighborhood.&quot;)
    print(&quot;It looks a lot nicer than the other houses. Less debilitated.&quot;)
    print(&quot;You decide it looks like a good place to explore, even if it looking at it for too &quot;
          &quot;long leaves you with an odd feeling.&quot;)
    print(&quot;You enter the Front Yard.&quot;)
    print()
    print(&quot;Welcome to The House. Collect seven items and enter the Final Room.&quot;)
    print(&quot;To move, type &#39;forward,&#39;, &#39;back,&#39; &#39;left,&#39; or &#39;right&#39;.&quot;)
    print(&quot;To pick up an item, type &#39;yes&#39; or &#39;no&#39;.&quot;)
    print(&quot;To exit the game, type &#39;quit&#39;.&quot;)

def iventory():
    items = {
        &quot;Front Entrance Key&quot; : {&quot;name&quot; : &quot;Front Entrance Key&quot;, &quot;description&quot; : &quot;An antique, rusted key.&quot;},
        &quot;Cutting Knife&quot; : {&quot;name&quot; : &quot;Cutting Knife&quot;, &quot;description&quot; : &quot;A large knife used to cleave meat.&quot;},
        &quot;Attic Key&quot; : {&quot;name&quot; : &quot;Attic Key&quot;, &quot;description&quot; : &quot;A small, silver key.&quot;},
        &quot;Family Photo&quot; : {&quot;name&quot; : &quot;Family Photo&quot;, &quot;description&quot; : &quot;A photo depicting a small family; a man with glasses, a dark-haired woman, and &quot;
                          &quot;a young girl with curly, brunette hair.&quot;},
        &quot;Emily&#39;s Journal&quot; : {&quot;name&quot; : &quot;Emily&#39;s Journal&quot;, &quot;description&quot; : &quot;A journal belonging to the young girl seen in the family photos.&quot;
                             &quot;Reading it depicts a sickly, yet cheerful girl slowly growing &quot;
                             &quot;weary and fearful of her father&#39;s insistence on constantly taking &quot;
                             &quot;unknown shots she doesn&#39;t recall her doctor prescribing, as well &quot;
                             &quot;as her parents sudden bouts of fighting.&quot;},
        &quot;Data Journal&quot; : {&quot;name&quot; : &quot;Data Journal&quot;, &quot;description&quot; : &quot;A log book kept by Emily&#39;s father. Reading the entries depicts a man &quot;
                          &quot;so desperate to save his daughter that he was willing to subject &quot;
                          &quot;both people and animals to strange and painful experiments. He appeared &quot;
                          &quot;triumphant toward the end, though the handwriting&#39;s deterioration &quot;
                          &quot;shows his odd mental state.&quot;},
        &quot;Apocalyptic Newspaper&quot; : {&quot;name&quot; : &quot;Apocalyptic Newspaper&quot;, &quot;description&quot; : &quot;A newspaper published around the time the world ended. It blames a &quot;
                                   &quot;man&#39;s cancer treatments for the sudden bouts of uncontrollable violence &quot;
                                   &quot;displayed by the patients.&quot;}
    }
    

def house():
    rooms = {
        &quot;Front Yard&quot; : {&quot;name&quot; : &quot;Front Yard&quot;, &quot;right&quot; : &quot;Back Yard&quot;, &quot;forward&quot;: &quot;Front Entrance&quot;,
                        &quot;text&quot; : &quot;You have entered the Front Yard. In front of you is the house, quaint and quiet.&quot;
                                 &quot;It has a porch and an antique-looking front entrance. To the right, there is &quot;
                                 &quot;the fence that separates the back yard from the front.&quot;},
        &quot;Back Yard&quot; : {&quot;name&quot; : &quot;Back Yard&quot;, &quot;left&quot; : &quot;Front Yard&quot;, &quot;item&quot; : &quot;Front Entrance Key&quot;,
                       &quot;text&quot; : &quot;You are in the back yard. It looks much worse back here, with an old, debilitated shed &quot;
                                &quot;and a broken down truck. It&#39;s paint is so rusted and peeled, it almost appears pink.&quot;
                                &quot;The inside is clean, if old and worn down. The console is unlocked. You look inside.&quot;},
        &quot;Front Entrance&quot; : {&quot;name&quot; : &quot;Front Entrance&quot;, &quot;back&quot; : &quot;Front Yard&quot;, &quot;right&quot; : &quot;Kitchen&quot;,
                            &quot;left&quot; : &quot;Living Room&quot;, &quot;forward&quot; : &quot;Upstairs Hallway&quot;,
                            &quot;text&quot; : &quot;Walking through the Front Entrance, you are greeted with a modest home. In front &quot;
                                     &quot;of you is a set of stairs, to the right the kitchen, and to the left the living &quot;
                                     &quot;room.&quot;},
        &quot;Kitchen&quot; : {&quot;name&quot; : &quot;Kitchen&quot;, &quot;left&quot;: &quot;Front Entrance&quot;, &quot;item&quot;: &quot;Cutting Knife&quot;,
                     &quot;text&quot;: &quot;The kitchen is a mess. Food is left on the table as if the family wasn&#39;t expecting to leave.&quot;
                             &quot;The fridge is rusted and some of the cabinets are torn open. One the counter, there is a &quot;
                             &quot;knife block filled with cutlery.&quot;},
        &quot;Living Room&quot; : {&quot;name&quot; : &quot;Living Room&quot;, &quot;right&quot; : &quot;Front Entrance&quot;, &quot;forward&quot; : &quot;Downstairs Bathroom&quot;,
                         &quot;item&quot; : &quot;Family Photo&quot;,
                         &quot;text&quot; : &quot;The living room is small, with an old school box TV in the corner and a couch and chair &quot;
                                  &quot;facing it. Forward, there is a small hallway leading to a bathroom, decorated with &quot;
                                  &quot;photographs.&quot;},
        &quot;Downstairs Bathroom&quot; : {&quot;name&quot; : &quot;Downstairs Bathroom&quot;, &quot;back&quot; : &quot;Living Room&quot;, &quot;item&quot; : &quot;Attic Key&quot;,
                                 &quot;text&quot; : &quot;The bathroom is tiny, only a half-bath as opposed to a full. Something glints &quot;
                                          &quot;in the rusted sink.&quot;},
        &quot;Upstairs Hallway&quot; : {&quot;name&quot; : &quot;Upstairs Hallway&quot;, &quot;back&quot; : &quot;Front Entrance&quot;, &quot;left&quot; : &quot;Emily&#39;s Room&quot;,
                              &quot;right&quot; : &quot;Upstairs Bathroom&quot;, &quot;forward&quot; : &quot;Hallway End&quot;,
                              &quot;text&quot; : &quot;Upstairs is only a long hallway. To the left, a door decorated with a young girl&#39;s drawing.&quot;
                              &quot;To the right, an open door leading to another bathroom. Further down, you can see more &quot;
                              &quot;doors.&quot;},
        &quot;Emily&#39;s Room&quot; : {&quot;name&quot; : &quot;Emily&#39;s Room&quot;, &quot;right&quot; : &quot;Upstairs Hallway&quot;, &quot;item&quot; : &quot;Emily&#39;s Journal&quot;,
                          &quot;text&quot; : &quot;Inside the girl&#39;s room, the far wall is decorated with letters spelling out the name &quot;
                                   &quot;Emily. The room is small and filled with toys, drawings, and crayons. The bed is &quot;
                                   &quot;plain, small with a purple cover. The corner of a book peaks out from beneath the blanket.&quot;},
        &quot;Upstairs Bathroom&quot; : {&quot;name&quot; : &quot;Upstairs Bathroom&quot;, &quot;left&quot; : &quot;Upstairs Hallway&quot;,
                               &quot;text&quot; : &quot;The bathroom is surprisingly large. You are taken aback by the sight of rusted, &quot;
                                        &quot;browned blood splattered across the mirror. You risk peaking behind the curtain of &quot;
                                        &quot;the shower-tub. The body of the mother from the photograph lies limply inside, a &quot;
                                        &quot;gaping wound on her head. It stinks. She is rotten. She&#39;s been here a while.&quot;
                                        &quot;You don&#39;t want to be in this room anymore.&quot;},
        &quot;Hallway End&quot; : {&quot;name&quot; : &quot;Hallway End&quot;, &quot;back&quot; : &quot;Upstairs Hallway&quot;, &quot;left&quot; : &quot;Strange Couple&#39;s Room&quot;,
                         &quot;forward&quot; : &quot;Attic&quot;,
                         &quot;text&quot; : &quot;Walking further down the hallway brings you next to the open ladder leading to the attic.&quot;
                                  &quot;To the left is another door.&quot;},
        &quot;Strange Couple&#39;s Room&quot; : {&quot;name&quot; : &quot;Strange Couple&#39;s Room&quot;, &quot;right&quot; : &quot;Hallway End&quot;, &quot;item&quot;: &quot;Data Journal&quot;,
                                   &quot;text&quot; : &quot;The room clearly belongs to Emily&#39;s parents. It is quaint, with a well-made bed and &quot;
                                            &quot;a large office desk to the far right. An open journal sits atop the desk.&quot;},
        &quot;Attic&quot; : {&quot;name&quot; : &quot;Attic&quot;, &quot;back&quot; : &quot;Hallway End&quot;, &quot;forward&quot; : &quot;Strange Room&quot;, &quot;item&quot; : &quot;Apocalyptic Newspaper&quot;,
                   &quot;text&quot; : &quot;As you climb the ladder to the attic, you can&#39;t help but feel repulsed. The sight in front of &quot;
                            &quot;you is clearly the workspace of the strange man with glasses. Emily&#39;s father. You would think it &quot;
                            &quot;was a morgue with the way the tables of covered bodies line up. There is a chem desk in the corner &quot;
                            &quot;and an oddly reinforced door at the end of the room. You see some strewn papers as you pass by the &quot;
                            &quot;chem desk.&quot;},
        &quot;Strange Room&quot; : {&quot;name&quot; : &quot;Strange Room&quot;, &quot;text&quot; : &quot;Something in you dreads entering the room, but your curiosity &quot;
                                                            &quot;gets the better of you. Inside is nothing but a small, medical bed. &quot;
                                                            &quot;Clearly, this is where Emily was forced to stay while her father &quot;
                                                            &quot;tried creating her medicine. There is a mutilated body on the floor.&quot;
                                                            &quot;Emily&#39;s father. You&#39;re nearly not fast enough to notice Emily as she lunges for you.&quot;}
        }

directions = [&#39;forward&#39;, &#39;back&#39;, &#39;right&#39;, &#39;left&#39;]
pick_up_item = [&#39;yes&#39;]
main_menu()
current_room = &quot;Front Yard&quot;

while True:
    if current_room == &quot;Strange Room&quot;:
        if len(inventory) == 7:
            print(&quot;Luckily, you discovered all of the strange items within the house, learned Emily&#39;s history as patient&quot;
                  &quot;zero. You even found a large knife. You use it to defend yourself, swiping at Emily and distracting her&quot;
                  &quot;long enough to sprint from the room. You run and you don&#39;t look back.&quot;)
            print(&quot;Good End&quot;)

        else:
            print(&quot;Unfortunately, you have not discovered all of the items within the house. You do not have the means to &quot;
                  &quot;defend yourself from Emily as she closes in on you. She drags you further into the room.&quot;)
            print(&quot;Bad End&quot;)
print(&quot;You are in {}.&quot;.format(current_room[&quot;name&quot;][&quot;text&quot;]))
pick_up_item = input(&#39;\nDo you wish to inspect the item? &#39;)
command = input(&#39;\nWhat do you do? &#39;)
if command in directions:
    if command in current_room:
        current_room = rooms[current_room[command]]
    else:
        print(&#39;You cannot go that way.&#39;)
elif command == &#39;quit&#39;:
    print(&#39;Thanks for playing!&#39;)
else:
    print(&#39;Invalid input&#39;)

答案1

得分: 1

你被困在 while True 循环中。要开始,你需要将以 print("You are in {}... 开头的行与 while 循环中的行保持相同的缩进。之后还会有一些其他问题需要修复。

英文:

You are stuck in the while True loop. To start you need to include the lines starting with print(&quot;You are in {}... at the same indentation as the lines in the while loop. There will be some other issues to fix after that.

答案2

得分: 0

这是一个简化版本的代码,其中有三个房间和一个钥匙。如果你拿起钥匙并带着它进入最后一个房间,那么你就赢了。如果你对它的工作原理有任何疑问,请随时告诉我。我的建议是,一行一行地拆解它,我认为有时学校在教授编程时要么进展得太快,要么进展得太慢。YouTube是一个非常出色的学习编程资源,只要你多做笔记。祝你在Python之旅中好运!

def main_menu():
    print("欢迎来到游戏。找到钥匙并进入最后一个房间。")
    print("要移动,请键入'前进'、'后退'、'左转'或'右转'。")
    print("要拾取物品,请键入'是'或'y'。")
    print("要退出游戏,请键入'退出'。")

def inventory():
    items = {
        "钥匙": {"名称": "钥匙", "描述": "看看这把漂亮的钥匙。我在中间房间找到它。"}
    }
    return items

def game_map():
    rooms = {
        "前厅": {"名称": "前厅", "前进": "中间房间"},
        "中间房间": {"名称": "中间房间", "后退": "前厅", "前进": "最后一个房间", "物品": "钥匙"},
        "最后一个房间": {"名称": "最后一个房间", "后退": "中间房间"}
    }
    return rooms

directions = ['前进', '后退', '右转', '左转']
main_menu()
rooms = game_map()
items = inventory()
current_room = rooms["前厅"]
player_inventory = []

while True:
    print("你在{}。".format(current_room["名称"]))
    if "物品" in current_room and current_room["物品"] not in player_inventory:
        pick_up_item = input('\n这里有一个物品。你想拾取它吗? ')
        if pick_up_item.lower() in ('是', 'y'):
            player_inventory.append(current_room["物品"])
            print("你拾取了{}。".format(current_room["物品"]))
            print(items[current_room["物品"]]["描述"])
    if '钥匙' in player_inventory and current_room["名称"] == "最后一个房间":
        print("你赢了!!万岁!")
        print("感谢参与游戏!")
        break
    else:
        command = input('\n你要做什么? ')
    if command in directions:
        if command in current_room:
            current_room = rooms[current_room[command]]
        else:
            print('你不能朝那个方向前进。')
    elif command == '退出':
        print('感谢参与游戏!')
        break
    else:
        print('无效的输入')
英文:

Here is a minimal version where there are three rooms and a key. If you pick up the key and enter the final room with it in your inventory, then you win. You may be able to modify this for your purposes, if you have any questions about how it works please let me know. My advice though, just break it down line by line, imo sometimes schools have a tendency to either go way too fast or way too slow when teaching programming. YouTube is a really excellent resource for learning programming as long as you take lots of notes. Good luck on your Python journey!

def main_menu():
    print(&quot;Welcome to The Game. Find the key and enter the Final Room.&quot;)
    print(&quot;To move, type &#39;forward&#39;, &#39;back&#39;, &#39;left&#39;, or &#39;right&#39;.&quot;)
    print(&quot;To pick up an item, type &#39;yes&#39; or &#39;y&#39;.&quot;)
    print(&quot;To exit the game, type &#39;quit&#39;.&quot;)

def inventory():
    items = {
        &quot;Key&quot; : {&quot;name&quot; : &quot;Key&quot;, &quot;description&quot;:\
                 &quot;Check out this neat key. I found it in the middle room.&quot;}
    }
    return items

def game_map():
    rooms = {
        &quot;Front Room&quot; : {&quot;name&quot; : &quot;Front Room&quot;, &quot;forward&quot;: &quot;Middle Room&quot;},
        &quot;Middle Room&quot; : {&quot;name&quot; : &quot;Middle Room&quot;, &quot;back&quot;: &quot;Front Room&quot;, &quot;forward&quot;: &quot;Final Room&quot;, &quot;item&quot;: &quot;Key&quot;},
        &quot;Final Room&quot; : {&quot;name&quot; : &quot;Final Room&quot;, &quot;back&quot;: &quot;Middle Room&quot;}
    }
    return rooms

directions = [&#39;forward&#39;, &#39;back&#39;, &#39;right&#39;, &#39;left&#39;]
main_menu()
rooms = game_map()
items = inventory()
current_room = rooms[&quot;Front Room&quot;]
player_inventory = []

while True:
    print(&quot;You are in {}.&quot;.format(current_room[&quot;name&quot;]))
    if &quot;item&quot; in current_room and current_room[&quot;item&quot;] not in player_inventory:
        pick_up_item = input(&#39;\nThere is an item here. Do you wish to pick it up? &#39;)
        if pick_up_item.lower() in (&#39;yes&#39;, &#39;y&#39;):
            player_inventory.append(current_room[&quot;item&quot;])
            print(&quot;You picked up a {}&quot;.format(current_room[&quot;item&quot;]))
            print(items[current_room[&quot;item&quot;]][&quot;description&quot;])
    if &#39;Key&#39; in player_inventory and current_room[&quot;name&quot;] == &quot;Final Room&quot;:
        print(&quot;You win!! Hooray!&quot;)
        print(&quot;Thanks for playing!&quot;)
        break
    else:
        command = input(&#39;\nWhat do you do? &#39;)
    if command in directions:
        if command in current_room:
            current_room = rooms[current_room[command]]
        else:
            print(&#39;You cannot go that way.&#39;)
    elif command == &#39;quit&#39;:
        print(&#39;Thanks for playing!&#39;)
        break
    else:
        print(&#39;Invalid input&#39;)

huangapple
  • 本文由 发表于 2023年6月19日 00:39:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定