gpt4 book ai didi

python - 类型错误 : unorderable types: NoneType() < int()

转载 作者:行者123 更新时间:2023-12-01 03:56:50 26 4
gpt4 key购买 nike

我正在尝试用 python 制作一个乒乓球风格的游戏,但每次我都会收到标题中写的错误。我已经尝试过谷歌搜索方法来修复错误,但我没有地方开始。我的距离函数可能不起作用。但任何东西都可能被破坏,所以任何帮助都会受到欢迎!这也是出现的完整错误:

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

这是代码:

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

最佳答案

您永远不会从 distance() 返回任何内容,因此它返回 None。您无法将 None 与 10 进行比较,因此这会导致错误。

而不是:

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

简单地写:

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

关于python - 类型错误 : unorderable types: NoneType() < int(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37311403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com