5
\$\begingroup\$

I'm totally new to coding and tried myself on a short text based adventure game. I got the feeling that the functions are the same ones all the time and the whole game is a bit monotonous... I'd be very thankful if anybody has some improvements or additional ideas!

import random
import time



def nameFunction(): #hello what's your name?
    name = input("Hello! What's your name, adventurer?\n")
    time.sleep(2)
    print ("Hi " + name + "...!")
    time.sleep(3)

def storyFunction(): #the story until hard part #half story is told
    print ("A few miles away,")
    time.sleep(3)
    print ("behind a huge wall out of undestroyable iron and concrete,")
    time.sleep(4)
    print ("is the most dangerous and frightening place you'll ever see.")
    time.sleep(5)
    print ("Once, many, many years ago the town had the euphonious name Therondia.")
    time.sleep(5)
    print ("The people living there where called 'the happy people', because of the prevailing satisfied and sunshiny atmosphere.")
    time.sleep(5)
    print ("Inhabitants of the sorrounding areas, envied people living in this town for the friendliness \nbetween the citizens and the completely safe neighborhoods, nothing bad had ever happened in Therondia.")
    time.sleep(6)
    print ("Everybody thought of it as the perfect place to spend their life.")
    time.sleep(5)
    print ("...")
    time.sleep(5)
    print ("But then it happened...")
    time.sleep(5)
    print ("... and everything changed.")


def readyFunction(): # ready to join?
    ready = input("Are you brave enough to join us exploring this haunted town? Then say 'yes'! But if you are too scared, you can say no...\n")
    if ready == "yes" or ready == "Yes" or ready == "yes!":
        print ("You're coming with us? That is amazing, we need every help we can get! But let us tell you the whole story so that you know in what you engage.")
        time.sleep(4)
        storyFunction()
    if ready == "no" or ready == "No":
        print ("You're not coming with us? What a petty... See you next time!")
    else:
        print ("That's not what I asked for...")
        readyFunction() #Problem: wenn danach das richtige eingegeben wird geht er trotzdem wieder in die Loop zu are you ready #

def missionFunctionAS(): #missionFunction after school
    print ("Well... Where should we search now?")
    time.sleep(3)
    schoolhomechurch = input ("What do you think? Should we search for her at school (1), at her families home (2) or at church (3)?\nJust answer '1', '2' or '3'!\n")
    time.sleep(3)
    if schoolhomechurch == "1":
        print("- you go to the school -")
        time.sleep(3)
        print("Hmmmmm... Let's split up, so if she's here, we can find her faster. But be careful that her friends do not see you!")
        schoolyesno = random.randint(1,2)
        if schoolyesno == 1:
            time.sleep(4)
            print ("HERE SHE IS!!!")
            time.sleep(3)
            print (".....")
            time.sleep(3)
            print ("Now that we know what happened to her, we can go home and carry on research for couring sickened people.")
            time.sleep(3)
            print ("And when we find a remedy, we'll come back and try to bring this town the peace it once held.")
            time.sleep(5)
            print ("... to be continued,extended and improved...")
        if schoolyesno == 2:
            time.sleep(4)
            print ("I think there is nobody around... Let's search somewhere else...")
            missionFunctionAS()
    if schoolhomechurch == "2":
        print ("- you go to the families home -")
        time.sleep(3)
        print ("Hmmmmm... Let's split up, so if she's here, we can find her faster. But be careful that her parents do not see you!")
        homeyesno = random.randint(1,2)
        if homeyesno == 1:
            time.sleep(4)
            print ("HERE SHE IS!!!")
            time.sleep(3)
            print (".....")
            time.sleep(3)
            print ("Now that we know what happened to her, we can go home and carry on research for couring sickened people.")
            time.sleep(3)
            print ("And when we find a remedy, we'll come back and try to bring this town the peace it once held.")
            time.sleep(5)
            print ("... to be continued,extended and improved...")
        if homeyesno == 2:
            time.sleep(4)
            print ("I think there is nobody around... Let's search somewhere else...")
            missionFunctionAS()
    if schoolhomechurch == "3":
        print ("- you go to the church -")
        time.sleep(3)
        print ("*whispering* Wow... this is an amazing building!... But pretty scary as well...")
        time.sleep(3)
        print ("LOOK! There she is... At the confessional...")
        time.sleep(3)
        print (" ......... ")
        time.sleep (3)
        print ("Now that we know what happened to her, we can go home and carry on research for couring sickened people.")
        time.sleep(3)
        print ("And when we find a remedy, we'll come back and try to bring this town the peace it once held.")
        time.sleep(5)
        print ("... to be continued,extended and improved...")


def rightpathFunction(): #which path to get into the town? -> 1. stop to be kicked out of the game!!
    time.sleep(4)
    print ("The only way to get into the city is to climb the wall with our special equipment.")
    time.sleep(4)
    print ("There is still one problem we would have to deal with if we made it to the other side of it:")
    time.sleep(4)
    print ("If anyone sees us, they will fight us. We have to be prepared for a battle.")
    time.sleep(4)
    print ("We are five people, they will be max. 10. We can overcome them if we hit 5 of them quickly. Let's go!")
    time.sleep(4)
    seen = random.randint(1, 2)
    if seen == 1:
        print ("Puhhh... We did it without drawing any attention towards us. Good Job!")
        missionFunction()
    if seen == 2:
        print ("Ohhhh sh***!! Okay guys we have to fight!")
        time.sleep(4)
        print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        print ("                         Fighting...                  ")
        print ("YOU MUST HIT ABOVE 5 OF YOUR ENEMIES TO WIN THIS FIGHT")
        print (" IF THE ZOMBIES HIT MORE OFTEN THAN YOU, YOU WILL DIE" )
        print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        time.sleep(4)
        humans = int(random.randint(3, 10))
        zombies = int(random.randint(1, 5))
        print ("You hit ", humans, "times.")
        time.sleep(2)
        print ("The Zombies hit ", zombies, "times.")
        time.sleep(2)
        if zombies > humans:
            print ("The Zombies have dealt more damage than you!")
            time.sleep(3)
            print ("You can't make it out alive... You die a heroic death....")
            time.sleep(3)
            playAgain = input ("Do you want to try to climb the wall again? God seems to give you another chance...")
            if playAgain == "yes" or playAgain == "Yes" or playAgain == "yes!":
                rightpathFunction()
            else:
                print ("Okay! See you next time!")
        else:
            print ("You won the fight!")
            time.sleep(3)
            print ("That was amazing! But we have no to time to rest on our success, let's go on.")
            missionFunction()


#def schoolFunction():
    #print ("Hmmmmm... Let's split up, so if she's here, we can find her faster.")
    #time.sleep(3)
    #print ("If somebody finds her, he contacts the rest of us via the walkie talkie!")
    #->choose random string from List
    #schoolyesno = ["shhhht....shtshht... (out of the walkie talkie) I found her! Come to room 143.", "shhhht....shtshht... (out of the walkie talkie) I think there is nobody around... Let's search somewhere else..."]
    #print (random.choice(schoolyesno))
    #if schoolyesno == "shhhht....shtshht... (out of the walkie talkie) I found her! Come to room 143.":
        #room143Function()
    #if schoolyesno == "shhhht....shtshht... (out of the walkie talkie) I think there is nobody around... Let's search somewhere else...":
        #missionFunction()
    #schoolyesno = random.randint(1, 2)
    #if schoolyesno == 1:
        #print ("shhhht....shtshht... (out of the walkie talkie) I found her! Come to room 143.")
        #room143Function()
    #if schoolyesno == 2:
        #print ("shhhht....shtshht... (out of the walkie talkie) I think there is nobody around... Let's search somewhere else...")

def missionFunction(): #where to go and if the girl is found
    print ("We need to find the girl. She is the key to everything that happended here. If we can find out what turned her to what ever she might be now,\nwe might find the cause and can save our families... ")
    time.sleep(4)
    print ("We assume the girl to be around either her school, her home or the church of the town. As you brang us good luck last time, you should decide again.")
    time.sleep(3)
    schoolhomechurch = input ("What do you think? Should we search for her at school (1), at her families home (2) or at church (3)?\nJust answer '1', '2' or '3'!\n")
    time.sleep(3)
    if schoolhomechurch == "1":
        print("- you go to the school -")
        time.sleep(3)
        print("Hmmmmm... Let's split up, so if she's here, we can find her faster. But be careful that her friends do not see you!")
        schoolyesno = random.randint(1,2)
        if schoolyesno == 1:
            time.sleep(4)
            print ("HERE SHE IS!!!")
            time.sleep(3)
            print (".....")
            time.sleep(3)
            print ("Now that we know what happened to her, we can go home and carry on research for couring sickened people.")
            time.sleep(3)
            print ("And when we find a remedy, we'll come back and try to bring this town the peace it once held.")
            time.sleep(5)
            print ("... to be continued,extended and improved...")
        if schoolyesno == 2:
            time.sleep(4)
            print ("I think there is nobody around... Let's search somewhere else...")
            missionFunctionAS()
    if schoolhomechurch == "2":
        print ("- you go to the families home -")
        time.sleep(3)
        print ("Hmmmmm... Let's split up, so if she's here, we can find her faster. But be careful that her parents do not see you!")
        homeyesno = random.randint(1,2)
        if homeyesno == 1:
            time.sleep(4)
            print ("HERE SHE IS!!!")
            time.sleep(3)
            print (".....")
            time.sleep(3)
            print ("Now that we know what happened to her, we can go home and carry on research for couring sickened people.")
            time.sleep(3)
            print ("And when we find a remedy, we'll come back and try to bring this town the peace it once held.")
            time.sleep(5)
            print ("... to be continued,extended and improved...")
        if homeyesno == 2:
            time.sleep(4)
            print ("I think there is nobody around... Let's search somewhere else...")
            missionFunctionAS()
    if schoolhomechurch == "3":
        print ("- you go to the church -")
        time.sleep(3)
        print ("*whispering* Wow... this is an amazing building!... But pretty scary as well...")
        time.sleep(3)
        print ("LOOK! There she is... At the confessional...")
        time.sleep(3)
        print (" ......... ")
        time.sleep (3)
        print ("Now that we know what happened to her, we can go home and carry on research for couring sickened people.")
        time.sleep(3)
        print ("And when we find a remedy, we'll come back and try to bring this town the peace it once held.")
        time.sleep(5)
        print ("... to be continued,extended and improved...")


def rightpathFunction(): #which path to get into the town? -> 1. stop to be kicked out of the game!!
    time.sleep(4)
    print ("There are two ways to get into the city: We could climb the wall (1) or take the secret tunnel (2).\nBoth have their risks.")
    time.sleep(4)
    print ("If we climb the wall and somebody sees us we will be dead by the time we arrive on the other side.\nIn the tunnel there are poisonous plants that randomly eject deadly gas that kills immediatly when inhaled.")
    time.sleep(4)
    chosenPath = input ("Which path should we try? 1 or 2 ? \n")
    time.sleep(3)
    print ("Okay... Let's take it! There's no wrong or right decision, it's just good or bad luck for us right now...")
    time.sleep(3)
    correctPath = random.randint(1,2)
    if str(chosenPath) == str(correctPath):
        print ("YES! You are amazing! We're inside! You will make all the decisions from now.")
        missionFunction()
    if str(chosenPath) != str(correctPath):
        print ("Oh....")
        time.sleep(3)
        print ("Ooooooooh NO !!!")
        time.sleep(3)
        print ("We...won't make it.")
        time.sleep(3)
        print ("----------------------------game over------------------------------")
        time.sleep(3)
        playAgain = input ("Do you want to choose again?")
        if playAgain == "yes" or playAgain == "Yes" or playAgain == "yes!":
            rightpathFunction()
        else:
            print ("Okay! See you next time!")

def stillinFunction():
    stillin = input ("Are you still willing to come with us?\n")
    if stillin == "yes" or stillin == "Yes" or stillin == "yes!":
        print ("Wow, I'm impressed. We can really need somebody brave like you. Which path shall we take?")
        rightpathFunction()
    if stillin == "no" or stillin == "No" or stillin == "no!":
        print ("Okay... We totally understand your choice. Goodbye!")
    if stillin != "yes" and stillin != "Yes" and stillin != "yes!" and stillin != "no" and stillin != "No":
        stillinFunction() #still in after hearing the whole story #still in after hearing the whole story?

def wholestoryFunction(): #beeing told the whole story
    wholestory = input("Do you still want to hear the whole story? You might change your mind after hearing it...\n")
    if wholestory == "yes" or wholestory == "Yes" or wholestory == "yes!":
        print ("So you seem to be one of the fearless ones...")
        time.sleep(3)
        print ("Fine. Here's the end of the tragedy.")
        time.sleep(3)
        print ("One day the little daughter of the mayor got missing.")
        time.sleep(3)
        print ("Nobody could imagine what could've happened to her, the girl was a good kid who woul've never run away.")
        time.sleep(4)
        print ("Moreover there was no ransom demand or anything similar that could've indicated a kidnap.")
        time.sleep(4)
        print ("The whole town was searching for her for 3 whole months.")
        time.sleep(3)
        print ("But then one day, all of a sudden, she came home to the front door as if nothing had ever happened.")
        time.sleep(5)
        print ("She was completly unharmed and at first showed no signs of mental problems,\nexcept that she couldn't remember anyhting from the last quarter of the year.")
        time.sleep(6)
        print ("The little girl went back to school just one day after beeing back home and everything seemed normal until the some kids\nin the class of the little girl got sick.")
        time.sleep(6)
        print ("From one day to another, they lost their ability to speak.")
        time.sleep(4)
        print ("In addition, they had anxiety attacks and always their face was contorted with pain. No doctor knew what to do...")
        time.sleep(4)
        print ("At first many children fell ill, but when the first parent fell sick, everyone knew, that it was a matter of time when the whole town would be infected.")
        time.sleep(4)
        print ("Panic broke out. Parents needed to decide - stay with their infected children or leave before it was too late.")
        time.sleep(5)
        print ("Many people left. But some stayed...")
        time.sleep(3)
        print ("And they still 'live' there. But they became something similar to zombies.")
        time.sleep(4)
        print ("Until now, they couldn't leave because of the wall the fleeing inhabitants built around their hometown,\nbut it's getting ramsackle...")
        time.sleep(5)
        print ("We need to get in and find the cause for the virus or whatever it may be, before they break out and infect all the adjoining villages.")
        stillinFunction()
    if wholestory == "no" or wholestory == "No" or wholestory == "no!":
        print ("I understand, let's just get in there and try to get out alive. You can choose which path we take to go there.")
        rightpathFunction()
    if wholestory != "yes" and wholestory != "Yes" and wholestory != "yes!" and wholestory != "no" and wholestory != "No":
        wholestoryFunction()
    #else:
        #wholestoryFunction()


def mainFunction():#mainFunction
    nameFunction()
    readyFunction()
    wholestoryFunction()
\$\endgroup\$
  • 1
    \$\begingroup\$ I noticed you commented out "schoolFunction" by using an octothorpe on each line. You can actually do this much easier by just turning it into a docstring by putting three quotation marks on either side of it. It would be easier than having to type that for however many lines. I would never recommend leaving either case in for code, but while you are developing and learning it can help. Example """This is my docstring, the parser will (sortof) ignore it""" \$\endgroup\$ – Erich Sep 2 '18 at 21:15
  • 1
    \$\begingroup\$ Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. \$\endgroup\$ – Mathias Ettinger Sep 7 '18 at 11:17
1
\$\begingroup\$

DRY Principle

Gurkan Cetin has mentioned this. This code is a lot simpler than yours. A lot cleaner as well.

time_per_word=0.4
def display(text):
    print(text)
    sleep(len(text.split())*time_per_word)

story=["A few miles away,",
        ...
        "... and everything changed."
    ]
def story_mode():
    for x in story:
        display(x)

Even better is to store all the story lines in a text file and read it simply by using

story=open("story.txt").readlines()

Then you can supply each line to the display function as shown.

You can even have multiple stories at multiple times and load each with the same function. Do not make functions out of the program flow. Instead, make functions out of reusable parts of the program.

Learn and Use Simple Data Structures

A tree is a perfect use of this type of general (question/answer/conditional next question) type of programs. Read about trees here. Implement a tree (python does not have an inbuilt type). Each node carries a question and responses to the question along with the parent's response. Then take all the children from a node and calculate the next question/node.

def get_next_node(present_question,answer_choices,chidrens_of_node):
    user_answer=get_input(present_question,answer_choices)
    for node in childrens_of_node:
        if node.parents_response==user_answer:
            return node

def get_input(input_prompt,input_options)
    lower_input_options=[input_option.lower() for input_option in input_options]
    user_input=input(input_prompt).lower() 
    while(user_input not in lower_input_options):
        user_input=input(input_prompt).lower()
    return user_input

Load your tree into memory using a text file as well.
These short reusable functions make your life a whole lot easier. Even if you have 10 answers to a given question giving 10 possible solutions it will still work. And the story can span across many nodes giving you thousands of different storylines.

The ready function is recursive. That is why you are having problems Do not write a recursive function when you don't need to and don't understand it. Read about recursion here. It is harder to implement recursion correctly even for very experienced programmers so keep that in mind.

\$\endgroup\$
3
\$\begingroup\$

A few remarks:

  1. Sleep duration is hard coded, it can be automatically calculated (depending on the text length).
  2. Display can be a function (including the sleep function).
  3. So you can just pass the string, it prints and waits.
  4. Instead of comparing the user input to Yes, yes and YES, you can simply compare the uppercase’d input to YES.
  5. Game design: you can consider having a kind of map (linked list, or some other data structure) for the players location (scenes), and another data structure for the game state (a simple inventory, like key, book, secret words, etc). This will make developing the game more an artwork rather than typing in the game into the code.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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