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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…