Python pong game help!

Hey guys. I’ve been following the Python game building tutorials and it’s my first time typing python code (I have only read books till now). Anyways I am carefully following everything in the pong game but I reach a point where I can’t get the program to work like the tutorial, even if the code looks to be the same.
The problem in particular is that the guy manages to have the ball bounce on the border by writing this:


# Border checking

    # Top and bottom
    if ball.ycor() > 290:
        ball.sety(290)
        ball.dy *= -1

But when I type the code, my ball just stays stuck at the top and keeps moving right. This is the fragment I have typed:

# Border Checking

    # Top and bottom
    if ball.ycor() > 290:
        ball.sety(290)
        ball.dy *= -1

But it’s almost as if the second part of my If function is not getting recognized. For reference, this is my entire code:

import turtle

wn = turtle.Screen()
wn.title("Pong by francho")
wn.bgcolor("black")
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("pink")
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("pink")
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("red")

ball.penup()
ball.goto(0, 0)

ball.dx = 0.2
ball.dy = 0.2

# Functions 

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)

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.xcor() + ball.dy)

    #Border checking
    if ball.ycor() > 290:
       ball.sety(290)
       ball.dy *= -1

And this is the guy’s code: http://christianthompson.com/sites/default/files/Pong/pong.py
The only different thing I have at this point is that my ball.dx and ball.dy are 0.2 instead of 2 because in my computer that’s the number that makes the ball move at an adequate speed (the guy said this could somehow change from system to system). I keep thinking it might be an error with the multiplication as it uses a float type number but it still doesn’t make sense to me. Do you know what it could be about? Thanks so much!

Is your indentation correct? It does not look right.

    #Border checking
    if ball.ycor() > 290:
        ball.sety(290)
        ball.dy *= -1

I’m not familiar with this Turtle module, but does this help any?

I have copied and pasted the guy’s code into mine to make sure that I didn’t hit the spacebar wrong or something. His snippet inside my code still doesn’t work, so I think my error must be elsewhere.

Edit: Solved it! There was an x elsewhere in the code where there should’ve been a y. Because they are the same number it didn’t cause a problem elsewhere until that point. (actually, because they are the same number I have no idea why my solution has worked… but well… computers gonna compute)

Your copy and paste into the forum has the spacing error I pointed out, so your copy and pasting has to be deleting spaces apparently.

You should also check the documentation to see if floats are supported in this use of Turtle. His example does not use floats.