Reputation: 111
Below is part of my code for a 'Tetris' game.
What I want to do is simply to make the bottom of the falling shape equal in height(-1) to the top of the static shape, on collision. However, as it is, I can only set the contact position as the bottom of the moving shape; resulting in it appearing to sink into the shape it collides with (which throws off the whole 'block' illusion).
stick = pygame.sprite.groupcollide(pieces_mobs, static_mobs, False, False, pygame.sprite.collide_mask)
This is what I'm currently using because I'm not sure of how to access the second value in a 'groupcollide' tuple -
for piece in pieces_mobs:
for instance in stick:
contact = instance.rect.bottom
instance.static(contact)
piece.update()
This is the logic I want -
for piece in pieces_mobs:
for pieces, static in stick:
contact = static.rect.top
pieces.static(contact)
piece.update()
Probably really simple, but I just can't find any examples.
Upvotes: 1
Views: 3401
Reputation: 146
As per pygame documentation, the output of groupcollide is a dictionary with the key being a sprite from the first group and the value a list of all the sprites from second group the key sprite is colliding with. So to achieve your goal:
stick = pygame.sprite.groupcollide(pieces_mobs, static_mobs, False, False, pygame.sprite.collide_mask)
for piece_mob, static_mob in stick.items():
piece_mob.rect.bottom = static_mob[0].rect.top
Upvotes: 2