在我的碰撞检查不起作用的情况下,在Python中制作蛇游戏有问题。我写了一个功能来检查蛇和食物本身之间的碰撞。当碰撞时它没有做任何事情,如果它与使用该功能发生碰撞,我已将其写入未描绘,我也放入打印功能,看看该功能是否正常,如果我使用它并且看不到打印。
def collide(block1,block2):
if math.sqrt(((block2.getCenterX() - block1.getCenterX()) **2)+ ((block2.getCenterY() - block1.getCenterY())**2)) < BLOCK_SIZE:
print("true")
return True
else:
return False
------------------------------------------------------- not part of functiom
if collide(theSnake[0],food) == True:
food.undraw()
foodX = random.randint(BLOCK_SIZE, WIN_WIDTH-BLOCK_SIZE)
foodY = random.randint(BLOCK_SIZE, WIN_HEIGHT-BLOCK_SIZE)
food.draw()
theSnake.append(block)
else:
foodX = foodX
foodY = foodY
答案 0 :(得分:0)
我建议您修改碰撞功能以提供更多信息。例如
def collide(block1,block2):
dx = block2.getCenterX() - block1.getCenterX()
dy = block2.getCenterY() - block1.getCenterY()
dist = math.sqrt(dx ** 2 + dy ** 2) # equivalent to math.hypot(dx, dy)
if dist < BLOCK_SIZE:
print("true")
return True
else:
print("false", dx, dy, dist)
return False