# Collisions in other sides on pong in python

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,
}
width = 550
height = 400

'x': width - 20,
'y': 160,
'width': 15,
'height': 100,
'velocity': 5,
'move': 0
}
'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)

fill(0, 0, 255)

fill(0, 255, 255)

def checkCollisions1():
global score
ball['velocity_x'] = -ball['velocity_x']

def checkCollisions2():
global score
ball['velocity_x'] = +ball['velocity_x']
ball['x']

def drawBall():
fill(255, 255, 255)

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()
checkCollisions1()
checkCollisions2()

run()


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

def checkCollisions1():
global score
ball['velocity_x'] = -ball['velocity_x']

def checkCollisions2():
global score
ball['velocity_x'] = +ball['velocity_x']
ball['x']


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.

• 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. – Jonathan Sheerit Jan 11 at 16:20
• @JonathanSheerit could you elaborate? My answer is a little lengthy, are you referring to a specific part or did you try everything I suggested? – TomTsagk Jan 11 at 16:24
• when you said to change the y's to x's and the width to height – Jonathan Sheerit Jan 11 at 16:28
• @JonathanSheerit My apologies, you are right, I forgot to change the code for the second paddle, I've edited my answer to reflect that. – TomTsagk Jan 11 at 16:34
• 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 – Jonathan Sheerit Jan 11 at 17:49