Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 全局功能块?_Python_Function - Fatal编程技术网

Python 全局功能块?

Python 全局功能块?,python,function,Python,Function,我目前正在用Python制作一个游戏。每当你想在游戏中获得帮助时,只要键入帮助,就可以阅读帮助部分 唯一的问题是,我需要为每个级别添加一个功能块 def level_01(): choice = raw_input('>>>: ') if choice=='help': level_01_help() def level_012(): choice = raw_input('>>>: ') if choice

我目前正在用Python制作一个游戏。每当你想在游戏中获得帮助时,只要键入帮助,就可以阅读帮助部分

唯一的问题是,我需要为每个级别添加一个功能块

def level_01():
    choice = raw_input('>>>: ')
    if choice=='help':
        level_01_help()


def level_012():
    choice = raw_input('>>>: ')
    if choice=='help':
        level_02_help()
所以我想知道是否有可能为所有级别创建一个全局功能块? 当您输入help时,将进入help(),然后它会自动返回到您刚刚使用的功能块


我真的希望你能理解我的意思,我也非常感谢所有能得到的帮助。

更好的解决方案是将你所处的级别存储为一个变量,并让help函数处理所有的帮助内容

例如:

def help(level):
    # do whatever helpful stuff goes here
    print "here is the help for level", level

def level(currentLevel):
    choice = raw_input('>>>: ')
    if choice=='help':
        help(currentLevel)
    if ...: # level was beaten
        level(currentLevel + 1) # move on to the next one

更好的解决方案是将您所在的级别存储为一个变量,并让help函数处理所有帮助内容

例如:

def help(level):
    # do whatever helpful stuff goes here
    print "here is the help for level", level

def level(currentLevel):
    choice = raw_input('>>>: ')
    if choice=='help':
        help(currentLevel)
    if ...: # level was beaten
        level(currentLevel + 1) # move on to the next one

实际上,您可以将帮助函数作为参数传递,这意味着您的代码可以变成:

def get_choice(help_func):
    choice = raw_input('>>>: ')
    if choice == 'help':
        help_func()
    else:
        return choice

def level_01():
    choice = get_choice(level_01_help)

def level_02():
    choice = get_choice(level_02_help)

理想情况下,所有与界面相关的任务都应该有一个单独的模块,这样游戏和界面将是两个独立的实体。这将使那些2911行更加清晰,如果您决定更改界面(例如从命令行更改为Tkinter或Pygame),您将有一个更轻松的时间。仅我的2¢

您实际上可以将帮助函数作为参数传递,这意味着您的代码可以变成:

def get_choice(help_func):
    choice = raw_input('>>>: ')
    if choice == 'help':
        help_func()
    else:
        return choice

def level_01():
    choice = get_choice(level_01_help)

def level_02():
    choice = get_choice(level_02_help)

理想情况下,所有与界面相关的任务都应该有一个单独的模块,这样游戏和界面将是两个独立的实体。这将使那些2911行更加清晰,如果您决定更改界面(例如从命令行更改为Tkinter或Pygame),您将有一个更轻松的时间。当然,概括总是可能的。但是由于您提供的信息很少(并且假设“帮助”是唯一常见的功能),原始代码非常简单。我不会牺牲这个属性,只为每个级别节省一行代码。

当然,概括总是可能的。但是由于您提供的信息很少(并且假设“帮助”是唯一常见的功能),原始代码非常简单。我不会为了每一级节省一行代码而牺牲此属性。

处理此类问题的一个非常好的方法是使用内置的python帮助。如果添加到函数中,它们将存储在函数对象的一个名为doc的特殊属性中。您可以通过以下代码访问它们:

def example():
    '''This is an example'''

print example.__doc__
>> This is an example

you can get to them in code the same way:
def levelOne():
   '''It is a dark and stormy night. You can look for shelter or call for help'''
   choice = raw_input('>>>: ')
   if choice=='help':
       return levelOne.__doc__
class Level(object):
    HELP = 'I am a generic level'

    def __init__(self, name, **exits):
       self.Name = name
       self.Exits = exits # this is a dictionary (the two stars) 
                          # so you can have named objects pointing to other levels

    def prompt(self):
       choice = raw_input(self.Name + ": ")
       if choice == 'help':
           self.help()
       # do other stuff here, returning to self.prompt() as long as you're in this level

       return None # maybe return the name or class of the next level when Level is over

    def help(self):
       print self.HELP

  # you can create levels that have custom content by overriding the HELP and prompt() methods:

  class LevelOne (Level):
     HELP = '''You are in a dark room, with one door to the north. 
        You can go north or search'''

     def prompt(self):
       choice = raw_input(self.Name + ": ")
       if choice == 'help':
           self.help() # this is free - it's defined in Level
       if choice == 'go north':
           return self.Exits['north']
这样做是保持代码和内容之间关系更清晰的好方法(尽管纯粹主义者可能会反对,这意味着您不能使用pythons内置的帮助函数来编写程序员到程序员的文档)

我认为从长远来看,您可能会发现级别希望成为类,而不是函数——这样您就可以存储状态(有人在级别1中找到键了吗?级别2中的怪物是否还活着)并最大限度地重用代码。大致轮廓如下:

def example():
    '''This is an example'''

print example.__doc__
>> This is an example

you can get to them in code the same way:
def levelOne():
   '''It is a dark and stormy night. You can look for shelter or call for help'''
   choice = raw_input('>>>: ')
   if choice=='help':
       return levelOne.__doc__
class Level(object):
    HELP = 'I am a generic level'

    def __init__(self, name, **exits):
       self.Name = name
       self.Exits = exits # this is a dictionary (the two stars) 
                          # so you can have named objects pointing to other levels

    def prompt(self):
       choice = raw_input(self.Name + ": ")
       if choice == 'help':
           self.help()
       # do other stuff here, returning to self.prompt() as long as you're in this level

       return None # maybe return the name or class of the next level when Level is over

    def help(self):
       print self.HELP

  # you can create levels that have custom content by overriding the HELP and prompt() methods:

  class LevelOne (Level):
     HELP = '''You are in a dark room, with one door to the north. 
        You can go north or search'''

     def prompt(self):
       choice = raw_input(self.Name + ": ")
       if choice == 'help':
           self.help() # this is free - it's defined in Level
       if choice == 'go north':
           return self.Exits['north']

处理此类问题的一个非常好的方法是使用内置的python帮助。如果添加到函数中,它们将存储在函数对象的一个名为doc的特殊属性中。您可以通过以下代码访问它们:

def example():
    '''This is an example'''

print example.__doc__
>> This is an example

you can get to them in code the same way:
def levelOne():
   '''It is a dark and stormy night. You can look for shelter or call for help'''
   choice = raw_input('>>>: ')
   if choice=='help':
       return levelOne.__doc__
class Level(object):
    HELP = 'I am a generic level'

    def __init__(self, name, **exits):
       self.Name = name
       self.Exits = exits # this is a dictionary (the two stars) 
                          # so you can have named objects pointing to other levels

    def prompt(self):
       choice = raw_input(self.Name + ": ")
       if choice == 'help':
           self.help()
       # do other stuff here, returning to self.prompt() as long as you're in this level

       return None # maybe return the name or class of the next level when Level is over

    def help(self):
       print self.HELP

  # you can create levels that have custom content by overriding the HELP and prompt() methods:

  class LevelOne (Level):
     HELP = '''You are in a dark room, with one door to the north. 
        You can go north or search'''

     def prompt(self):
       choice = raw_input(self.Name + ": ")
       if choice == 'help':
           self.help() # this is free - it's defined in Level
       if choice == 'go north':
           return self.Exits['north']
这样做是保持代码和内容之间关系更清晰的好方法(尽管纯粹主义者可能会反对,这意味着您不能使用pythons内置的帮助函数来编写程序员到程序员的文档)

我认为从长远来看,您可能会发现级别希望成为类,而不是函数——这样您就可以存储状态(有人在级别1中找到键了吗?级别2中的怪物是否还活着)并最大限度地重用代码。大致轮廓如下:

def example():
    '''This is an example'''

print example.__doc__
>> This is an example

you can get to them in code the same way:
def levelOne():
   '''It is a dark and stormy night. You can look for shelter or call for help'''
   choice = raw_input('>>>: ')
   if choice=='help':
       return levelOne.__doc__
class Level(object):
    HELP = 'I am a generic level'

    def __init__(self, name, **exits):
       self.Name = name
       self.Exits = exits # this is a dictionary (the two stars) 
                          # so you can have named objects pointing to other levels

    def prompt(self):
       choice = raw_input(self.Name + ": ")
       if choice == 'help':
           self.help()
       # do other stuff here, returning to self.prompt() as long as you're in this level

       return None # maybe return the name or class of the next level when Level is over

    def help(self):
       print self.HELP

  # you can create levels that have custom content by overriding the HELP and prompt() methods:

  class LevelOne (Level):
     HELP = '''You are in a dark room, with one door to the north. 
        You can go north or search'''

     def prompt(self):
       choice = raw_input(self.Name + ": ")
       if choice == 'help':
           self.help() # this is free - it's defined in Level
       if choice == 'go north':
           return self.Exits['north']

“功能块”是指第一部分(
choice=raw_input(“>>>:”)
等)还是每个级别的帮助功能,即
level_01_help
level_02_help
,等等?也许我不理解这个问题,但调用一个函数后,你总是“回到你刚来的功能块”,你不需要做任何特殊的事情来实现这一点。当你到达
help()
函数的末尾时,它会自动发生。所谓“函数块”,你是指第一部分(
choice=raw\u input('>>:')
等)还是每个级别的帮助函数,即
level\u 01\u help
level\u 02\u help
等?也许我不理解这个问题,但你总是这样调用一个函数后,“返回到您刚刚使用的函数块”,您不必做任何特殊的操作来实现这一点。当您到达
help()
函数的末尾时,它会自动发生。
get\u choice
应该是def get\u choice(help\u func):而1:choice=raw\u输入('>>>:')if choice!=“help”:返回选项help_func()否则,在一个帮助请求后,它将返回None,级别将需要控制这种情况,并且获得的收益很少。@MarioRossi我几乎在这些方面添加了一些东西,但他认为他可能已经在帮助函数本身中处理了,因为他在原始代码中没有包含while循环。+1注意尽管如此:)
get\u choice
应该是def get\u choice(help\u func):而1:choice=raw\u input(“>>>:”),如果choice!=“help”:返回choice help\u func()否则,在一个帮助请求后,它将返回None,级别将需要控制这种情况,并且获得的收益很少。@MarioRossi我几乎在这些方面添加了一些东西,但他认为他可能已经在帮助函数本身中处理了,因为他在原始代码中没有包含while循环。+1注意(尽管如此:)我很抱歉。我不想透露太多我正在制作的游戏。当然,我可以提供更多的信息。我目前有2911行