Sep-08-2021, 01:15 AM
Hi, I am following a tutorial on Youtube on how to make this pong game. I follow the tutorial code-for-code, and I am getting a different result than the instructor on Youtube, but my code and parameters are exactly the same as his. However, in the Youtube video his "ball" bounces from the center to the bottom right and bounces back. But my code only shows the ball starting from lower edge of the turtle screen and shoots off and never seeing it again. It doesn't even bounce back. I set ball.dx *=-1 to tell the ball to bounce back. Not sure what is wrong with my code or parameters, but it's exactly the same as his. any explanation or suggestion will be helpful and much appreciated!.
import turtle wn = turtle.Screen() wn.title("Pong by @TokyoEdTech") 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("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.1 ball.dy = -0.1 # Function 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") # this calls the function paddle_a_up() wn.onkeypress(paddle_a_down, "s") wn.onkeypress(paddle_b_up, "Up") # this calls the function paddle_a_up() wn.onkeypress(paddle_b_down, "Down") # Main game loop 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: #the ball gone past the paddle and is off the screen ball.goto(0, 0) ball.dx *= -1 if ball.xcor() > -390: # the ball gone past the paddle and is off the screen ball.goto(0, 0) ball.dx *= -1