【问题标题】:Checkpoint in a game using while loop in Python在 Python 中使用 while 循环在游戏中检查点
【发布时间】:2021-10-21 10:52:00
【问题描述】:

我正在尝试使用 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: " )
                                    

【问题讨论】:

  • 欢迎来到 SO!你的问题有点太具体了,你的代码也很复杂。您的游戏将变得越复杂,您就越需要将这些while checkpoint == ? 语句放在任何地方。这将成为不可能完成的任务!相反,创建一个处理玩家命令的输入循环,然后赋予这些不同的命令函数。

标签: python if-statement while-loop checkpoint


【解决方案1】:

您可以尝试为每个事件使用函数,而不是使用这种复杂的 whileif 构造......然后根据需要在彼此内部调用不同的函数。

例子:

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

【讨论】:

    【解决方案2】:

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

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

    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 在游戏中移动!每个检查点函数要么处理命令并返回一个新的检查点函数,要么未处理命令(函数返回None)并通知玩家他们的移动无效。

    【讨论】:

      猜你喜欢
      • 2019-06-03
      • 2014-10-30
      • 2013-10-01
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 2016-11-22
      相关资源
      最近更新 更多