我正在学习使用 python 编写冒险游戏。我对代码的运行方式感到满意,只是当玩家选择再次玩游戏时,它会重复相同的旧生物选择。我希望每当玩家选择再次玩游戏时它都会做出新的生物选择并保持选择直到回合结束。这是我的代码:
import random
import time
creatures = ["wicked fairy","gorilla","huge beast","maskquarade","Giant"]
def get_random_creature():
creature = random.choice(creatures)
return creature
creature = get_random_creature()
def print_pause(str):
print(str)
time.sleep(2)
def print_pause_longer(str1,str2,str3):
print(str1,str2,str3)
time.sleep(3)
def intro():
print_pause("You find yourself standing in an open field")
print_pause_longer("Rumour has it that a",creature,"is somewhere around here,")
print_pause("it has been terrorizing a nearby village")
print_pause("In front of you are two passage ways.")
print_pause("Left and Right")
print_pause("Enter 'L' to go left")
print_pause("Enter 'R' to go right")
def go_left():
print_pause("You take left, and find an old cave")
print_pause("What would you like to do?")
print_pause("Enter 1 to get into the cave")
print_pause("Enter 2 to go back to the open field")
choice2 = input()
if choice2 == "1":
in_cave()
elif choice2 =="2":
open_field()
else:
go_left()
def in_cave():
print_pause("You push open the door of cave")
print_pause("The first thing you saw was a shinny sword that spinned around on its own")
print_pause("what would you like to do next?")
print_pause("Enter 1 to collect the sword")
print_pause("Enter 2 to leave the cave")
choice3 =input()
if choice3 == "1":
collect_sword()
elif choice3 == "2":
leave_cave()
else:
in_cave()
def collect_sword():
print_pause("You reach out for the sword and collect it")
print_pause("Then you walk out of the cave")
print_pause("As you moved some miles from the cave")
print_pause("You heared a loud noise")
print_pause_longer("Sounds like the",creature,"grrrrrrrrrrrrrrrrrh!!!")
print_pause("It is right behind you")
print_pause("what would you do next?")
print_pause("Enter 1 to run away")
print_pause("Enter 2 to fight")
choice4 = input()
if choice4 == "1":
run_away()
elif choice4 == "2":
win_fight()
else:
collect_sword()
def run_away():
print_pause("You make an attempt to run away")
print_pause_longer("The",creature, "catches up with you")
print_pause("You are defeated")
play_again()
def win_fight():
print_pause("You turn around to fight")
print_pause_longer("As the ",creature, "beheld your shinny sword, it fell dead")
print_pause_longer("You have rid the town of the",creature,"Yay!!")
print_pause("You are Victorious")
play_again()
def lost_fight():
print_pause_longer("You face the ", creature," with courage")
print_pause("But in no time")
print_pause("You discovered you've just engaged in a lost battle")
print_pause("You have been defeated")
play_again()
def leave_cave():
print_pause("You leave the cave")
print_pause("You take right, and find a house")
print_pause("You pushed the door open to get in")
print_pause_longer("AHHHH!!!", "it is the house of the", creature)
print_pause("You were Terrified")
print_pause("Enter 1 to run away")
print_pause("Enter 2 to fight back")
choice5 = input()
if choice5 == "1":
run_away()
elif choice5 == "2":
lost_fight()
else:
leave_cave()
def open_field():
print_pause("You hurry back to the open field")
print_pause("In front of you are two passage ways.")
print_pause("Left and Right")
print_pause("Enter 'L' to go left")
print_pause("Enter 'R' to go right")
play_game()
def go_right():
print_pause("You take right, and find a house")
print_pause("You pushed the door open to get in")
print_pause_longer("AHHHH!!!", "it is the house of the", creature)
print_pause("You were Terrified")
print_pause("Enter 1 to run away")
print_pause("Enter 2 to fight back")
choice6 = input()
if choice6 == "1":
run_away()
elif choice6 == "2":
lost_fight()
else:
go_right()
def play_game():
choice1 = input()
if choice1 == "L":
go_left()
elif choice1 == "R":
go_right()
else:
print_pause("Sorry,i don't get you")
intro()
play_game()
def play_again():
print_pause("Would you like to play again, yes or no")
play_again = input("y or n\n")
if play_again == "n":
print_pause("Thanks for playing! See you next time.")
elif play_again == "y":
print_pause("Excellent! Restarting the game...")
intro()
play_game()
else:
play_again()
intro()
play_game()
最佳答案
问题在于,get_random_creature()
仅在 creature = get_random_creature()
中的 biggining 中调用一次。
每次调用 intro()
函数时,它都会使用相同的 creature
变量(已存储)
您可以通过将 creature = get_random_creature()
添加到介绍函数中来轻松解决此问题:
def intro():
creature = get_random_creature()
print_pause("You find yourself standing in an open field")
print_pause_longer("Rumour has it that a",creature,"is somewhere around here,")
print_pause("it has been terrorizing a nearby village")
print_pause("In front of you are two passage ways.")
print_pause("Left and Right")
print_pause("Enter 'L' to go left")
print_pause("Enter 'R' to go right")
希望对你有帮助
关于python - 如何使用 python 重置游戏中的随机选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54456806/