-1

I'm using rect based collisions right now and I want to transition into using masked collisions and I can't seem to figure out how I can implement it.

This is how I'm currently handling rect based collsions

class Player(pygame.sprite.Sprite):
    ....

    def move(self):
        if self.direction.magnitude() != 0:
            self.direction = self.direction.normalize()
      
        self.rect.x += self.direction.x * self.speed
        self.check_collision('horizontal')
        self.rect.y += self.direction.y * self.speed
        self.check_collision('vertical)

        self.rect.center = self.rect.center

    def check_collision(self, direction):
        if direction == 'horizontal':
            for sprite in self.obstacle_sprites:
                if sprite.rect.colliderect(self.rect):
                    if self.direction.x > 0:  # moving right
                        self.rect.right = sprite.rect.left
                    if self.direction.x < 0:  # moving left
                        self.rect.left = sprite.rect.right

        if direction == 'vertical':
            for sprite in self.obstacle_sprites:
               if sprite.rect.colliderect(self.rect):
                    if self.direction.y > 0:  # moving down
                        self.rect.bottom = sprite.rect.top
                    if self.direction.y < 0:  # moving up
                        self.rect.top = sprite.rect.bottom


I have seen some videos but I can't seem to understand most of them.

0 Answers0