I am following a paddles game tutorial and I am trying to get the ball to hit the paddles and change its trajectory.
I thought that it would be better if I used range()
instead of the code shown in the tutorial but mine ends up not executed (the ball just goes through the paddles).
The code in the tutorial works perfectly. The code used in the tutorial:
#Hit Paddles
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
My code:
#Hit Paddles
if ball.xcor() in range(340, 350) and\
ball.ycor() in range((paddle_b.ycor() - 40), (paddle_b.ycor() + 40)):
ball.setx(340)
ball.dx *= -1
The game was created using the turtle module, window wd = 800
, window height = 600
, paddles are 20 * 100
, and I placed them at x = -350/350
and ball diameter is 20
.