4
\$\begingroup\$

I've decided to make a text adventure game but I feel that the code I have now is annoying, and as I get farther, it will make it hard to read. Is this a correct assumption? Please tell me what I'm doing wrong so I can improve on my techniques with coding.

#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
\$\endgroup\$
0

2 Answers 2

3
\$\begingroup\$

Thoughts:

  • You're effectively creating Choose Your Own Adventure(tm) books:

https://en.wikipedia.org/wiki/Choose_Your_Own_Adventure

  • Keep the text for each choice/page in a separate file, no need to clutter up your code. Perhaps even name the files page1.txt, page2.txt, etc.

  • Instead of having the logic of which page can lead to which pages, include that logic in the text files themselves. That way, your adventure can get longer without your code getting longer. Of course, preprocess the "this page leads to pages x/y/z" before printing them for the end user.

\$\endgroup\$
2
\$\begingroup\$

Your code is indeed too repetitive. You can use a list of messages and a loop over it:

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")

Writing a version of this function where multiple inputs are allowed is left as an exercise for the reader (see the in operator).

\$\endgroup\$
2
  • \$\begingroup\$ I failed to mention this in my post but I'm a complete beginner. This is basically my knowledge of python so far. If you have time, could you give a complete explanation of the code you showed me here, and how I would write a different version that would plug in to my code? \$\endgroup\$
    – Rustywasp
    Mar 16, 2016 at 23:31
  • \$\begingroup\$ @Rustywasp You should start with a tutorial then. This isn't a good place to come and ask for help, if you don't at least have enough experience to understand the answers you get. \$\endgroup\$
    – user229044
    Mar 17, 2016 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.