[英]Branching dialogue in python
我在業余時間自學python。 我只研究了幾天,所以為了練習,我一直在處理各種 raw_input 行。 目前我正在嘗試做一個簡短的文字冒險,供朋友試用。 我碰到了一個街區,它的一部分詢問用戶是否想進入一個村庄。 如果他們選擇進入,一切正常,但如果他們選擇不進入,我需要它跳過村內發生的所有對話和輸入。 如何讓它跳到頁面下方的輸入?
這是非常基本的,但在這里:
name = raw_input("What is your name adventurer? ")
where = raw_input("You are in a magical forest, you can go North or East, which
way do you go?")
if where.lower() == "north":
troll = raw_input("You encounter a troll, what do you do? Attack or flee? ")
else:
print("You encounter a dragon, you are dead, GAME OVER")
if troll.lower() == "attack":
print ("You have no weapon, that was a bad choice, you are dead, GAME OVER")
else:
flee = raw_input("You flee further North, you encounter a village, do you
enter or continue? ")
if flee.lower() == "enter":
army = raw_input("As you enter the village, a knight hands you a sword,
assuming you are there to join the army. Join him or leave the village? ")
else:
print ("You ignore the village and continue down the road.") #jump from here
if army.lower() == "join":
print ("You head off to war and are killed almost instantly because you are an
untrained fool")
else:
print ("You are kicked out of the village")
dark = raw_input("It is getting dark, do you continue or seek shelter") #to here
if dark.lower() == "continue":
print ("You are attacked by vampires and killed, GAME OVER")
else:
caves = raw_input("You find two caves, do you pick cave one which is pitch
black, or two which has light coming from it? ")
if caves.lower() == "one":
print ("You enter the cave, light a small fire, and sleep for the night.")
else:
print ("You enter the cave, there are bandits inside, you are murdered, GAME OVER")
我想在第一條粗線之后跳到黑暗。 如果發現其他任何可以修復或改進的內容,請務必說出來。 歡迎所有批評。
更改此塊:
if flee.lower() == "enter":
army = raw_input("As you enter the village, a knight hands you a sword,
assuming you are there to join the army. Join him or leave the village? ")
else:
print ("You ignore the village and continue down the road.") #jump from here
if army.lower() == "join":
print ("You head off to war and are killed almost instantly because you are an
untrained fool")
else:
print ("You are kicked out of the village")
dark = raw_input("It is getting dark, do you continue or seek shelter")
進入這個區塊:
if flee.lower() == "enter":
army = raw_input("As you enter the village, a knight hands you a sword,
assuming you are there to join the army. Join him or leave the village? ")
if army.lower() == "join":
print ("You head off to war and are killed almost instantly because you are an
untrained fool")
else:
print ("You are kicked out of the village")
else:
print ("You ignore the village and continue down the road.")
dark = raw_input("It is getting dark, do you continue or seek shelter")
這將army
輸入移動到enter
塊的內部,以便它只有在進入該語句時才會執行。
一些注意事項:您可能希望將所有這些都放在main()
函數中,並在您死后調用return
以便它不會繼續。 另一種選擇,如果你不想使用一個函數,就是把所有東西都放在一個try
塊中,如果你死了就調用raise
,然后用你的except
塊處理死亡。 你說你是初學者,所以你可能還沒有學過這些東西,但這絕對是值得研究的東西
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.