让Python 3.9识别参数并采取相应的行动(交互式故事叙述)



我会尽量简洁明了。

我正在写一个交互式故事,它会根据"滚动"而改变。一个d20的骰子。我已经设法弄清楚了如何开始,但是我觉得Python实际上并没有听我给它的参数,因为它只是做了一些事情。

基本上,应该是这样的:

玩家同意他们想玩游戏->玩家掷骰子->游戏使用随机摇出的数字来决定玩家将有哪个开始。

目前的情况是,一切都很顺利,直到它应该吐出玩家所拥有的开始。它似乎不是根据我的参数来决定的。例如,你应该有"人类";如果玩家投到5或更少,则开始,并显示"精灵";从6岁到18岁开始。以下是昨天发生的事情:

venvScriptspython.exe C:/Users/drago/PycharmProjects/D20/venv/main.py
Hello! Would you like to go on an adventure? y/n >> y
Great! Roll the dice.
Press R to roll the D20.
You rolled a 15!
You start as a human.
As a human, you don't have any special characteristics except your ability to learn.

相关代码如下:

def NewGame():
inp = input("Hello! Would you like to go on an adventure? y/n >> ")
if inp == "y" or inp == "yes":
print("Great! Roll the dice.")
input("Press R to roll the D20.")
print("You rolled a " + str(RollD20()) + "!")
PostGen()
else:
input("Okay, bye! Press any key to exit.")
sys.exit()
def PostGen():
if RollD20() <= 5:
print("You start as a human.")
PostStartHum()
elif RollD20() >= 6:
print("You start as an elf.")
PostStartElf()
elif RollD20() >= 19:
print("You lucked out, and can start as a demigod!")
PostStartDemi()

def RollD20():
n = random.randint(1, 20)
return n
def PostStartHum():
print("As a human, you don't have any special characteristics except your ability to learn.")
def PostStartElf():
print("As an elf, you have a high intelligence and a deep respect for tradition.")
def PostStartDemi():
print("As a demigod, you are the hand of the gods themselves; raw power incarnated in a human form...")
print("However, even mighty decendants of gods have a weakness. Be careful."

谢谢你的帮助。

  1. 将您的PostGen函数转换为以下内容:
def PostGen(rollValue):
if rollValue <= 5:
print("You start as a human.")
PostStartHum()
elif rollValue >= 6:
print("You start as an elf.")
PostStartElf()
elif rollValue >= 19:
print("You lucked out, and can start as a demigod!")
PostStartDemi()
  1. NewGame功能更改为:
def NewGame():
inp = input("Hello! Would you like to go on an adventure? y/n >> ")
if inp == "y" or inp == "yes":
print("Great! Roll the dice.")
input("Press R to roll the D20.")
rollValue = RollD20()
print("You rolled a " + str(rollValue) + "!")
PostGen(rollValue)
else:
input("Okay, bye! Press any key to exit.")
sys.exit()

每次调用RollD20()时都生成一个新的随机数。你需要将该值存储在某个地方,并在游戏会话中重用它。

每次呼叫RollD20,您将获得一个新的随机数。因此,如果您想在多个if中使用相同的随机数,则需要将该值放入另一个变量中。

def NewGame():
inp = input("Hello! Would you like to go on an adventure? y/n >> ")
if inp == "y" or inp == "yes":
print("Great! Roll the dice.")
input("Press R to roll the D20.")
result = RollD20()
print("You rolled a " + str(result) + "!")
PostGen(result)
else:
input("Okay, bye! Press any key to exit.")
sys.exit()

从那里你改变PostGen()接受结果:

def PostGen(result):
if result <= 5:
print("You start as a human.")
PostStartHum()
elif result >= 6:
print("You start as an elf.")
PostStartElf()
elif result >= 19:
print("You lucked out, and can start as a demigod!")
PostStartDemi()

最新更新