【问题标题】:Trouble with time dependent variable reference时间相关变量参考的问题
【发布时间】:2014-10-14 13:16:45
【问题描述】:

我要制作一个隐藏按钮,当您在它上方徘徊时会显示它,然后在短按它时亮起(在我的脚本 atm 中为 2 秒),然后运行一个函数。我实施了一种对我来说似乎合乎逻辑的机制来实现这一目标。我得到一个变量引用错误。

这是我的按钮类:

class Button:
    def __init__(self, imgInAc, imgAc, posX, posY, width, height, function, gameInstance, hiddenButton):
        self.imgInAc = pygame.image.load(imgInAc)
        self.imgAc = pygame.image.load(imgAc)
        self.posX = posX
        self.posY = posY
        self.w = width
        self.h = height
        self.function = function
        self.gameInstance = gameInstance
        self.hiddenButton = hiddenButton
        # Make clickDump false, to make sure, refTiem gets measured only once after the button press,
        # because otherweise it would get overwritten over and over again while the game loop is running
        self.clickDump = False 

    def button(self):
        # Measure the current time to later run the button function while displaying the active image before doing so.
        curTime = time.clock()
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if self.posX+self.w > mouse[0] > self.posX and self.posY+self.h > mouse[1] > self.posY:
            if self.hiddenButton == False:
                self.gameInstance.gameDisplay.blit(self.imgAc,(self.posX,self.posY))
                if click[0] == 1:
                    buttonFunctions.buttonFunctions(self.function)
            # If the button is a ahidden one, go through teh following protocol
            elif self.hiddenButton == True:
                # Display the off version if the mouse hovers over it
                self.gameInstance.gameDisplay.blit(self.imgInAc,(self.posX,self.posY))
                if click[0] == 1:
                    # if you click on it, display teh active version
                    self.gameInstance.gameDisplay.blit(self.imgAc,(self.posX,self.posY))
                    ###print curTime
                    # If clickDump is false, defien teh reference time relative to teh current time.
                    # Then make it true, so the ref time doesn't get changed again, while the game loop is running
                    if self.clickDump == False:
                        refTime = time.clock()+2
                        ###print refTime
                        self.clickDump = True
                # If ClickDump is true, check if teh time condition is satiesfied. Note, that this check is run outside the "if click[0] == 1"-scope,
                # to make sure, it also gets checked if the click happened in a previoues cycle.
                if self.clickDump == True:
                    # If the time interval between the click event and the current time has elapsed
                    # set clickDUmp back to flase, so it can be used for teh next button press protocol
                    # and finally run the button function.
                    if curTime > refTime:
                        self.clickDump = False
                        buttonFunctions.buttonFunctions(self.function)

        else:
            if self.hiddenButton == False:
                self.gameInstance.gameDisplay.blit(self.imgInAc,(self.posX,self.posY))

错误是:

"...第 296 行,在按钮中

如果 curTime > refTime:

UnboundLocalError: 在赋值之前引用了局部变量 'refTime'"

我真的不明白这一点,因为我认为我只执行如果 refTime 已初始化,则比较时间变量的部分,因为条件或运行比较 onyl 在 refTime 被分配其值的部分得到满足.

抱歉,我的评论中可能存在大量拼写错误……我只是为自己写的。

所以……我很困惑。

【问题讨论】:

  • 只有在self.clickDump == False设置 refTime(严格来说,应该是if not self.clickDump:),并且只有在@987654325 时才get @(同样,应该是 if self.clickDump:)。他们不可能都是真的。
  • 但这就是我想做的。 ...?不是吗。我的逻辑错误在哪里?
  • 通常,局部变量在函数结束时不再存在。如果您在第一次调用button 时创建了refTime,那么当您再次调用button 时它就不会存在了。
  • 您应该使用if self.clickDump: 而不是if self.clickDump == True:False 也是如此:if not self.clickDump:
  • 啊!一定是这样。谢谢!

标签: python class variables time variable-assignment


【解决方案1】:

这是由于refTime的范围。

reftime 在某个if 循环中分配,而在另一个if 循环中使用。

这样分配。

class Button(object):
    ...
    ...
    def button(self):
        refTime = 0

【讨论】:

  • 那么它将是一个类属性,被所有Buttons共享,这可能会导致以后出现微妙和混乱的错误。
  • @jonrsharpe 但在代码中其他任何地方都没有使用它。
  • 即使该属性仅在Button.button 中使用(我们无法保证),您可以有多个按钮共享相同状态的事实仍然存在一个问题。事实上,它可能比当前的问题更糟糕 - 默默地做错事比大声失败更难解决。
  • 我刚刚意识到一些事情。难道我不能在 buttonFunctions.buttonFunctions(self.function) 之前的睡眠命令之前手动更新屏幕吗?
猜你喜欢
  • 1970-01-01
  • 2012-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多