麻烦与时间相关的变量参考

时间:2014-10-14 13:16:45

标签: python class variables time variable-assignment

我要制作一个隐藏的按钮,当你踩过它时会显示,然后在按下它一小段时间(在我的脚本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'

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

对于我的纪念中可能出现的错别字而感到抱歉......我只是为自己写的。

所以......我很困惑。

1 个答案:

答案 0 :(得分:-1)

这是由于refTime

的范围

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

像这样分配。

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