我正在嘗試使用 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: " )
uj5u.com熱心網友回復:
最好通過輸入事件回圈來處理具有不同功能的命令來制作這樣的狀態機:
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處理玩家命令的方式在游戲中移動!
uj5u.com熱心網友回復:
你可以嘗試將作業與每個事件,而不是這種復雜的功能while和if結構...然后呼叫內部彼此不同的功能,你希望他們發生。
例子:
def checkpoint_1():
input = ....
if ....:
checkpoint_2()
else:
checkpoint_3()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/329045.html
上一篇:查找一行中第一次出現的負值
下一篇:如何在串列中列印出正確的答案
