#Paddle and Ball collisions
if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() -40):
ball.setx(340)
ball.dx *= -1
if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() -40):
ball.setx(-340)
can anyone explain me about the first line of this code???Why -40 and +40 is used
can anyone explain me about the first line of this code???Why -40 and +40 is used
Both ball.ycor()
and paddle_b.ycor()
are points. However, the paddle itself is some sort of rectangular entity 80 pixels tall. So we need to determine if the point that is the ball's vertical center lies over any portion of the paddle:
and (ball.ycor() < paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() -40)
A more idiomatic way to write this might be:
and paddle_b.ycor() -40 < ball.ycor() < paddle_b.ycor() + 40
Similarly for the ball's horizontal position:
(ball.xcor() > 340 and ball.xcor() < 350)
or:
340 < ball.xcor() < 350
We don't need to consider the paddle's current horizontal position as it doesn't move horizontally -- it's in a fixed position with respect to the X axis.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.