递减函数参数(PYTHON)

时间:2016-06-20 00:35:17

标签: python function pygame arguments

我在循环中调用类似于后面的函数:

    def bigAnim(tick,firstRun):
        smallAnim(x,y,duration)
        #more anims and logic...



    def smallAnim(x, y,duration):
        duration -= 1
        if duration != 0:
            Anim.blit(screen,(x ,y))
            Anim.play()


        else:
            Anim.stop()
            loopedOnce = True
            return loopedOnce

现在说我要在大型动画中调用smallAnim,如下所示:

    def bigAnim(tick,firstRun):
        smallAnim(0,50,5)

现在无限期地调用smallAnim,因为持续时间永远不会低于4(每次在循环中调用时重置为5)。什么是解决这个问题的最佳方法?

1 个答案:

答案 0 :(得分:1)

您需要在bigAnim中进行计数,并且只有在值大于零时才调用smallAnim()

或者您可以返回当前的持续时间:

def bigAnim(tick,firstRun):
    duration = smallAnim(x,y,duration)
    #more anims and logic...

def smallAnim(x, y, duration):
    duration -= 1
    if duration > 0:
        Anim.blit(screen,(x ,y))
        Anim.play()
    return duration

您的根本问题是Python does pass the references to the variables, but integers are immutable.

使用字符串更容易理解:

功能

def foo(s):
    s = " world"
如果您致电s

只会修改该功能的本地foo("hello")。您会看到的典型模式是:

def foo(s):
    return s + " world"

然后...... print foo("hello")