Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

Python pygame - bouncing ball (UnboundLocalError: local variable 'move_y' referenced before assignment)

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...