简体   繁体   English

如何解决此错误“UnboundLocalError:分配前引用的局部变量'a'”

[英]How can I fix this error “UnboundLocalError: local variable 'a' referenced before assignment”

Hi I'm looking for a solution to this problem, I've declared a global object but when I try to use it in a function the compiler give me this error message "UnboundLocalError: local variable 'a' referenced before assignment" I don't get why, I declared it as global so it doesn't make any sense that error.嗨,我正在寻找解决这个问题的方法,我已经声明了一个全局 object 但是当我尝试在 function 中使用它时,编译器给了我这个错误消息“UnboundLocalError: local variable 'a' referenced before assignment”我不'不明白为什么,我将其声明为全局,因此该错误没有任何意义。 I used a free module called g2d to animate some sprites this is the code:我使用了一个名为 g2d 的免费模块来为一些精灵设置动画,这是代码:

    import g2d
    from actor import Actor, Arena
    sprites = g2d.load_image("moon-patrol.png")
    bckd = g2d.load_image("moon-patrol-bg.png")
    arena = Arena((700, 500))

    class Rover(Actor):

        def __init__(self, arena, pos):
            self._x, self._y = pos
            self._w, self._h = 35, 25
            self._dx, self._dy = 0, 0
            self._arena_w, self._arena_h = arena.size()
            self._ground = 100
            self._jump = -10
            arena.add(self)

        def move(self):
            self._y += self._dy
            if self._y < 0:
                self._y = 0
            elif self._y + self._dy > self._arena_h - self._ground:
                self._y = self._arena_h - self._ground
            else:
                self._dy += 0.4

            self._x += self._dx
            if self._x < 0:
                self._x = 0
            elif self._x > self._arena_w - self._ground:
                self._x = self._arena_w - self._ground

        def go_up(self):
            if self._y >= self._arena_h - self._h - self._ground:
                self._dy = self._jump

        def go_right(self):
            self._dx = 10

        def go_left(self):
            self._dx = -10

        def stay(self):
            self._dy, self._dx = 0, 0

        def symbol(self):
            return 210, 155, self._w, self._h

        def position(self):
            return self._x, self._y, self._w, self._h

    a = Rover(arena, (100, 400))

    def tick():
        r, r1, r2 = 0, 0, 0
        ARENA_W, ARENA_H = arena.size()

        g2d.clear_canvas()
        rel_x = r % ARENA_W
        g2d.draw_image_clip(bckd,(0, 100, 512, 150),(rel_x - ARENA_W,0,700,500))

        if rel_x < ARENA_W:
            g2d.draw_image_clip(bckd,(0, 100, 512, 150),(rel_x,0,700,500))
        r -= 5
        rel_x1 = r % ARENA_W
        g2d.draw_image_clip(bckd,(0, 385, 512, 125),(rel_x1 - ARENA_W,300,700,200))

        if rel_x1 < ARENA_W:
            g2d.draw_image_clip(bckd,(0, 385, 512, 125),(rel_x1,300,700,200))
        r1 -= 3
        rel_x2 = r % ARENA_W
        g2d.draw_image_clip(bckd,(0, 513, 512, 125),(rel_x2 - ARENA_W,400,700,100))

        if rel_x2 < ARENA_W:
            g2d.draw_image_clip(bckd,(0, 513, 512, 125),(rel_x2,400,700,100))
        r1 -= 1

        if a.symbol != (0, 0, 0, 0):
            g2d.draw_image_clip(sprites, a.symbol(), a.position())

        if g2d.key_pressed("Spacebar"):
            a.go_up()

        if g2d.key_pressed("ArrowRight"):
            a.go_right()

        if g2d.key_pressed("ArrowLeft"):
            a.go_left()

        if (g2d.key_released("ArrowRight") or g2d.key_released("ArrowLeft")):
            a.stay()

        arena.move_all()

        for a in arena.actors():    
## Here is where the compiler give me the error
## "UnboundLocalError: local variable 'a' referenced before assignment
            if a.symbol != (0, 0, 0, 0):
                g2d.draw_image_clip(sprites, a.symbol(), a.position())


    def main():    
        g2d.init_canvas(arena.size())
        g2d.main_loop(tick)
    main()

Declare a as a global variable in the tick function.在记号tick中将a声明为全局变量。

def tick():
    global a
    ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何解决:UnboundLocalError:分配前引用的局部变量“生成” - how can i fix: UnboundLocalError: local variable 'generate' referenced before assignment 如何修复“UnboundLocalError:分配前引用的局部变量'user_score'”? - How can I fix "UnboundLocalError: local variable 'user_score' referenced before assignment"? 如何使 dataframe 全球化? 出现错误 - UnboundLocalError:分配前引用的局部变量 - How can I make a dataframe global? Getting error - UnboundLocalError: local variable referenced before assignment 如何修复“UnboundLocalError:赋值前引用的局部变量‘books’”? - How to fix "UnboundLocalError: local variable 'books' referenced before assignment"? 如何修复 UnboundLocalError:在 Python 中分配之前引用的局部变量“df” - How to fix UnboundLocalError: local variable 'df' referenced before assignment in Python UnboundLocalError:赋值前引用的局部变量'error' - UnboundLocalError: local variable 'error' referenced before assignment UnboundLocalError:赋值前引用了局部变量“i” - UnboundLocalError: local variable 'i' referenced before assignment 赋值之前引用了unboundlocalerror局部变量&#39;i&#39; - unboundlocalerror local variable 'i' referenced before assignment 我收到错误消息:UnboundLocalError:分配前引用了本地变量&#39;porc&#39; - I got error : UnboundLocalError: local variable 'porc' referenced before assignment UnboundLocalError 错误:赋值前引用了局部变量“i” - UnboundLocalError error: local variable 'i' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM