请我正在学习使用python编写冒险游戏代码。我对代码的运行方式感到满意,只是当玩家选择再次玩游戏时,它会重复相同的旧生物选择。我希望每当玩家选择再次玩游戏并维持游戏时,我都会重新选择新的生物。选择直到回合结束。这是我的代码:
[api]
[api.statistics]
recentErrors = 10
答案 0 :(得分:3)
问题是get_random_creature()
仅在creature = get_random_creature()
中被调用一次。
每次调用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")
希望有帮助
答案 1 :(得分:0)
根据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")