[Python] Turtle.distance used to detect collision between ball and paddle, but how does it...

Discussão em 'Python' iniciado por Stack, Fevereiro 26, 2025.

  1. Stack

    Stack Membro Participativo

    [​IMG]

    I am trying to replicate the pong game, and so far it works, but I haven't clarify this solution as I was expecting an error by using distance function from turtle.

    #Collision with paddle
    if abs(ball.xcor()) > 340:
    if ball.distance(r_paddle) < 70 or ball.distance(l_paddle) < 70:
    ball.bounce_x()


    Distance function measures the distance between the Middle of the object, but not the borders. Since my paddle has a weight of 70, the distance to collision is 70 as well. That will work for the y axis, but this condition technically includes the x axis.

    Nevertheless, the code works as expected, it bounces when it touches the paddle (A). But my question is why it does not bounce as soon as the ball is within the delimited distance (70)(B).

    Continue reading...

Compartilhe esta Página