Skip to content Skip to sidebar Skip to footer

Pygame Bouncy Ball Sinks Through Floor

The code below bounces a ball but for some reason the ball goes through the ground after it finishes its bounces. Anyone Know Why? The idea of the code is a ball starts at the top

Solution 1:

Since the falling speed of the ball is greater than 1 pixel, you have to make sure that the ball does not fall below the lower edge of the window. You need to constrain the bottom of the ball to the bottom of the window:

done = Falsewhilenot done:
    # [...]

    x = x + dx
    y = y + dy
    ballrect.topleft = (x,y)

    #PART Aif ballrect.left < 0or ballrect.right > width:
        dx = -dx
    if ballrect.top < 0:
        dy = -dy
    if ballrect.bottom > height:
        ballrect.bottom = height                       # <---
        y = ballrect.top                               # <---
        dy = -dy

    # [...]

Post a Comment for "Pygame Bouncy Ball Sinks Through Floor"