myRect.left矩形左侧 X 坐标的 int 值。
myRect.right 矩形右侧 X 坐标的 int 值。
myRect.top 矩形顶边 Y 坐标的 int 值。
myRect.bottom 底边 Y 坐标的 int 值。
由于所有这些属性都返回整数,这可能就是您的代码无法正常工作的原因。
此外,如果您的目标ballSnapLeft()是将球移动到远离玩家的位置,则ballRect.right = playerRect.left - distance只会更改矩形的 X 坐标。要使球也在 Y 坐标中移动,您可以执行以下操作
def ballSnapTop():
ballRect.top = playerRect.bottom - distance
Run Code Online (Sandbox Code Playgroud)