pygame与天花板碰撞?

时间:2017-01-08 00:41:26

标签: python pygame collision-detection

我只看到过有关墙壁碰撞的信息,但我在制作玩家时遇到了问题"碰撞"它的头在天花板上。不是撞击它的头部,速度被设置为0并且由于重力而加速回落,物体被吸到其上方平台的顶部。为什么这不起作用,我该如何解决?我跟随一个教程并追溯到我自己的改编版,内容创建者说它很难实现。如果你对角碰撞,显然可能会出现错误? (根据内容创建者的说法)

非常感谢你,老师希望我能在几天之内创造一些东西来展示人们面前,没有任何游戏引擎的经验。

def update(self):
    # Game Loop - Update
    self.all_sprites.update()
    # check if player hits a platform
    hits = pygame.sprite.spritecollide(self.player,self.platforms , False)
    if self.player.vel.y > 0:
        if hits:
            self.player.pos.y = hits[0].rect.top
            self.player.vel.y = 0
    if self.player.vel.y < 0:
        if hits:
            self.player.rect.top = hits[0].rect.bottom
            self.player.vel.y = 0

1 个答案:

答案 0 :(得分:0)

而是将速度设置为0,您可能需要将速度设置为-1或其他一些负值以强制移动&#39;向下&#39;超出边界条件。如果需要,您也可以在相同条件下添加加速度:

if hits:
        self.player.pos.y = hits[0].rect.top
        self.player.vel.y = -1  # example velocity
        self.player.acc.y = 2   # example acceleration
相关问题