Reputation: 15
I have 2 sprites, each of them in a different pygame.sprite.Group() and having rect.x and rect.y. However when i'm trying to detect the collisions between them, there no results. Apart by using the pygame.sprite.collide_mask, but it detect only the first collision but not the others. Although Sprites are collided in the screen. Functions which tests collisions are threads. I've tried many functions of pygame collisions like pygame.sprite.collide_mask, pygame.sprite.spritecollideany(), . . .without results.
When i print the self of each sprites of these groups, it prints that their all are sprites :
<attack1 sprite(in 1 groups)>
The main code that test collisions :
class attack1(pygame.sprite.Sprite,threading.Thread):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
threading.Thread.__init__(self)
self.image = self.imageAttack1.get_image(503 ,468 ,20 ,19)
self.image = pygame.transform.scale(self.image,(60,57)).convert_alpha()
self.lsAttack1.append(self.image)
self.rect = self.image.get_rect()
self.rect.y = 0
self.rect.x = 0
vr.spriteGroupAttack1.add(self)
self.start()
def run(self):
while vr.managementDisplay :
self.result = pygame.sprite.spritecollide(self,vr.spriteGroupCaracter1,False)
if self.result :
print('Yes')
or with groups :
def run(self):
while vr.managementDisplay :
self.result = pygame.sprite.groupcollide(vr.spriteGroupAttack1,vr.spriteGroupCaracter1,False)
if self.result :
print('Yes')
or with pygame.sprite.collide_mask
def run(self):
while vr.managementDisplay :
self.result = pygame.sprite.collide_mask(self,vr.lsCaracter[0])
if self.result :
print('Yes')
The expected result is that the collide is detected.
Upvotes: 1
Views: 141
Reputation: 14906
Sprite collision in PyGame is based on the sprite's rect
member.
So given two sprites, the code will check sprite1.rect
against sprite2.rect
for overlapping co-ordinates.
The sprite's self.rect
needs to have it's x
and y
co-ordinates updated when it moves, so that it reflects any on-screen movement of the bitmap.
As far as I can tell from the OP's code, the rect.x
and rect.y
are not updated, so the comparison will always just be at 0,0
, forever.
To use masked collision, the sprite also needs to define .mask
image, which can be generated from an image with transparency fairly easily:
self.mask = pygame.mask.from_surface( self.image )
As far as I can tell, the sprite code in the OP does not contain a mask, thus pygame.sprite.collide_mask()
will not work.
Here is an example sprite class:
class MaskedSprite( pygame.sprite.Sprite ):
def __init__( self, bitmap ):
pygame.sprite.Sprite.__init__( self )
self.image = bitmap
self.mask = pygame.mask.from_surface( self.image )
self.rect = self.image.get_rect()
self.rect.x = random.randrange( 0, WINDOW_WIDTH )
self.rect.y = random.randrange( 0, WINDOW_HEIGHT ) )
def moveTo( self, x, y ):
self.rect.x = x
self.rect.y = y
def update( self ):
pass #TODO
Obviously the bitmap parameter needs to have some transparency, otherwise the mask is a pointless waste of CPU.
Upvotes: 1