English 中文(简体)
粉碎 如果在“init”类内就餐;这项工作吗?
原标题:Python: If-elses inside a class "init"; does this work?

我再说一遍,另一个新方案是“学习艰难之路”,是针对我不完全理解的。

我说,虽然我确实享有他的教训计划,但一个不幸的副作用似乎是,我不理解参与方案拟订世界的许多技术讨论。 我花了两天时间来说明一下什么是即时的,甚至现在我只想到,他。 因此,如果我担心自己是多余的话,那么对我的问询的答案就已经以技术上更清晰的方式提出。

不管怎么说,我试图在这里做的是,用班子描述每个房间,完成文字的冒险。 我实际上做的是向这个新老的、更多的“OOP”做事方式进口矿工,改用基本上为980条功能线的节目。

因此,我以前在每个职能中都有这些房间,可以检查你是否达到某些游戏目标,向你们描述房间。 例:

def room_one():
    if "flashlight" in pack:
        print "You see an exit to the NORTH and a bear to the EAST."
    elif "flashlight" not in pack:
        print "It s too dark to see."

会议室,但适当说明。 因此,在尝试使用教室时,我似乎击中了隔离墙。 我向各位展示了Im的代码,用来界定游戏机和房间。

class Runner(object):
    def __init___(self):
        self.goals = [] #an array for game achievements (talked_to_king, etc)
        self.pack = [] #an array for game items (flask, key, etc)

    def play(self, currentroom, gamestate):

        while True:

            print "
--------"
            print currentroom.desc #this line prints the room description, could
            cmd = raw_input("> ")  #it also be part of the problem?
            gamestate.state = currentroom.actions(cmd)

            if gamestate.state != "SAME":
                currentroom = gamestate.state

            else:
                pass

class Room(object):
    def __init__(self):
        self.desc = "null" #short room description
        self.lookdesc = "super null" #longer room description for the LOOK command


    def actions(self, cmd):
        if  help  in cmd:
            print """
             look  -- see your surroundings, including exits.
             get  -- to pick up an item (if it can be picked up).
             inventory  -- displays your collected items
             throw  -- some objects can be thrown
             talk  -- speak to someone
            Other commands, or objects to interact with, will tend to
            show up IN UPPERCASE. (but still type them in lowercase!)
            Cardinal directions (north, south, east, west) for movement.
            """
            return "SAME"
        elif  inv  in cmd:
            print "--Inventory--
"
            for items in game.pack:
                print items
            print "-------------
"
            return "SAME"
        elif  look  in cmd:
            print self.lookdesc
            return "SAME"
        elif  goals  in cmd:
            for goal in game.goals:
                print goal
            return "SAME"
        else:
            print "That won t work here."
            return "SAME"


class Office(Room):

    def __init__(self):

        super(Office, self).__init__()
        # here s the part that doesn t work
        if  principal  in game.goals:
            self.desc = "You re in the principal s office, and there s a zombie in here!"
        else:
            self.desc = "You re in the principal s office."

        self.lookdesc = "The walls in here are dingy from decades of smoking.
A door to the WEST leads back to the foyer."



    def actions(self, cmd):

        if "west" in cmd and not "principal" in game.goals:

            print "Buck up and talk to the old prune."

            return "SAME"

        elif "talk" in cmd:

            print "You call to the principal."
            game.goals.append( principal )
            next = raw_input("--Mr. Friiiiinkseseeees...--")
            print "But OH MY DAMN he s actually a zombie now."
            next = raw_input("--Aww weak--")
            return "SAME"

        return super(Office, self).actions(cmd)

为了检查“俱乐部”阵列是否被附在后面,我确实在安放室母班子检查功能,并且确保了足够的游戏。 目标附后。 但是,我的发言似乎实际上没有做任何事情;不管目标阵列中的内容如何,我总是在我回到会议厅时拿到其他东西。

公平地把脚石放在房间里,就像在黑暗中打上那种枪一样,事实上,我感到惊讶的是,我拿不出一个错误的信息来告诉我,他们不是什么地方! 但它似乎忽视了游戏的内容。 目标阵列告诉我,这是这样做的最佳途径。 我如何执行?

作为一种分点——如果游戏改变足以使房间内容大为改变,那么,是否认为最好为被改变的房间设置一个整整类其他教室? 维持“地图”和说Three室(Room)总是是会议室Three,无论发生了什么样的游戏改变,还是如果到游戏回到会议室时,那的位置是shed、洗劫、大火、整 full和lett,难道这只是一个完全不同的会议室吗? 我希望而不是,因为让每个房间都只是这个房间,让像游戏这样的东西内容,似乎更为合理。 目标和游戏。 包装改变了班级的情况,而不是仅仅在满足某些条件时进行新班。

不管怎么说,这种情况早已减少。 tl;dr - Am I do it weird?

EDIT AGAIN:按照这里的人的建议,清理了主要问题代码,以达到新法典,并增加了“Room”母类别,看看这是否是问题的一部分。 然而,我确实认为,该法典的质量有所改善。

FINAL EDIT(希望): Aha, Stone的回答再次非常有益。 自2006年以来 我将“同声”送回我的主人,正在使用老的办公室。 我需要创立一个全新的案例,以便让它认识到这一变化;我通过将“办公室”而不是“SAME”归还来做到这一点。 感谢大家不仅帮助解决手头的问题,而且帮助使《守则》更容易阅读,并帮助我改变我对我的话语句的想法。 Mwah :D

最佳回答

当你有100个房间时,情况如何? 如果发言,你的作用是100? (基本上再次提出)

在游戏改变时,直接与你想要的东西挂钩可能更好。

编辑:我只读一下你现在面临的问题似乎是显而易见的,除非你放弃重要的法典。

The only time that you appear to add principal to to goals is inside of a member function of class Office. Therefore, by the time that you get around to adding principal to goals, it s too late, Office has already been initialized and won t be re-initialized unless you create a new instance of Office. You ll need to update the room s description after you add that goal.

问题回答

I suppose by this:

    if not  principal  in game.goals:

这意味着:

    if  principal  not in game.goals:

    if not ( principal  in game.goals):


办事处的目标在哪里建立? 由于你只是检查制造物体的条件,因此,如果你再次使用同一物体,则说明不会改变。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签