pygame中的碰撞问题,为什么它对静态对象有效但对移动对象无效

时间:2018-11-20 23:16:24

标签: python pygame collision

我一直在阅读,并注意到一些有关使用Rect进行此游戏中的碰撞的建议。但是我仍然为这不能按书面要求而感到困扰。

基本上,在此乒乓球克隆中,我的球会从静态对象反弹,但是当我尝试使其从我的播放器对象反弹时,它会一直通过。有人可以解释为什么会这样吗?

class ball(object):
    def __init__(self, x, y, radius, speedx, speedy):
        self.x = x
        self.y = y
        self.radius = radius
        self.speedx = speedx
        self.speedy = speedy

    def move(self):
        ##collide with vertical boundaries
        if self.y > screenHeight - self.radius - 10:
            self.speedy = self.speedy*-1
        elif self.y < self.radius + 10:
            self.speedy = self.speedy * -1


        ##collide with level walls
        if self.x == topWall.x and topWall.y < self.y < topWall.y + topWall.frontHeight:
            self.speedx = self.speedx * -1
        elif self.x == midWall.x and midWall.y < self.y < midWall.y + midWall.frontHeight:
            self.speedx = self.speedx * -1
        elif self.x == botWall.x and botWall.y < self.y < botWall.y + botWall.frontHeight:
            self.speedx = self.speedx * -1

        ##collide with player paddle
        if (self.x + self.radius == player.x + player.width and
            player.y <= self.y <= player.y + player.height):
            self.speedx = self.speedx * -1

            self.speedx = self.speedx * -1

同样,它确实可以从顶部/中部/底墙弹起,但不会使玩家弹跳。

0 个答案:

没有答案