Pong Game Why it doesn´t function the collision with the ball

Hi i´m very new with programming, and i´m starting doing projects in python:

# pong game
#Part 1:window


import turtle
window= turtle.Screen()
window.title("Ping PoNG")
window.bgcolor("black")
window.setup(width=800, height=600)
window.tracer(0)

# Score 
score_A = 0
score_B = 0


#Paddle A
paddle_A= turtle.Turtle()
paddle_A.speed(0)
paddle_A.shape("square")
paddle_A.color("white")
paddle_A.shapesize(stretch_wid=5, stretch_len=1)
paddle_A.penup()
paddle_A.goto(-350, 0)


#Paddle B
paddle_B= turtle.Turtle()
paddle_B.speed(0)
paddle_B.shape("square")
paddle_B.color("white")
paddle_B.shapesize(stretch_wid=5, stretch_len=1)
paddle_B.penup()
paddle_B.goto(350, 0)

#Ball
Ball= turtle.Turtle()
Ball.speed(0)
Ball.shape("square")
Ball.color("white")
Ball.penup()
Ball.goto(0, 0)
Ball.dx= 0.20
Ball.dy= -0.20

#pen
pen= turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto (0, 260)
pen.write ("Player A:0 Player B: 0", align="center", font=("Courier", 24, "normal"))

#Functions
def paddle_A_up():
    y=paddle_A.ycor()
    y += 25
    paddle_A.sety(y)

def paddle_A_down():
    y=paddle_A.ycor()
    y -=25
    paddle_A.sety(y)

def paddle_B_up():
    y=paddle_B.ycor()
    y += 25
    paddle_B.sety(y)

def paddle_B_down():
    y=paddle_B.ycor()
    y -= 25
    paddle_B.sety(y)

#Keyboard binding
window.listen()
window.onkeypress(paddle_A_up, "w")
window.onkeypress(paddle_A_down, "s")
window.onkeypress(paddle_B_up, "Up")
window.onkeypress(paddle_B_down, "Down")

#Main game loop
while True: 
    window.update()


    #Move the Ball
    Ball.setx(Ball.xcor() + Ball.dx)
    Ball.sety(Ball.ycor() + Ball.dy)

    #Border checking
    if Ball.ycor() > 290:
        Ball.sety(290)
        Ball.dy *= -1
    
    if Ball.ycor() < -290:
        Ball.sety(-290)
        Ball.dy *= -1
    
    if Ball.xcor() > 390:
        Ball.goto(0, 0)
        Ball.dx *= -1
        score_A += 1
        pen.clear()
        pen.write ("Player A:{} Player B: {}".format(score_A, score_B), align="center", font=("Courier", 24, "normal"))

    if Ball.xcor() < -390:
        Ball.goto(0, 0)
        Ball.dx *= -1
        score_B += 1
        pen.clear()
        pen.write ("Player A:{} Player B: {}".format(score_A, score_B), align="center", font=("Courier", 24, "normal"))
    #Paddle and ball collisions

        if Ball.xcor() > 330 and (Ball.ycor() < paddle_B.ycor() + 50 and Ball.ycor() > paddle_B.ycor() - 50):
            Ball.dx *= -1

Define what you mean by “doesn’t function”. Also, what part of the code do you think should be working but does not work as expected?

Hi Augusto, you did a good job.
Let’s see why the ball does not bounce off the paddle:
If you notice, the section of the code that handles the collision with paddle is within another condition that has nothing to do with it!

    if Ball.xcor() < -390:
        Ball.goto(0, 0)
        Ball.dx *= -1
        score_B += 1
        pen.clear()
        pen.write("Player A:{} Player B: {}".format(score_A, score_B), align="center", font=("Courier", 24, "normal"))

        # Paddle and ball collisions
        if Ball.xcor() > 330 and (Ball.ycor() < paddle_B.ycor() + 50 and Ball.ycor() > paddle_B.ycor() - 50):
            Ball.dx *= -1

You’ll have to untab this section so it is not dependable on the other, just like this:

    if Ball.xcor() < -390:
        Ball.goto(0, 0)
        Ball.dx *= -1
        score_B += 1
        pen.clear()
        pen.write("Player A:{} Player B: {}".format(score_A, score_B), align="center", font=("Courier", 24, "normal"))

    # Paddle and ball collisions
    if Ball.xcor() > 330 and (Ball.ycor() < paddle_B.ycor() + 50 and Ball.ycor() >  paddle_B.ycor() - 50):
        Ball.dx *= -1

Also, don’t forget that this only handles the collision with the right paddle. You will have to write similar code for the left one.

Good luck.