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!