Search code examples
pythonruntime-error

Ping Pong game problem with x and y cords


ok so I created a game with python its like 2d ping pong, the problem comes when I made that if the ball_y == racket_y it doesnt apply to the while racket it only apply on the Lowest cord of the racket and not on the whole racket


Solution

  • This is because the y value of a rectangle in pygame is determined by a point. Usually it's the top left, but I guess in your case the racket_y is it's bottom left. A better way to do the collisions is to use the racket itself and the colliderect function, which checks for collisions between the entirety of the ball and the entirety of the racket. Note, to use it you have to call the function on the object's rect you want to start at, and in parenthesis you state the rectangle of the other object. Then you invert the direction of the ball.