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()
move_y
as a global variable, otherwise theball_move()
function thinks it's a local variable. - John Gordonif
statement at the top that affectsmove_y
. Please post the full traceback, and the lines it describes, if you haven't done so already. - OakenDuckmove_x
andmove_y
are redefined in the first few lines of the function; they're available to any inner functions insidemain
. - OakenDuck