1

So in this function I have collision detection between a ball and a paddle and score (and other stuff that aren't relevant for this question) . When the ball hits the paddle it bounces off in the oposite direction. It works fine for the left side of the paddle, but if the ball hits from the top or the bottom it kinda slides and the score skyrockets.

def Objects (paddle,ball,hits,font,black):  
    if ball.BallRect.colliderect(paddle.PaddleRect):
        ball.vx *= -1
        score_text = font.render(f"Score: " + str(hits + 1),True, black)
        temp += 1
        
    else:
        score_text = font.render(f"Score: " + str(hits),True, black)
        window.blit(score_text,(20,20))
    
    return temp
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Tinko
  • 59
  • 4

1 Answers1

1

See Sometimes the ball doesn't bounce off the paddle in pong game.

When the ball hits the left paddle, the ball bounces to the right and the following x-direction must be positive. When the ball hits the right paddle, the ball bounces to the left and the following x-direction must be negative.
Use abs(x) to compute the absolute value of a number. The new direction of the ball is either abs(ball.vx) or -abs(ball.vx).

You have to distinguish between leftPaddle and rightPaddle

if ball.BallRect.colliderect( leftPaddle.PaddleRect ):
    ball.vx = abs(ball.vx)

    # [...]

elif ball.BallRect.colliderect( rightPaddle.PaddleRect ):
    ball.vx = -abs(ball.vx)

    # [...]

Or you need to know which paddle is hit. Pass either "left" or "right" to the argument side:

def Objects(paddle, side, ball, hits, font, black):  
    if ball.BallRect.colliderect(paddle.PaddleRect):
      
        if side == "left":
            ball.vx = abs(ball.vx)
        else:
            ball.vx = -abs(ball.vx)
    
      
        score_text = font.render(f"Score: " + str(hits + 1),True, black)
        temp += 1
        
    else:
        score_text = font.render(f"Score: " + str(hits),True, black)
        window.blit(score_text,(20,20))
    
    return temp

If you only have 1 paddle, it is sufficient to set the correct direction. If you have just the right paddle:

def Objects(paddle, ball, hits, font, black):  
    if ball.BallRect.colliderect(paddle.PaddleRect):

        ball.vx = -abs(ball.vx)

        score_text = font.render(f"Score: " + str(hits + 1),True, black)
        temp += 1

    else:
        score_text = font.render(f"Score: " + str(hits),True, black)
        window.blit(score_text,(20,20))
    
    return temp
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Î forgoted to add that there is one paddle. Here's a screenshot https://prnt.sc/we6mcy When the ball hits the top or the bottom of the paddle it slides – Tinko Jan 01 '21 at 16:47
  • @Tinko So what is the problem? If you have just the right paddle, `ball.vx = -abs(ball.vx)` is the solution. I've extended the answer. – Rabbid76 Jan 01 '21 at 16:56
  • thanks man. I saw the other post you answered and it works now. By the way, why do I see you on every post on stack overflow? – Tinko Jan 02 '21 at 16:59
  • @Tinko Thank you. You just see me on the posts with the tag [tag:pygame] (or [tag:opengl], [tag:glsl]) – Rabbid76 Jan 02 '21 at 17:08
  • I posted another question on the same topic. It works fine, but the score skyrockets. It's not visible, but multiple collisions happen at the same time. – Tinko Jan 02 '21 at 17:23