Search code examples
pythonif-statementwhile-loopcheckpoint

Checkpoint in a game using while loop in Python


I am trying to make an adventure game with checkpoints in python leading back to a certain point in the game when you lose, get an answer wrong or to play again but it keeps bring me back to the wrong point or looping some sections over and over again. I'm not sure what to do. Python Code down below (Its very long and a bit complicated):

checkpoint = 1
checkpoint2 = 2
checkpoint3 = 3
checkpoint4 = 4
checkpoint5 = 5
checkpoint6 = 6

checkpoint3b = 3
checkpoint4b = 4
checkpoint5b = 5
checkpoint6b = 6
again = 'k'

while again == 'k':
    
    print("Welcome to your adventure")
    play = input("do you wish to play. y for yes, n for no: ")
    while play == "y":
        
        name = input("Great! Pick a name for your character: ")
        
        while checkpoint == 1:    
            intro = input("Hello " + name + ". It is a sunny morning and your in front of the school. Do you wish to go in or skip school. y to skip, n to go to school: ")
            if intro is "y":
                
                while checkpoint2 == 2:    
                    path = input("Ok! You decide to take a walk through the forest and come across two paths. Path 1 leads to a bright and glittery land with faries. Path 2 leads to a dry and isolated desert. Pick a path!(1 or 2): ")
                    if path is "1":
                        fairy = input("Hi "+ name + ". I am the fairy of this forest and I will help you on your quest today. Your first task will be to choose a name for me! Whats my name: ")
                        
                        while checkpoint3 == 3:    
                            storyline1 = input("Thats an awesome name! Now follow me down this path.......... Oh no! The evil fairy has sent down a troll to deter you. What should we do.(fight = 1, run = 2): ")
                            if storyline1 == "1":
                                battle1 = ("BOOM!......... WOW! Good job, you deafeated the troll, but what you didn't know was that I'm the EVIL FAIRY! That was my troll that you defeated but now I'm going to defeat you. MUHAHAHAHA!!!")
                                print(battle1)
                                contin = input("Type ok to continue: ")
                                
                                while checkpoint4 == 4:
                                    message1 = input(" Uh oh! Your in trouble now. To defeat the evil fairy solve the following riddles: 1. The more of this there is, the less you see. What is it? (one word answer): ")
                                    if message1 == "darkness":
                                        print("Great!")
                                        while checkpoint5 == 5:
                                            
                                            riddle2 = input("Next one: 2.  It belongs to you, but other people use it more than you do. What is it? Your...(one word answer)")
                                            if riddle2 == "name":
                                                print("Well done!")
                                                
                                                while checkpoint6 == 6 and again == "k" :
                                                    riddle3 = input("Last one: 3. What has a head and a tail but no body? A...(one word answer)")
                                                    if riddle3 == "coin":
                                                        print("Amazing! You have defeated the fairy and completed the adventure!")
                                                        again = input("Enter k to play again: ")
                                                    else:
                                                        print("Sorry you have the wrong answer, try again.")
                                                        checkpoint6 = input("Enter 6 to try again: ")
                                            else:
                                                print("Sorry you have the wrong answer, try again.")
                                                checkpoint5 = input("Enter 5 to try again: ")
                                    else:
                                        print("Sorry you have the wrong answer, try again.")
                                        checkpoint4 = input("Enter 4 to try again: ")
                                        
                            else:
                                print("Sorry the troll caught up to you and gobbled you up, try again.")
                                checkpoint3 = input("Enter 3 to try again: ")
                    else:
                        sandman = input("Hi "+ name + ". I am the sandman of this desert and I will help you on your quest today. Your first task will be to choose a name for me! Whats my name: ")
                        
                        while checkpoint3b == "3":
                            storyline2 = input("Thats an sick name! Now follow me up this dune.......... Oh no! The wicked Sandmaster has sent down a warthog to defeat you. What should we do.(fight = 1, run = 2): ")
                            if storyline2 == "2":
                                battle2 = ("ZOOOOM!......... WOW! Awesome sauce, you out ran the warthog,they're not very fast after all. Unfortunately, I tricked you! HAHA, now I'm going to defeat you. MUHAHAHAHA!!!")
                                print(battle2)
                                contin2 = input("Type ok to continue: ")
                                    
                                while checkpoint4b == "4":
                                    message2 = input(" Uh oh! Your in trouble now. To defeat the evil Sandmaster solve the following riddles: 1. What has many keys but can’t open a single lock? A... (one word answer): ")
                                    if message2 == "piano" or message2 == "keyboard":
                                        print("Great!")
                                                
                                        while checkpoint5b == "5":
                                            riddle2b = input("Next one: 2.   I’m light as a feather, yet the strongest person can’t hold me for five minutes. What am I? Your...(one word answer): ")
                                            if riddle2b == "breath":
                                                print("Well done!")
                                                            
                                                while checkpoint6b == "6" and again == "k":
                                                    riddle3b = input("Last one: 3. What has hands, but can’t clap? A...(one word answer): ")
                                                    if riddle3b == "clock":
                                                        print("Amazing! You have defeated the evil Sandmaster and completed the adventure!")
                                                        again = input("Enter k to play again: ")
                                                        
                                                    else:
                                                        print("Sorry you have the wrong answer, try again.")
                                                        checkpoint6b = input("Enter 6 to try again: ")
                                            else:
                                                print("Sorry you have the wrong answer, try again.")
                                                checkpoint5b = input("Enter 5 to try again: ")
                                    else:
                                        print("Sorry you have the wrong answer, try again.")
                                        checkpoint4b = input("Enter 4 to try again: ")
                                                
                            else:
                                print("Sorry the warthog was pretty strong and knocked you out, try again.")
                                checkpoint3b = input("Enter 3 to try again: ")
            else:
                while intro == "y" and checkpoint2 == "2":
                    print("Nahh going to school is boring, lets skip school anyway.")
                    checkpoint2 = input("Enter number '2' to continue: " )
                                    

Solution

  • You can try to work with functions for each event instead of this complex while and if constructions... and then call the different functions inside each other as you want them to happen.

    Example:

    def checkpoint_1():
        input = ....
        if ....:
            checkpoint_2()
        else:
            checkpoint_3()