2

I'm attempting to make a marble simulation in pygame (python 3.7.3) and am struggling to get collisions between marbles and components. The main issue is that the collision detection methods I've tried are not detecting the image but the whole rectangle.

class Marble(pygame.sprite.Sprite):
    def __init__(self,x,y,vx,vy,display,colours,image,ax,ay=2):
        pygame.sprite.Sprite.__init__(self)
        self.image = image
        self.rect = self.image.get_rect()
        self.image.set_colorkey((0,0,0,255))
        self.mask = pygame.mask.from_surface(self.image,255)
    def detect_collision(self,components):
        if pygame.sprite.spritecollide(self,components,False,pygame.sprite.collide_mask):
            return True`

class Masked_Component(pygame.sprite.Sprite):
    def __init__(self,x,y,start,end,image,function):
        pygame.sprite.Sprite.__init__(self)
        self.x = x
        self.y = y
        self.start = start
        self.end = end
        self.image = image
        self.function = function
        self.rect = self.image.get_rect()
        self.image.set_colorkey((0,0,0,255))
        self.mask = pygame.mask.from_surface(self.image,255)`

Any help would be appreciated and sorry for the messy code.

Lunderhill
  • 21
  • 2
  • I tested a black-sprite mask similar to this code, and it works OK. Is this how your sprite image is constructed - with a black mask? My test code is using `SpriteGroup` collide though. – Kingsley Feb 06 '20 at 21:39
  • Another thought: The *sprite-under-test* is **not** in `components` during the collision call right? Because a sprite will always collide with itself. – Kingsley Feb 06 '20 at 22:15
  • @Kingsley i have a black triangle as one the images and have attempted to only detect collisions with the black pixels. What is the _sprite-under-test_? If you need another part of my code i can put it up. – Lunderhill Feb 07 '20 at 09:11
  • If you mean whether the marble is in the components for the collision test it is not. – Lunderhill Feb 07 '20 at 09:42

0 Answers0