首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单文字冒险游戏

简单文字冒险游戏
EN

Code Review用户
提问于 2016-03-16 21:46:58
回答 2查看 904关注 0票数 4

我决定做一个文字冒险游戏,但我觉得我现在的代码是烦人的,随着我走得更远,它将使它难以阅读。这是正确的假设吗?请告诉我,我做错了什么,以便我可以提高我的技术与编码。

代码语言:javascript
复制
#VARIABLES======================================================================================================================================
else_statement = "How hard is it to press 1..."
introduction = "Introduction:\n\n""The year is 567 C.E. You and your family live in Czarne, Pomerania. There has been a major population shift to your region as viking raids terrorize the people along the shore. Because of this massive population shift, tensions are high between the populas. There are constant fights in the street because the local militia isn't plentiful enough to pacify the fights. The viking raiders exhausted the coatal villages. To keep funding their raids, they must go inland to the village of Czarne..."
introduction_1 = "You, your wife, and your kids are having dinner in your small home. Your wife prepared a heavenly soup and chicken. After dinner you put your kids to bed and you fell asleep. You are awaken by screams outside your house. Your wife and kids were already awake and were huddled up close to you. You sit up and say 'it's just another fight'. Then a man weilding an axe bursts through the door. He starts to charge for your family. You charge and tackle the man. Several punches are thrown but you managed to choke the man out. You then realized that this is no ordinary man... he is a viking. After learning of this news, you gathered your family and left through the back door."
introduction_2 = "You gather your family and leave through the back door. When you stepped outside, you were greeted with horror. Houses being burned down, people being slain, livestock being taken. You were seeing your life crumble with your own eyes. 'We need to make it to the town hall' you say. Your family is speechless, the kids are in so much shock that they're forgetting to cry. Taking advantage of the chaos that is happening, you and your family start running to the town hall. You arrive at the town hall only to see that the vikings had captured it. They all see you bursting through the door. You told your family to run while you fought them."
introduction_3 = "You told your family to run. Your family started to run out of the town hall. You, with the axe from the viking earlier, started to fight off the viking invaders. You managed to kill 5 mighty viking during your fury but you were knocked out. The viking cheif, who watched as you fought his men, was intrigued by you. His men were about to kill you but the chief block the swing and told his men to keep you."
introduction_4 = "You wake up hours later on the chief's boat. 'Where is my family!' you scream. The chief turns around and starts to walk toward you. 'Your family is safe, but if you want them to live, you must join me in my raids.' the chief says. You are angry and confused. 'Why would I help you viking scum!?' you say. 'Because if you don't, I will throw your family off the boat! So you decide on what you love most, your family or your pride'. 'I will never help you and your viking scum!' you said"
introduction_5 = "The chief angerly grunts and gives the order to throw your family overboard. You yell to the chief saying that you will help him."
introduction_6 = "'No no! Stop! Fine I will help'. The chief laughs and tells his men to put the family back on the deck. 'Now that we're all on the same page, I can tell you your assignment' says the chief. 'You are to go to the village of Ustka and raid it. You will be accompanied by a platoon of vikings. You are to raid as much as you can in the time frame. Don't fail me, or it'll be your head!'"

chapter1_1 = "\n   Chapter 1""\nThe Chief's Pet\n You arrive at Ustka in the dead of night. The villagers haven't noticed that vikings are there. Do you tell your men to sneak up(1) or go in axes blazing(2)?"
chapter1_1_choice1 = "You and your men sneak up to houses. You are about to give the order to move in when you hear someone walking towards you. Do you initiate the attack(1) or wait for the person to walk past(2)"
chapter1_1_choice2 = "You give the order to attack. Your men charge into the village. The villagers were caught off guard. Your men slaughtered many villagers. When the militia arrived you see that they are heavily equipped. This is unusual for a coastal village. When you took a closer look at their armor, you see a flag on their armor but you can't quite make it out. Your men charge into the militia. The caualties are great on both sides of the fight, but you win the battle. After taking a closer look at the militia, you see that they are not militia at all. They're soldiers sent by the local Pomeranian king. He must have sent these troops to the rest of the coastal villages to stop raiding. You still need to raid to the rest of the village. Do you go to the Town Hall to get the loot(1) or to the barracks so you can kill any remaining troops(2)?"

q = 0
wrong = "\nWrong. Try again.\n"



#GAME BEGINS HERE===============================================================================================================================
def viking(): 
    """Text Adventure"""
    global q
    print(introduction)

    while q==0:
        answer=str(raw_input("Type 1 to continue: "))
        if answer == "1":
            print(introduction_1)
            q+=1
            break 
        else:
            print(wrong)
            print(introduction)
            pass

    while q==1:
        answer=str(raw_input("Type 1 to continue: "))
        if answer == "1":
            print(introduction_2)
            q+=1
            break     
        else:
            print(wrong)
            print(introduction_1)
            pass

    while q==2:
        answer=str(raw_input("Type 1 to continue: "))
        if answer == "1":
            print(introduction_3) 
            q+=1
            break
        else:
            print(wrong)
            print(introduction_2)
            pass

    while q==3:
        answer=str(raw_input("Type 1 to continue: "))
        if answer == "1":
            print(introduction_4)
            q+=1
            break
        else:
            print(wrong)
            print(introduction_3)
            pass

    while q==4:
        answer=str(raw_input("Type 1 to continue: "))
        if answer == "1":
            print(introduction_5)
            q+=1
            break
        else:
            print(wrong) 
            print(introduction_4) 
            pass

    while q==5:
        answer=str(raw_input("Type 1 to continue: "))
        if answer == "1":
            print(introduction_6) 
            q+=1
            break     
        else:
            print(wrong)
            print(introduction_5)
            pass

    while q==6:
        answer=str(raw_input("Type 1 or 2 to continue: "))
        if answer == "1":
            print(chapter1_1)
            q+=1
            break     
        else:
            print(wrong)
            print(introduction_6)
            pass

    while q==6:
        answer=str(raw_input("Type 1 or 2 to continue: "))
        if answer == "1":
            print(chapter1_1_choice1)
            q+=1
            break     
        else:
            print(wrong)
            print(chapter1_1)
            pass

    while q==6:
        answer=str(raw_input("Type 1 or 2 to continue: "))
        if answer == "1":
            print(chapter1_1_choice2)
            q+=1
            break     
        else:
            print(wrong)
            print(chapter1_1)
            pass
EN

回答 2

Code Review用户

发布于 2016-03-16 22:47:11

思想:

  • 您实际上正在创建“选择您自己的冒险”(Tm)书籍:

https://en.wikipedia.org/wiki/Choose_你的_自力_冒险

  • 将每个选项/页面的文本保存在一个单独的文件中,不需要将代码弄乱。甚至可以将这些文件命名为page1.txt、page2.txt等。
  • 与其拥有哪个页面的逻辑可以导致哪个页面,不如将该逻辑包含在文本文件本身中。这样,您的冒险可以变得更长,而不需要您的代码变得更长。当然,在为最终用户打印页面之前,先对“此页面导致页面x/y/z”进行预处理。
票数 3
EN

Code Review用户

发布于 2016-03-16 22:12:12

您的代码确实太重复了。您可以使用消息列表并对其进行循环:

代码语言:javascript
复制
def show_with_input_in_the_middle(messages, 
                                  expected_input,
                                  prompt="Type `{}` to continue: "):
    index = 0
    while index < len(messages):
        print(messages[index])
        if raw_input(prompt.format(expected_input)) == expected_input:
            index += 1
        else:
            print("Invalid input.\n")

编写允许多个输入的此函数的版本作为读者练习(请参阅in操作符)。

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/123041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档