Paython Game building tutorial error

Hey guys. it’s my first time typing code.I have been following the Python game building tutorials and I had a proplem in the pong game It Shuts self off and does not rebound when hit the paddle can someone tell me the reason why?

import turtle

wn =turtle.Screen()
wn.title("ping pong")
wn.bgcolor("RED")
wn.setup(width=800, height=600)
wn.tracer(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.shape("circle")
ball.color("white")
ball.speed(0)
ball.penup()
ball.goto(0, 0)
ball.shapesize(stretch_wid=1.5)
ball.dx =  0.2
ball.dy =  0.2


#movement
def paddle_A_up():
    y = paddle_A.ycor()
    y += 20
    paddle_A.sety(y)


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

  #paddle_B_
def paddle_B_up():
    y = paddle_B.ycor()
    y += 20
    paddle_B.sety(y)



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



#keyboard binding
wn.listen()
wn.onkeypress(paddle_A_up, "w")
wn.onkeypress(paddle_A_down, "s")

wn.onkeypress(paddle_B_up, "Up")
wn.onkeypress(paddle_B_down, "Down")

while True:
    wn.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    

    if ball.xcor() < -390:
        ball.goto(0 , 0)
        ball.dx *= -1                       


# paddle and pall 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



Welcome, Omar.

Please edit your post to give it a descriptive title, and add the question along with any necessary information to the body of your post.

Cheers

1 Like

Hello again,

To answer your question:
The error in the console (you should be able to see this):

TypeError: unsupported operand type(s) for -: ‘method’ and ‘int’

It is referring to this line, because it is trying to perform a mathematical operation (-) on a method…

ball.ycor() > (paddle_B.ycor -40)

Just a typo.

Hope this helps

1 Like