与倾斜平台精灵的Pygame碰撞故障

时间:2017-06-30 04:31:17

标签: python pygame collision-detection collision

在最近的平台电脑上工作时,我遇到了在游戏中使用的倾斜平台精灵上发生的碰撞问题。我已经尝试在倾斜的精灵上实现一个碰撞掩模,希望能够抵消这个麻烦的错误。但是当我运行该程序时,会发生这种抖动错误,其中玩家精灵在倾斜平台上上下跳动,如果我尝试像任何其他平台一样实现这个倾斜平台精灵,玩家将像精灵一样走过精灵,当它真的是一个三角形的精灵。我希望上面提到的文字,如果没有,我会非常乐意详细说明,并且赞赏任何建议:)

我认为错误出现在以下代码段之一中。如果您觉得需要查看更多代码,我会非常乐意提供更多代码。

class Platform(pygame.sprite.Sprite):
    def __init__(self, id, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.tile_images = []
        self.get_images()

        self.id = int(id)
        self.image = self.tile_images[self.id]
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

        if self.id == 5 or self.id == 4: #4 and 5 are the slanted platform sprites
            self.mask = pygame.mask.from_surface(self.image)

我的游戏课的更新部分。

def update(self):
    self.player_sprite.update()

    if self.player.rect.x <= 0:
        self.player.direction = "R"
        self.player.rect.x = 0

    if self.player.vel.y > 0:
        hits = pygame.sprite.spritecollide(self.player, self.platform_list, False)
        if hits:
            lowest = hits[0]
            for hit in hits:
                if hit.rect.bottom > lowest.rect.bottom:
                    lowest = hit
            if self.player.rect.y < lowest.rect.centery:
                self.player.pos.y = lowest.rect.top + 1
                self.player.vel.y = 0
                self.player.jumped = False

播放器更新部分,可能是重力错误?

def update(self):
    self.animate()

    self.acc.x += self.vel.x * PLAYER_FRICTION
    self.vel += self.acc
    if abs(self.vel.x) < 0.1:
        self.vel.x = 0
    self.pos += self.vel + 0.5 * self.acc
    self.rect.midbottom = self.pos
    self.acc = vec(0, PLAYER_GRAV)

1 个答案:

答案 0 :(得分:0)

我对pygame面具并不熟悉,但我会尝试一下。

 hits = pygame.sprite.spritecollide(self.player, self.platform_list, False)

使用倾斜平台的掩码测试碰撞,但碰撞处理代码使用的是 rect

 self.player.pos.y = lowest.rect.top + 1

抖动来自这里。你的玩家一直到掩码,但是在碰撞处理之后发送回来,这比掩码更高。