Python初学者,基于文本的游戏

时间:2014-04-28 06:36:22

标签: python text text-based

我是编程游戏的新手;我是通过Learn Python the Hard Way的3/4,我对我制作的一个基于文本的游戏提出了一个问题......所以在这个游戏中我的guy被困在一个荒岛上你和你在一起有left rightinto the jungle的选项(原始输入)。选择方向后,您可以选择行走多少英里。每个方向应该具有不同的最终结果(和英里距离)。

如果您输入的数字小于目的地的里程数,系统会提示您选择“转身”或“继续”。如果您输入turn around,则表示您回到开头,你再次被要求选择一个方向。如果你输入keep going,程序将返回英里(),你可以选择新的里程数。

def miles():
        print "How many miles do you walk?"     
    miles_choice = raw_input("> ")
    how_much = int(miles_choice)    
    if how_much >= 10:
        right_dest()    
    elif  how_much < 10:
        turn()  
    else: 
        print "You can't just stand here..."
        miles() 

好的,这里有两个问题:

  1. 我如何制作它,以便如果用户最初输入的距离小于目的地距离,第二英里输入+第一英里输入==到达目的地的里程数,它将添加输入并运行我的目标函数,而不仅仅是重复里程()。

  2. 由于所有三个最终目的地都有不同的距离,我应该写三个单独的里程功能吗?是否有办法使它取决于所选择的原始方向,miles()将运行不同的端点?

  3. 我很抱歉,如果这没有舔感觉......我还在学习,我不知道如何完全解释我想要碰到的东西。

3 个答案:

答案 0 :(得分:0)

您可以在dict中存储每个方向行走的里程数,然后检查字典以查看用户是否走得足够远:

distances = {
    'right': 7,
    'left': 17,
    'forward': 4
}

direction_choice = raw_input("> ")
miles_choice = raw_input("> ")

if how_much >= distances['direction_choice']:
    right_dest()    
elif  how_much < distances['direction_choice']:
    turn()  
else: 
    print "You can't just stand here..."
    miles()

请务必正确验证并转换用户输入,我尚未解决。 祝你好运!

答案 1 :(得分:0)

我不完全理解要求(预期的行为和约束)。但是,你可以考虑将一个参数传递给你的函数(通过和参数),以传达游戏在该方向上可以达到的最大里程数。)

例如:

#!/usr/bin/env python
# ...
def miles(max_miles=10):
    print "How many miles do you walk?"
    while True:     
        miles_choice = raw_input("> ")
        try:
            how_much = int(miles_choice)
        except ValueError, e:
            print >> sys.stderr, "That wasn't a valid entry: %s" % e
            continue

        if max_miles > how_much > 0:
            break
        else:
            print "That's either too far or makes no sense"
    return how_much

...在这种情况下,您通过&#34; max_miles&#34;将最大有效里程数传递到函数中。参数,然后返回一个有效的整数(介于1和max_miles之间)。

此功能的来电者有责任根据需要致电right_dest()turn()

请注意,我已移除了对miles()的递归调用,并将其替换为while True:循环,围绕try: ... except ValueError: ...验证循环。在这种情况下,这比递归更合适。当how_much的值有效时,代码会在循环外执行break

(顺便说一句,如果你在没有参数的情况下调用miles(),那么参数将根据&#34;默认参数&#34;特征设置为10。这对Python来说是不寻常的(和Ruby)...但基本上使得参数可选用于有合理默认值的情况。)

答案 2 :(得分:0)

@Question#1:我使用了Class intern变量。您可能需要它们用于进一步编程部件,并且当您在一个方向上完成时应该将它变为零,从下一步/ lvl开始为零。

@Question#2:词典是最好的方法,self.dest。参数pos用作从字典中获取值的键。

class MyGame:
    def __init__(self):
        self.current_miles = 0
        self.dest = {'Left' : 10, 'Into the jungle' : 7, 'Right' : 22}

    def miles(self,pos):

        print "How many miles do you walk?"     
        miles_choice = raw_input("> ") 
        self.current_miles += int(miles_choice) 

    if self.current_miles >= self.dest.get(pos):
            self.miles("Right")    
    elif  self.current_miles < self.dest.get(pos):
        print "you went "+ str(self.current_miles) + " miles"
    else: 
        print "You can't just stand here..."
        self.miles(pos) 

mg = MyGame()
mg.miles('Into the jungle')
相关问题