Python重新游玩时生成不同随机敌人与武器的问题求助
解决文字冒险游戏重新游玩时随机值不刷新的问题
问题出在全局变量的初始化时机:enemy、weapons和items在程序启动时仅被初始化一次,重新游玩时不会重新生成新的随机选项,也不会重置物品列表。
修改方案
将全局变量的初始化逻辑移到play_game()函数内部,每次启动新游戏时重新生成随机敌人、武器,并清空物品列表。同时修正物品存储的小问题(原代码用集合存储武器,改为直接存字符串更合理)。
修改后的完整代码
import time import random def print_pause(message_to_print): print(message_to_print) time.sleep(1) def intro(enemy): print_pause("You find yourself standing in an open field, " "filled with grass and yellow wildflowers.") print_pause(f"Rumor has it that a {enemy} is somewhere around here, and " "has been terrifying the nearby village.") def choice(items, enemy, weapons): while True: choice_input = input("Enter 1 to knock on the door of the house.\n" "Enter 2 to peer into the cave.\n" "What would you like to do?\n" "Please enter 1 or 2.\n") if choice_input == '1': house(items, enemy, weapons) elif choice_input == '2': cave(items, enemy, weapons) else: print("Please enter 1 or 2.\n") def fight(items, enemy, weapons): print_pause(f"The {enemy} attacks you!") fight_choice = input("Would you like to (1) fight or (2) run away?\n") if fight_choice == '1': if weapons in items: print_pause(f"As the {enemy} moves to attack, you unsheath " f"your new {weapons}.") print_pause(f"The {weapons} of Ogoroth shines brightly in your " "hand as you brace yourself for the attack.") print_pause(f"But the {enemy} takes one look at your shiny new " "toy and runs away!") print_pause(f"You have rid the town of the {enemy}. " "You are victorious!") play_again() else: print_pause(f"You feel a bit under-prepared for this, " "what with only having a tiny dagger.") print_pause(f"You do your best...") print_pause(f"but your dagger is no match for the {enemy}.") print_pause(f"You have been defeated!") print_pause(f"G A M E O V E R") play_again() elif fight_choice == '2': print_pause("You run back into the field. Luckily, " "you don't seem to have been followed.") choice(items, enemy, weapons) def house(items, enemy, weapons): print_pause("You approach the door of the house.") print_pause(f"You are about to knock when the door " f"opens and out steps a {enemy}.") print_pause(f"Eep! This is the {enemy} house!") fight(items, enemy, weapons) def cave(items, enemy, weapons): if weapons in items: print_pause("You've been here before, and gotten all " "the good stuff. It's just an empty cave now.") print_pause("You walk back out to the field.") else: print_pause(f"You peer cautiously into the cave.") print_pause(f"It turns out to be only a very small cave.") print_pause(f"Your eye catches a glint of metal behind a rock.") print_pause(f"You have found the magical {weapons} of Ogoroth!") print_pause(f"You discard your silly old dagger and take " f"the {weapons} with you.") print_pause(f"You walk back out to the field.") items.append(weapons) choice(items, enemy, weapons) def play_again(): while True: again_choice = input("Play again? [y|n]\n") if again_choice == 'y': play_game() elif again_choice == 'n': print('Thanks for playing! Goodbye!') exit(0) else: print("Please enter y or n.") def play_game(): # 每次新游戏初始化变量 items = [] enemy = random.choice(["Troll", "Pirate", "Gorgon", "Monster"]) weapons = random.choice(["Bow", "Axe", "Bazooka", "Sword"]) intro(enemy) choice(items, enemy, weapons) play_game()
关键修改点
- 将
items、enemy、weapons的初始化移至play_game()函数内,确保每次新游戏都生成新值 - 将这些变量作为参数传递给需要使用它们的函数,避免依赖全局变量,代码更规范
- 修正物品存储逻辑:从
items.append({weapons})改为items.append(weapons),同时判断条件从({weapons}) in items改为weapons in items - 重命名了部分和函数名冲突的变量(比如
choice函数内的输入变量改为choice_input)
内容的提问来源于stack exchange,提问作者Luis Martinez
相关产品推荐
相关产品推荐

