1

I have a rect object called ball_rect in my code this object has his ordinate decrease every second by 2, eventually it falls and reach the bottom of my screen and disappear. I want this object to not fall beyond 200 pixels in y axis.

I also knew a the command to do that but I forgot.

here's the code:

ball_y = 20
ball_x = 100

ball = pygame.image.load("data/ball.png")
ball_rect = ball.get_rect(topleft = (ball_x,ball_y)

def ball_area(): #here I want to put the code to restrict it in the margin of the screen
                      

1 Answers1

0

You have to test if the bottom of the ball rectangle is below 200. If it is below, the change it to 200. Update ball_y with the new top of the ball rectangle:

def ball_area():
    global ball_x, ball_y

    ball_rect = ball.get_rect(topleft = (ball_x, ball_y)
    if ball_rect.bottom > 200:
        ball_rect.bottom = 200
        ball_y = ball_rect.top
Rabbid76
  • 202,892
  • 27
  • 131
  • 174