我正在创建一个基于文本的冒险游戏。角色正在浏览由城市街区组成的地图。我有一个direction_function,它获取raw_input,然后将字符移动到正确的相邻块中。然而,我有特殊的功能,比如在大多数街区上要拿起的物品或要与之互动的人。这里我也使用了raw_input。如果他们输入了正确的关键字,他们就会进行交互,但如果他们通过输入方向来忽略这些关键字,则会将他们传递到direction_function中,从而再次提示他们输入raw_input。有没有办法将他们的初始答案传递到direction_function中,这样他们就不必重复他们的答案了?
这是我的direction_function:
def direction_function(left, right, up, down, re):
direc = raw_input(">")
if direc in west:
left()
elif direc in east:
right()
elif direc in north:
up()
elif direc in south:
down()
elif direc in inventory_list:
inventory_check()
re()
else:
print "try again"
re()我为每个块指定一个函数,如下所示
def block3_0():
print "You see a bike lying in your neighbor's yard. Not much else of interest."
direc = raw_input(">")
if direc in ("take bike", "steal bike", "ride bike", "borrow bike", "use bike"):
print "\n"
bike.remove("bike")
school_route()
else:
direction_function(block2_0, block4_0, block3_1, block3_0, block3_0)发布于 2012-08-31 00:31:12
您可以在direction_function上使用默认参数值来传递上一次对raw_input的最终调用的结果,例如:
def direction_function(direction=None):
direction = direction or raw_input()
# Do something with the input如果没有提供方向(常规工作流),测试将以调用raw_input来获取一些方向结束。如果提供了一个方向(就像你已经读过的那个方向一样),它将被直接使用。
发布于 2012-08-31 00:33:27
是的,你只需要以这样的方式定义你的函数就可以了。
例如,考虑如下内容:
def direction_function(input = 'default_val'):
if input != 'default_val':
input = raw_input()
# do your stuff here有了这样的函数结构,您可以做的就是在调用direction_function()方法的代码块中检查交互值或方向条件,并将调用函数的输入值传递给它。所以如果方向是玩家选择的,那么输入应该是'default_val‘。
https://stackoverflow.com/questions/12201328
复制相似问题