Riderfighter Riderfighter - 6 months ago 15
Python Question

TypeError: unorderable types: NoneType() < int()

I'm trying to make a pong style game in python but each time I get the error written in the title. I've already tried googling ways to fix the error but I don't have a place to start on. It might be possible that my distance function might not be working. But anything could be broken so any help would be loved! Also this is the full error that comes out:

Traceback (most recent call last):
File "/Users/NAME/Documents/PyPong.py", line 47, in <module>
checkCollisions()
File "/Users/NAME/Documents/PyPong.py", line 35, in checkCollisions
if distance(b,e) < 10:
TypeError: unorderable types: NoneType() < int()


And this is the code:

from tkinter import *
from math import *
##MAIN GAME PART##
window = Tk()
canvas = Canvas(window,width=400,height=400)
canvas.pack()
##FUNCTIONS##
def movePaddles(event):
key=event.keysym
if key == 'Up':
canvas.move(PaddleTwo,0,-10)
elif key == 'Down':
canvas.move(PaddleTwo,0,10)
if key == 'w':
canvas.move(PaddleOne,0,-10)
elif key == 's':
canvas.move(PaddleOne,0,10)
if key == 'j':
canvas.move(PingPongBall,-10,0)
elif key == 'k':
canvas.move(PingPongBall,10,0)

def distance(target1,target2):
target1coords = canvas.coords(target1)
target2coords = canvas.coords(target2)
x1 = (target1coords[0] + target1coords[2] ) / 2
y1 = (target1coords[1] + target1coords[3] ) / 2
x2 = (target2coords[0] + target2coords[2]) / 2
y2 = (target2coords[1] + target2coords[3]) / 2
d = sqrt( (x2-x1)** 2 + (y2-y1)** 2)

def checkCollisions():
for e in PaddleList:
if distance(PingPongBall,e) < 10:
print('hit') #print info on the shell

##PADDLES##
PaddleOne = canvas.create_rectangle(10 ,150 ,25 ,250 ,fill='blue')
PaddleTwo = canvas.create_rectangle(400,150 ,385 ,250 ,fill='blue')
PingPongBall = canvas.create_rectangle(200, 200, 210 ,210 ,fill='red')
PaddleList = [PaddleOne,PaddleTwo]
BallList = [PingPongBall]

canvas.bind_all('<Key>',movePaddles)
while True:
checkCollisions()

Answer

You never return anything from distance() so it returns None. You can't compare None to 10 so this results in an error.

Instead of:

d = sqrt( (x2-x1)** 2 + (y2-y1)** 2)

Simply write:

return sqrt( (x2-x1)** 2 + (y2-y1)** 2)