0
\$\begingroup\$

Im trying to create pong in python, so far Ive set up correct collisions on the right paddle but upon just trying to reverse the code to work with the other side it refuses to work, simply glitching and holding the ball in its axis somehow, anyone know how to help?`

from processing import *

ball = {
    'x': 50,
    'y': 50,
    'velocity_x': 3,
    'velocity_y': 3,  
    'radius': 10
}
width = 550
height = 400

paddle1 = {
    'x': width - 20,
    'y': 160,
    'width': 15,
    'height': 100,
    'velocity': 5,
    'move': 0 
}
paddle2 = {
    'x': width - 545 ,
    'y': 160,
    'width': 15,
    'height': 100,
    'velocity': 5,
    'move': 0 
}
score = 0
def setup():
    # set the size of the canvas
    size(width, height)

def draw():
    # clear the previous frame by setting the background color of the canvas
    background(0, 0, 0)

def drawPaddle1():
    fill(0, 0, 255)
    rect(paddle1['x'], paddle1['y'], paddle1['width'], paddle1['height'])

def drawPaddle2():
    fill(0, 255, 255)
    rect(paddle2['x'], paddle2['y'], paddle2['width'], paddle2['height'])

def checkCollisions1():
    global score
    if ball['y'] >= paddle1['y'] and ball['y'] <= paddle1['y'] + paddle1['height']:
        if ball['x'] + ball['radius'] >= paddle1['x']:
            ball['velocity_x'] = -ball['velocity_x']
            ball['x'] = paddle1['x'] - ball['radius']

def checkCollisions2():
    global score
    if ball['x'] >= paddle2['x'] and ball['x'] <= paddle2['x'] + paddle2['width']:
        if ball['x'] + ball['radius'] >= paddle2['x']:
            ball['velocity_x'] = +ball['velocity_x']
            ball['x'] 
            ball['x'] = paddle2['x'] + ball['radius']







def drawBall():
    fill(255, 255, 255)
    ellipse(ball['x'], ball['y'], 2 * ball['radius'], 2 * ball['radius'])

def moveBall():
    ball['x'] = ball['x'] + ball['velocity_x']
    ball['y'] = ball['y'] + ball['velocity_y']
    if ball['y'] + ball['radius'] >= height or ball['y'] - ball['radius'] <= 0:
        ball['velocity_y'] = -ball['velocity_y']
    if ball['x'] + ball['radius'] >= width or ball['x'] - ball['radius'] <= 0:
        ball['velocity_x'] = -ball['velocity_x']

def draw():
    # clear the previous frame
    background(0, 0, 0)
    fill(255, 255, 255)

    drawBall()
    moveBall()
    drawPaddle1()
    drawPaddle2()
    checkCollisions1()
    checkCollisions2()




run()`
\$\endgroup\$
0
\$\begingroup\$

I've noticed a few wrong things on the collision lines:

def checkCollisions1():
    global score
    if ball['y'] >= paddle1['y'] and ball['y'] <= paddle1['y'] + paddle1['height']:
        if ball['x'] + ball['radius'] >= paddle1['x']:
            ball['velocity_x'] = -ball['velocity_x']
            ball['x'] = paddle1['x'] - ball['radius']

def checkCollisions2():
    global score
    if ball['x'] >= paddle2['x'] and ball['x'] <= paddle2['x'] + paddle2['width']:
        if ball['x'] + ball['radius'] >= paddle2['x']:
            ball['velocity_x'] = +ball['velocity_x']
            ball['x'] 
            ball['x'] = paddle2['x'] + ball['radius']

First, on checkCollisions1, you check if ball['x'] + ball['radius'] >= paddle1['x']: and if that's true, you set the ball's position like this: ball['x'] = paddle1['x'] - ball['radius']. This is not wrong, if the ball moves on the X axis every frame without having its speed ever hit 0. But if for some reason that function runs twice without the ball moving on the X axis, it will trigger collision twice.

You mentioned the first collision works properly, so that's probably not a big issue, but something worth being aware of. One suggested solution is changing the if condition to use > instead of >=, so that when collision happens, the ball will be placed in a position where collision can no longer happen, unless one of the two objects moves closer to each other.

On the second collision there are a number of things wrong. Firstly, on the first if condition, you check the ball's and paddle's X axis, contrary to the first collision where you check the Y axis first.

First collision:

if ball['y'] >= paddle1['y'] and ball['y'] <= paddle1['y'] + paddle1['height']:

Second collision:

if ball['x'] >= paddle2['x'] and ball['x'] <= paddle2['x'] + paddle2['width']:

This is not enough, as it's the same as the first collision, so you need to change a little bit of the code like this:

if ball['x'] <= paddle2['x'] and ball['x'] >= paddle2['x'] - paddle2['width']:

This seems to be an error as on the next line you check the ball's X and paddle's X again, so they should be changed to the Y axis, and width should change to height.

On the next line, I can't tell if it was a copy-paste error, or it's a syntax error (I have limited knowledge on python), but this line seems wrong:

ball['x']

That's all I found from a quick look, let me know if the problem still occurs.

\$\endgroup\$
  • \$\begingroup\$ well when I input it like that the ball teleports back to the racket when it reaches the middle of the screen and will then bounce off again. \$\endgroup\$ – Jonathan Sheerit Jan 11 at 16:20
  • \$\begingroup\$ @JonathanSheerit could you elaborate? My answer is a little lengthy, are you referring to a specific part or did you try everything I suggested? \$\endgroup\$ – TomTsagk Jan 11 at 16:24
  • \$\begingroup\$ when you said to change the y's to x's and the width to height \$\endgroup\$ – Jonathan Sheerit Jan 11 at 16:28
  • \$\begingroup\$ @JonathanSheerit My apologies, you are right, I forgot to change the code for the second paddle, I've edited my answer to reflect that. \$\endgroup\$ – TomTsagk Jan 11 at 16:34
  • \$\begingroup\$ thank you thats super helpful (by that I mean your edited answer) unfortunately now instead it treats the barrier like its not there at all and just passes through it \$\endgroup\$ – Jonathan Sheerit Jan 11 at 17:49

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.