基于文本的游戏输入

Text based game input

我可能只是太笨了,但我真的想不通。我正在制作一个基于文本的游戏,为了模拟角色在不同场景中的移动,我使用了以下代码:

 import random

 north = ("There is a path to the North. ")
 south = ("There is a path to the South. ") 
 east  = ("There is a path to the East. ")
 west = ("There is a path to the West. ")

 nsew = [north, south, east, west]

 print ("You find yourself in a woodland clearing. ")

 print (random.choice(nsew))
 print (random.choice(nsew))
 print (random.choice(nsew))
 print (random.choice(nsew))

 direction = input("Which way do you go? ")

这应该会打印列表中的四个随机项目,确实如此。我希望能够做类似的事情:

if direction == ("North"):
    print ("You decide to go North.")

但是每个都有一个 if 语句,这样如果用户键入 "east" 程序将响应 "you decide to go East"

谢谢

嗯,您首先读取用户输入,将所有字母设置为小写,然后将结果字符串与 'north'、'south'、'east' 和 'west' 进行比较.

import random

paths = []
tiaptt = "There is a path to the "
nsew = ["north", "south", "east", "west"]
amount = random.randrange(1,4)


for path in random.sample(nsew,amount):
    print tiaptt+path
    paths.append(path.lower())

direction = raw_input("Which way do you go? ")

try:
    index = paths.index(direction.lower())
    print "You have chosen to go %s"%paths[index]
except:
    print "You have chosen a wrong direction"

希望这对您有所帮助。它适用于 python 2.7,如果您使用 3+,您可能需要将我的 raw_input 更改为输入。并将 () 放在打印件周围。

print ("You have chosen to go %s"%paths[index])