Python pygame - 弹跳球(UnboundLocalError:赋值前引用了局部变量“move_y") [英] Python pygame - bouncing ball (UnboundLocalError: local variable 'move_y' referenced before assignment)

查看:62
本文介绍了Python pygame - 弹跳球(UnboundLocalError:赋值前引用了局部变量“move_y")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个函数,负责从屏幕边缘弹起球.我知道我可以用数学和 Vector2 函数做得更好,但我想知道为什么会出现这个错误,以及为什么我可以在没有这行代码的情况下运行窗口:

如果 ball.y >= HEIGHT - 10:move_y = -vall_vel

代码

class Ball:def __init__(self, x, y, 颜色):自我.x = x自我.y = yself.color = 颜色def draw(self, window):pygame.draw.circle(窗口,self.color,(self.x,self.y),10)球=球(宽度//2,高度//2,红色)定义主():运行 = 真ball_vel = 10move_x = ball_velmove_y = ball_vel定义更新():WINDOW.fill(黑色)ball.draw(窗口)player.draw(窗口)pygame.display.update()def ball_move():如果高度 - 10 >ball.y >0 和 WIDTH - 10 >球.x >0:ball.x += move_xball.y += move_y如果 ball.y >= HEIGHT - 10:move_y = -ball_vel运行时:时钟滴答(FPS)球移动()更新()

解决方案

问题是由函数引起的

导致问题的代码在一个函数中:

<块引用>

def ball_move():如果高度 - 10 >ball.y >0 和 WIDTH - 10 >球.x >0:ball.x += move_xball.y += move_y如果 ball.y >= HEIGHT - 10:move_y = -ball_vel

在函数ball_move 中写入变量move_y.这意味着该变量在函数体内声明并且是一个局部变量(ball_move 中的局部变量).在声明之前读取变量会导致错误

<块引用>

UnboundLocalError:赋值前引用了局部变量move_y"

您必须使用 global 语句 如果要将变量解释为全局变量.实际上在函数main 中存在一个同名的变量.但是由于要在 main 中设置相同的变量,因此它也必须在那里声明为全局:

def main():全局运行,move_x,move_y # <---- ADD运行 = 真ball_vel = 10move_x = ball_velmove_y = ball_vel定义更新():WINDOW.fill(黑色)ball.draw(窗口)player.draw(窗口)pygame.display.update()def ball_move():全局 move_x, move_y # <---- ADD如果高度 - 10 >ball.y >0 和 WIDTH - 10 >球.x >0:ball.x += move_xball.y += move_y如果 ball.y >= HEIGHT - 10:move_y = -ball_vel运行时:时钟滴答(FPS)球移动()更新()

I wanted to create a function that is responsible for bouncing ball from the edge of the screen. I know i can do it better with math and Vector2 function but i wonder why i have this error, and why i can run window without this line of code:

if ball.y >= HEIGHT - 10:
    move_y = -vall_vel

Code

class Ball:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color

    def draw(self, window):
        pygame.draw.circle(window, self.color, (self.x, self.y), 10)

ball = Ball(WIDTH // 2, HEIGHT // 2, red)

def main():
    run = True

    ball_vel = 10
    move_x = ball_vel
    move_y = ball_vel

    def update():
        WINDOW.fill(black)

        ball.draw(WINDOW)

        player.draw(WINDOW)
        pygame.display.update()

    def ball_move():
        
        if HEIGHT - 10 > ball.y > 0 and WIDTH - 10 > ball.x > 0:
            ball.x += move_x
            ball.y += move_y
        
        if ball.y >= HEIGHT - 10:
            move_y = -ball_vel

    while run:
        clock.tick(FPS)

        ball_move()

        update()

解决方案

The issue is caused by the function

The code which causes the issue is in a function:

def ball_move():

   if HEIGHT - 10 > ball.y > 0 and WIDTH - 10 > ball.x > 0:
       ball.x += move_x
       ball.y += move_y

   if ball.y >= HEIGHT - 10:
       move_y = -ball_vel

In the function ball_move is written to the variable move_y. This means the variable is declared inside the function's body and is a local variable (local in ball_move). Reading a variable before it is declared causes the error

UnboundLocalError: local variable 'move_y' referenced before assignment

You have to use the global statement if you want to be interpret the variable as global variable. Actually there exist a variable with the same name in the function main. But since the same variable is to be set in main, it must also be declared there as global:

def main():
    global run, move_x, move_y        # <---- ADD

    run = True
    ball_vel = 10
    move_x = ball_vel
    move_y = ball_vel

    def update():
        WINDOW.fill(black)
        ball.draw(WINDOW)
        player.draw(WINDOW)
        pygame.display.update()

    def ball_move():
        global move_x, move_y         # <---- ADD

        if HEIGHT - 10 > ball.y > 0 and WIDTH - 10 > ball.x > 0:
            ball.x += move_x
            ball.y += move_y
        if ball.y >= HEIGHT - 10:
            move_y = -ball_vel

    while run:
        clock.tick(FPS)
        ball_move()
        update()

这篇关于Python pygame - 弹跳球(UnboundLocalError:赋值前引用了局部变量“move_y")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆