我试图在python中制作一个pong风格的游戏,但每次我都会在标题中写出错误。我已经尝试使用谷歌搜索方法来解决错误,但我没有办法开始。我的距离函数可能无法正常工作。但任何事情都可能被打破,所以任何帮助都会被爱!这也是出现的完整错误:
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()
答案 0 :(得分:2)
您永远不会从def test(x):
for i in range(0,6):
if i < x:
print i
返回任何内容,因此会返回distance()
。您无法将None
与10进行比较,因此会导致错误。
而不是:
None
简单地写一下:
d = sqrt( (x2-x1)** 2 + (y2-y1)** 2)