我正在用Python在Pygame中创建Pong游戏(很明显),并且是Pygame的新手,因此希望对物理操作有所帮助,例如当球触碰桨叶时,它将颠倒速度并朝相反的方向发展.到目前为止,一切正常,但是当球到达球拍时,球会直接穿过球拍,并且不会改变方向.我已经解决了问题,因此桨板不会离开屏幕,并且当球遇到墙时球会改变方向,但是当球遇到桨板时球不会改变方向.任何帮助或提示,将不胜感激.
我的桨课程:
class Paddle:
def __init__(self, x, y):
self.x = x
self.y = y
self.height = 40
self.width = 10
def draw(self, canvas):
pygame.draw.rect(canvas, pygame.Color(0,0,255),(self.x,self.y,self.width,self.height))
def contains(self, ptX, ptY):
return self.x < ptX < self.x + self.width & self.y < ptY < self.y + self.height
def overlaps(self, otherRectangle):
return otherRectangle.colliderect(Rect(self.x,self.y,self.height, self.width))
我的球课
class Ball:
def __init__(self, x, y):
#position of ball
self.x = x
self.y = y
#speed of ball
self.dx = 5
self.dy = 5
self.height = 10
self.width = 10
def draw(self, canvas):
pygame.draw.rect(canvas, pygame.Color(0,255,0), (self.x,self.y,self.width,self.height))
def reset(self):
self.x = 320
self.y = 240
self.dx = -self.dx
self.dy = 5
我的目标是使球碰到球拍或反弹时的速度反转(负速)(重叠点).