Reputation: 41
I already have a simple game engine built where the player and enemy classes are only objects.
I would like to make them sprites so that I can check for collison and just generally manage them better, but I can't seem to find any good examples on how to do this...
I have extended both the player and enemy classes to extend pygame.sprite.Sprite
classes.
But when I checked for the collision between the two,
It said that the two classes need rects
(rectangles).
I am unaware of how to do this...
Upvotes: 1
Views: 9419
Reputation: 11180
Sprites are objects, everything you need is already included as fields in the sprite class. You need to explicitly set the bounding rectangles for the sprites.
from the pygame docs:
pygame.sprite.spritecollide find Sprites in a Group that intersect another Sprite pygame.sprite.spritecollide(sprite, group, dokill, collided = None): return Sprite_list
Return a list containing all Sprites in a Group that intersect with another Sprite. Intersection is determined by comparing the Sprite.rect attribute of each Sprite.
So you need to set sprite.rect to the rectangle that you want your sprite collision to be based on. Normaly you can use the size of your sprite image as your collision rectangle.
s.image = pygame.image.load("sprite.png").convert() #load a sprite image
s.rect = b.image.get_rect() # set collision rectangle
Upvotes: 3