在 Python 中使用 while 循环的游戏检查点

Checkpoint in a game using while loop in Python

我正在尝试制作一款冒险游戏,在 python 中设置检查点,当你输了、回答错误或再次玩游戏时会回到游戏中的某个点,但它总是让我回到错误的点或一遍又一遍地循环某些部分。我不知道该怎么办。 Python 下面的代码(很长而且有点复杂):

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

最好通过具有处理具有不同功能的命令的输入事件循环来制作一种像这样的状态机。

首先将所有不同的检查点定义为函数,然后启动命令处理循环:

def at_the_gate(command):
  if command == "open":
    print("You opened the gate")
    return inside_the_gate

at_the_gate.moves = ["open"]

def inside_the_gate(command):
  if command == "look":
    print("There is a big orc blocking the way")
    # Looking around does not change the game much.
    # Return this function again to stay in the same state.
    return inside_the_gate
  elif command == "fight":
    print("You're fighting a big orc")
    # You will have to make a new function here
    return fighting_the_orc

inside_the_gate.moves = ["look", "fight"]

# This is where the game begins: `at_the_gate`
state_function = at_the_gate
# The game engine works simply by repeatedly
# asking the user for input
while True:
  command = input("> ")
  new_state = state_function(command)
  if new_state is None:
    # The `state_function` didn't know what to do
    # with the command.
    print("This is not a valid move, choose from:")
    print(", ".join(state_function.moves))
  else:
    # By changing the `state_function` you can have each
    # player command processed differently based on which
    # "checkpoint" they are at.
    state_function = new_state

您可以看到这样您的代码布局保持非常平坦,您可以通过更改处理玩家命令的 state_function 在游戏中四处移动!每个检查点函数要么处理命令和 returns 一个新的检查点函数,要么命令未被处理(函数 returns None)并且玩家被告知他们的移动无效。

您可以尝试为每个事件使用函数而不是这种复杂的 whileif 构造...然后在您希望它们发生时调用彼此内部的不同函数。

示例:

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