mercredi 15 août 2018

Text-based adventure game looping to previous line?

I am learning Python by creating a text-based adventure game. I was having a difficult time figuring out if I could call a previous variable. For example, this is a snippet of my code (advice on how to consolidate would be helpful haha) I have an if statement that allows the player to select an input.

If they select 1. They choose "Accept defeat".

I have the same outcome at the end, for the player to choose "Accept defeat" again. I would like to loop back to the first alien variable if that option is chosen. Rather than writing the same if, elif, statements over again. Is this possible?

   alien = input(f_y + s_b + "> ");

   if alien == "1":
        print ("")
        print (s_b + "You approach the knife slowly, While the alien is distracted. You finally reach the knife, but as you look up, the alien stares back at you.")
        print ("")
        print (s_b + "You make a move to stab the alien, but he is too quick. With one swift motion, the alien thrusts you into the air.")
        print ("")
        print (s_b + "You land hard, as the alien makes it's way towards you again. What should you do?")
        print ("")
        print (f_g + s_b + "1. Accept defeat?")
        print ("")
        print (f_g + s_b + "2. Last ditch effort?")
        print ("")

        improv = input(f_y + s_b + "> ")

        if improv == "1":
            print ("")
            print (s_b + "The alien stands over you, as you grimace in pain. You extend your arms out revealing your chest. Showing the alien you're not afrain of death.")
            print ("")
            print (s_b + "The alien stops, and brings it's face close to yours. You have gained the alien's respect, he walks slowly back to the arm he was consuming.")
            print ("")
            print (s_b + "You lie in shock, What should you do now?")
            print ("")
            print (s_b + f_g + "1. Get up and try to communicate with the alien?")
            print ("")
            print (s_b + f_g + "2. Get up and continue onward through this seemingly dangerous world?")
            print ("")

            onward = input(f_y + s_b + "> ")

            if onward == "1":
                print ("")
                print (s_b + "You take a deep breath, still in pain, you slowly limp your way towards the alien.")
                print ("")
                print (s_b + "As the alien hears you approach, it stops eating the arm and turns towards your direction.")
                print ("")
                print (s_b + "What should you say?")
                print ("")
                print (s_b + f_g + "1. 'Why didn't you kill me?'")
                print ("")
                print (s_b + f_g + "2. 'Help me escape this place.'")
                print ("")

                kill = input(f_y + s_b + "> ")


                if kill == "1":
                    print ("")
                    print (s_b + "You walk close to the alien as he stares into your eyes, you show no fear as you stare right back. 'Why didn't you kill me?' you ask.")
                    print ("")
                    print (s_b + "The alien huffs and responds in the deepest voice imaginable, 'I only kill those who have fear.'")
                    print ("")
                    print (s_b + "You respond..")
                    print ("")
                    print (s_b + f_g + "1. I have fear.")
                    print ("")
                    print (s_b + f_g + "2. You don't scare me, nothing does.")
                    print ("")

                    kill2 = input(f_y + s_b + "> ")

                    if kill2 == "1":
                        print ("")
                        print (s_b + "'I am terrified of you' you say. I am terrified of everything, I had given up.'")
                        print ("")
                        print (s_b + "The alien responds 'This is why you approach me? To express your fear? Have I misread you?'")
                        print ("")
                        print (s_b + "'You haven't misread me, I am ready to die.' you respond.")
                        print ("")
                        print (s_b + "The alien respects your wishes, 'Well, I can arrange that..'")
                        print ("")
                        print (s_b + "The alien swiftly swipes off your head, leaving your body lifeless.")
                        print ("")
                        print (s_b + f_r + "You died.. GAME OVER!! You gave up too soon..")
                        print ("")

                if kill == "2":
                    print ("")
                    print (s_b + "You limp up to the alien, brushing down your clothes.")
                    print ("")
                    print (s_b + "The alien stares at you, seemingly waiting for you to say something.")
                    print ("")
                    print (s_b + "'Help me escape this place' you say.")
                    print ("")
                    print (s_b + "'Help you?' responds the alien with a fighteningly deep voice. 'Why would you think I would help you?'")
                    print ("")
                    print (s_b + f_g + "1. Because you didn't kill me, and I need to get out of this place.")
                    print ("")
                    print (s_b + f_g + "2. Because I will kill you if you dont.")
                    print ("")


            elif onward == "2":
                print ("")
                print (s_b + "You quickly turn over and limp as far from the alien as you can.")
                print ("")
                print (s_b + "This world is dark, so it's difficult to see what's ahead.")
                print ("")
                print (s_b + "You continue onward in hopes to find a way out.")
                print ("")




        elif improv == "2":
            print ("")
            print (s_b + "You quickly scan the area for weapons and shelter. The alien is approaching quickly. You notice a black wall.")
            print ("")
            print (s_b + "There is no way you can reach the wall in time, and even if you do, the alien will just follow..")
            print ("")
            print (s_b + "What should you do?")
            print ("")
            print (f_g + s_b + "1. Attempt to run behind the wall")
            print ("")
            print (f_g + s_b + "2. Accept defeat?")
            print ("")

            wall = input(f_y + s_b + "> ")

Aucun commentaire:

Enregistrer un commentaire