r/pythontips • • Feb 24 '23

Module What's wrong with my Code

I'm trying to code a Ping Pong game with two Paddles on the sides and a ball. I want the ball to bounce of the paddels, wich works on one Paddel but not the other. There is no error, no "Problem" in my Terminal and I copied the code from Panel B and changed it for Panel A but the ball just flies through and I don't see the mistake. Here is my (relevant) Code:

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)

if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() - 50): ball.setx(340) ball.dx *= -1

[ if (ball.xcor() > -340 and ball.xcor() < -350) and (ball.ycor() < paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() - 50): ball.setx(-340) ball.dx *= -1 ]

The problem is probably somewhere between the [ ]'s

0 Upvotes

3 comments sorted by

13

u/dangerlopez Feb 24 '23

You'll need to repost this with the code formatted better. To do so, start a new paragraph and add four spaces at the front of each line to enter the coding mode:

def function():
    pass

Then you'll want to add more spaces (you should do four) to indicate indenting.

1

u/PhilAndMaude Feb 25 '23

It's really hard to debug code by just looking at it, which is what you are trying to do. You need to either:

  • run in a debugger like VS Code, Wing or PyCharm

  • cram in a million print statements

Because your conditions are anded together, you could nest them thus and include print statements which will tell you the path that execution takes through your code:

if ball.xcor() > 340:
    print(1)
    if ball.xcor() < 350):
        print(2)
        if ball.ycor() < paddle_b.ycor() + 40:
            print(3)
            if ball.ycor() > paddle_b.ycor() - 50:
                print(4)
                ball.setx(340)
                ball.dx *= -1

1

u/WRX186 Feb 28 '23

Hello all, I’m doing the same game in python ( using PyCharm in Ubuntu). And my issue is with the speed of the ball. I tried everything but couldn’t get the ball to slow down. Any tips or advice? Thank you 😊 Ra