如何使用python重置游戏中的随机选择
我正在学习用python编写一个冒险游戏。我对代码运行的方式很满意,只是当玩家选择再次玩游戏时,它会重复相同的旧生物选择。我希望每当玩家选择再次玩游戏时,它会做出新的生物选择,并将选择保持到回合结束。这是我的代码:如何使用python重置游戏中的随机选择,python,Python,我正在学习用python编写一个冒险游戏。我对代码运行的方式很满意,只是当玩家选择再次玩游戏时,它会重复相同的旧生物选择。我希望每当玩家选择再次玩游戏时,它会做出新的生物选择,并将选择保持到回合结束。这是我的代码: import random import time creatures = ["wicked fairy","gorilla","huge beast","maskquarade","Giant"] def get_random_creature(): creature
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()
问题是
bioture=get\u random\u bioture()
中的biggining只调用了一次get\u random\u bioture()
。
每次调用时,您intro()
函数都使用相同的生物
变量(已存储)
您可以通过将生物=get_random_biote()
添加到您的介绍函数中,轻松解决此问题:
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")
希望这有助于研究Ary的答案,我会在游戏开始时将该生物变量声明为None,然后在intro()函数中用随机值填充它 示例如下:
creature = None
def print_pause(str):
print(str)
time.sleep(0.1)
def print_pause_longer(str1,str2,str3):
print(str1,str2,str3)
time.sleep(0.1)
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")
您还需要将其作为参数添加到所有使用生物名称的函数定义中,否则它将仅在简介的函数上下文中可用。或者您可以在开始时将其声明为“无”,然后在简介中更改它。您是对的@BoboDarph。没有深入查看代码,但忽略了以下内容:(你的代码无论如何都会工作的,因为如果他在main和intro函数中保持它为随机的,它仍然在上下文中。如果他只是将它从main移到intro,它会因为上下文而失效。如果他在这两个地方声明它为随机的,调试它将是一个有趣的时间。非常感谢,但它没有工作。r因此,每当代码中提到变量“生物”时,就会调用一个新的生物。计划是随机挑选一个生物,并在游戏回合中保持其价值,直到玩家选择“再次玩”选项,然后应该有另一个随机挑选